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
SasList, but it should bedict, just like in the original code: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:
The result looks just as intended and without any error message: