mirror of
https://github.com/openfaas/faas.git
synced 2025-06-21 14:23:25 +00:00
updated sentiment analysis function to handle multiple sentences and fix decoding issue
Added MakerShift to community.md Update community.md commented reload(sys)
This commit is contained in:
@ -1,6 +1,11 @@
|
|||||||
import sys
|
import sys
|
||||||
|
import json
|
||||||
from textblob import TextBlob
|
from textblob import TextBlob
|
||||||
|
|
||||||
|
# set default encoding to UTF-8 to eliminate decoding errors
|
||||||
|
reload(sys)
|
||||||
|
sys.setdefaultencoding('utf8')
|
||||||
|
|
||||||
def get_stdin():
|
def get_stdin():
|
||||||
buf = ""
|
buf = ""
|
||||||
for line in sys.stdin:
|
for line in sys.stdin:
|
||||||
@ -10,8 +15,18 @@ def get_stdin():
|
|||||||
if(__name__ == "__main__"):
|
if(__name__ == "__main__"):
|
||||||
st = get_stdin()
|
st = get_stdin()
|
||||||
blob = TextBlob(st)
|
blob = TextBlob(st)
|
||||||
out =""
|
res = {
|
||||||
for sentence in blob.sentences:
|
"polarity": 0,
|
||||||
out = out + "Polarity: " + str(sentence.sentiment.polarity) + " Subjectivity: " + str(sentence.sentiment.subjectivity) + "\n"
|
"subjectivity": 0
|
||||||
print(out)
|
}
|
||||||
|
|
||||||
|
for sentence in blob.sentences:
|
||||||
|
res["subjectivity"] = res["subjectivity"] + sentence.sentiment.subjectivity
|
||||||
|
res["polarity"] = res["polarity"] + sentence.sentiment.polarity
|
||||||
|
|
||||||
|
total = len(blob.sentences)
|
||||||
|
|
||||||
|
res["sentence_count"] = total
|
||||||
|
res["polarity"] = res["polarity"] / total
|
||||||
|
res["subjectivity"] = res["subjectivity"] / total
|
||||||
|
print(json.dumps(res))
|
||||||
|
Reference in New Issue
Block a user