Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

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

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('
'):
        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...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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('
'):
        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(' '): could probably be replaced with the simpler for line in f:, but I can't be sure without seeing your input file.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...