How to iterate through each line of a text file and get the sentiment of those lines using python?

一世执手 提交于 2019-12-13 09:03:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!