问题
Currently, I'm working on Sentiment Analysis part. For this I have preferred to use Standford Core NLP library using python. I'm able to get the sentiment for each sentence using the following code : from pycorenlp import StanfordCoreNLP
nlp = StanfordCoreNLP('http://localhost:9000')
res = nlp.annotate("I love you. I hate him. You are nice. He is dumb",
properties={
'annotators': 'sentiment',
'outputFormat': 'json',
'timeout': 1000,
})
for s in res["sentences"]:
print("%d: '%s': %s %s" % (
s["index"],
" ".join([t["word"] for t in s["tokens"]]),
s["sentimentValue"], s["sentiment"]))
But, my requirement is, I have a text file which contains around 100 sentences which are seperated by new line .
So, I tried using the following code to open a text file and read the sentences and find the sentiment for each sentence.
from pycorenlp import StanfordCoreNLP
nlp = StanfordCoreNLP('http://localhost:9000')
with open("/Users/abc/Desktop/test_data.txt","r") as f:
for line in f.read().split('\n'):
print("Line:" + line)
res = nlp.annotate(line,
properties={
'annotators': 'sentiment',
'outputFormat': 'json',
'timeout': 1000,
})
for s in res["sentences"]:
print("%d: '%s': %s %s" % (
s["index"],
" ".join([t["word"] for t in s["tokens"]]),
s["sentimentValue"], s["sentiment"]))
But, somehow all the sentences of the text file are overridden and I'm getting the sentiment for the last sentence. As, I'm new to python can anyone please help me out regarding the same...
回答1:
I'll give this a stab, but as I commented, I'm not really qualified and this code will be untested. The lines added or changed are marked with # <<<<<<
.
from pycorenlp import StanfordCoreNLP
nlp = StanfordCoreNLP('http://localhost:9000')
results = [] # <<<<<<
with open("/Users/abc/Desktop/test_data.txt","r") as f:
for line in f.read().split('\n'):
print("Line:" + line)
res = nlp.annotate(line,
properties={
'annotators': 'sentiment',
'outputFormat': 'json',
'timeout': 1000,
})
results.append(res) # <<<<<<
for res in results: # <<<<<<
s = res["sentences"] # <<<<<<
print("%d: '%s': %s %s" % (
s["index"],
" ".join([t["word"] for t in s["tokens"]]),
s["sentimentValue"], s["sentiment"]))
I would imagine that for line in f.read().split('\n'):
could probably be replaced with the simpler for line in f:
, but I can't be sure without seeing your input file.
来源:https://stackoverflow.com/questions/52160274/how-to-iterate-through-each-line-of-a-text-file-and-get-the-sentiment-of-those-l