1

I am trying to run a few hundred Trip Advisor reviews through a Sentiment Analysis program (via textblob), so that it will read each review and provide a sentiment for it. The program already works in that you can type a sentence in and it will return the sentiment (i.e. positive, negative, neutral). I would like to run a excel document through the program without having to manually type in each review. Ideally, the program returns a sentiment on each review.....How do I do this?

This is the code I already have...

from textblob import TextBlob
import string

z = 10

poscounter = 0

negcounter = 0

neucounter = 0

totalsentences = 0

while z > 0:
    
y = input("Type your sentence ").lower()  
    y = y.translate(str.maketrans('', '', string.punctuation))
    y1 = TextBlob(y)
    sentCheck = y1.sentiment.polarity

    if y == "stop":
        
            print(f"Positive Sentiment: {(poscounter/totalsentences) * 100} %")
            print(f"Negative Sentiment: {(negcounter/totalsentences) * 100} %")
            print(f"Neutral Sentiment: {(neucounter/totalsentences) * 100} %")
            exit()
            
        
    xplitIt = y.split(" ")
    
    for something in xplitIt:
        if something == "crowded" or something == "busy" or something == "crowds" or something == "hate" or something == "hated":
            print("negative")
            sentCheck = -0.1
            break
            
            

    if sentCheck==0:
        print("neutral")
        neucounter+=1

    elif sentCheck>0 and sentCheck <=1:
        print("positive")
        poscounter+=1

    elif sentCheck == -0.1 or sentCheck < 0:
        negcounter+=1
        if sentCheck != -0.1:
            print("negative")
          
    totalsentences = totalsentences + 1

   
Guest User
  • 29
  • 3

1 Answers1

1

There are libraries to read text from word documents. You can have a look at textract that was suggested in this answer.

The text you read will probably have multiple reviews that you have to split into a list of reviews (e.g. with python's split() function). You can then give each of these reviews as y into your algorithm.

How you have to process your input depends on the structure of your document. You should include an example in your question if you need further help.

JANO
  • 2,995
  • 2
  • 14
  • 29