Aug-30-2017, 12:10 PM
Hi all,
I am new to Python programming and I am trying to write a code on sentiment analysis. I am taking dataset from the contents of text files (.txt).
Firstly I have defined a Class called "FileClass" which has following members
I am facing a issue that the lines that I have updated for first text file inside FileClass object in fileObjects list is getting updated for rest of FileClass objects inside the fileObjects list
Like I am having 3 text files HarryBrown.txt, TheGhostWriter.txt and UpInTheAir.txt
and they are having different contents.
But on running my code I am getting an output like this:
Please advise me where I am doing it wrong.
Thanks
I am new to Python programming and I am trying to write a code on sentiment analysis. I am taking dataset from the contents of text files (.txt).
Firstly I have defined a Class called "FileClass" which has following members
import glob from textblob import TextBlob class FileClass(object): fileName = "" lines = [] overallSentiment = "" def __init__(self,name): self.fileName = name def updateLines(self,line): self.lines.append(line) def updateSentiment(self,sentiment): self.overallSentiment = sentimentThen in another class I am handling File reading and Sentiment analysis job
class FileClient(object): fileObjects = [] def __init__(self): lines = [] try: files = glob.glob('*.txt') for i,file_name in enumerate(sorted(files)): self.fileObjects.append(FileClass(file_name)) with open(file_name) as f: for line in f: singleLine = line.strip(' \t\n\r').split(".") for sl in singleLine: if (sl.strip(' \t\n\r') and len(sl.strip(' \t\n\r'))!=1): self.fileObjects[i].updateLines(sl.strip(' \t\n\r')) except Exception as e: print("Error: File reading error ", e) def get_line_sentiment(self, line): analysis = TextBlob(line) return analysis.sentiment.polarity def calculateFileOverallSentiment(self,fileObj): positiveSentiment = 0 neutralSentiment = 0 negativeSentiment = 0 for l in fileObj.lines: sentimentPolarity = self.get_line_sentiment(l) if sentimentPolarity > 0: positiveSentiment = positiveSentiment + 1 elif sentimentPolarity == 0: neutralSentiment = neutralSentiment + 1 else: negativeSentiment = negativeSentiment + 1 numOflines = len(fileObj.lines) positiveSentimentPer = (positiveSentiment * 100)/numOflines negativeSentimentPer = (negativeSentiment * 100)/numOflines neutralSentimentPer = (neutralSentiment * 100)/numOflines print("Positive Sentiments: ", positiveSentimentPer) print("Negative Sentiments: ", negativeSentimentPer) print("Neutral Sentiments: ", neutralSentimentPer) if positiveSentimentPer >= 33.33: fileObj.updateSentiment("positive") elif neutralSentimentPer >= 33.33: fileObj.updateSentiment("neutral") elif negativeSentimentPer >= 33.33: fileObj.updateSentiment("negative") else: fileObj.updateSentiment("CheckTheFuntion") return fileObj def set_sentiments(self): for f in self.fileObjects: print("Processing for: ", f.fileName) f = self.calculateFileOverallSentiment(f) def get_sentiments(self): for f in self.fileObjects: print("Overall sentiment of the file", f.fileName ," is: ", f.overallSentiment) for l in f.lines: print(l) print("*****************************************************") def main(): fileClient = FileClient() fileClient.set_sentiments() fileClient.get_sentiments() if __name__ == "__main__": # calling main function main()In the class FileClient, inside init function, I am appending an object of type FileClass on finding a text file inside the current folder.
I am facing a issue that the lines that I have updated for first text file inside FileClass object in fileObjects list is getting updated for rest of FileClass objects inside the fileObjects list
Like I am having 3 text files HarryBrown.txt, TheGhostWriter.txt and UpInTheAir.txt
and they are having different contents.
But on running my code I am getting an output like this:
Output:Processing for: HarryBrown.txt
Positive Sentiments: 16.666666666666668
Negative Sentiments: 50.0
Neutral Sentiments: 33.333333333333336
Processing for: TheGhostWriter.txt
Positive Sentiments: 16.666666666666668
Negative Sentiments: 50.0
Neutral Sentiments: 33.333333333333336
Processing for: UpInTheAir.txt
Positive Sentiments: 16.666666666666668
Negative Sentiments: 50.0
Neutral Sentiments: 33.333333333333336
Overall sentiment of the file HarryBrown.txt is: neutral
This film tells the story of Harry Brown, a pensioner living on a decaying housing estate in South London
Formerly a marine, Harry now lives a lonely life, with his wife on death’s door in a hospital and few friends in an area increasingly plagued by drugs and crime
The film tells the story of Harry’s stand against the anarchy he perceives around him and the events that forced him to take action
Given the likelihood that its director Roman Polanski may never make another film, it is difficult not to approach The Ghost Writer with high expectations and even higher hopes
Jason Reitman’s Up in the Air is one of those rare films that deals with contemporary working life
It focuses on the impact of the recession in the US, even using recently-unemployed people to give the fictional story a docudrama quality
*****************************************************
Overall sentiment of the file TheGhostWriter.txt is: neutral
This film tells the story of Harry Brown, a pensioner living on a decaying housing estate in South London
Formerly a marine, Harry now lives a lonely life, with his wife on death’s door in a hospital and few friends in an area increasingly plagued by drugs and crime
The film tells the story of Harry’s stand against the anarchy he perceives around him and the events that forced him to take action
Given the likelihood that its director Roman Polanski may never make another film, it is difficult not to approach The Ghost Writer with high expectations and even higher hopes
Jason Reitman’s Up in the Air is one of those rare films that deals with contemporary working life
It focuses on the impact of the recession in the US, even using recently-unemployed people to give the fictional story a docudrama quality
*****************************************************
Overall sentiment of the file UpInTheAir.txt is: neutral
This film tells the story of Harry Brown, a pensioner living on a decaying housing estate in South London
Formerly a marine, Harry now lives a lonely life, with his wife on death’s door in a hospital and few friends in an area increasingly plagued by drugs and crime
The film tells the story of Harry’s stand against the anarchy he perceives around him and the events that forced him to take action
Given the likelihood that its director Roman Polanski may never make another film, it is difficult not to approach The Ghost Writer with high expectations and even higher hopes
Jason Reitman’s Up in the Air is one of those rare films that deals with contemporary working life
It focuses on the impact of the recession in the US, even using recently-unemployed people to give the fictional story a docudrama quality
*****************************************************
You can see that Content for HarryBrown.txt is showing for all other files.Please advise me where I am doing it wrong.
Thanks