I am trying to analyze the sentiment of each given sentence from a text file line by line. The code is working whenever I am using the hard coded sentences from the first question linked. When I use the text file input, I get the TypeError
.
This is related to the question asked here. And the line by line from text file code is coming from this question:
The first one works, the second with the text-file ("I love you. I hate him. You are nice. He is dumb")
does not work. Here is the code :
from pycorenlp import StanfordCoreNLP
nlp = StanfordCoreNLP('http://localhost:9000')
results = []
with open("c:/nlp/test.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 get this error:
line 21, in
s["index"],
TypeError: list indices must be integers or slices, not str
Looks like I solved the problem. As londo pointed out: This line sets S
as List
, but it should be dict
, just like in the original code:
s = res["sentences"]
I moved the code into the same loop where the file is read and analyzed line by line and I print the result directly there. So the new code looks like this:
from pycorenlp import StanfordCoreNLP
nlp = StanfordCoreNLP('http://localhost:9000')
with open("c:/nlp/test.txt","r") as f:
for line in f.read().split('\n'):
res = nlp.annotate(line,
properties={
'annotators': 'sentiment',
'outputFormat': 'json',
'timeout': 15000,
})
for s in res["sentences"]:
print("%d: '%s': %s %s" % (
s["index"],
" ".join([t["word"] for t in s["tokens"]]),
s["sentimentValue"], s["sentiment"]))
The result looks just as intended and without any error message:
0: 'I love you .': 3 Positive
0: 'I hate him .': 1 Negative
0: 'You are nice .': 3 Positive
0: 'He is dumb .': 1 Negative