Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 List data-member of object not updating inside loop.
#1
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

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 = sentiment
Then 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
Quote
#2
you mix class and instance variables. This is common gotcha when one is inexperienced with classes - don't worry :-)
Quote
#3
Thanks for the help.
Now I got it and corrected myself like this:

import glob
from textblob import TextBlob

class FileClass(object):	
	def __init__(self,name):
		self.fileName = name
		self.lines = []
		self.overallSentiment = ""

	def updateLines(self,line):
		self.lines.append(line)

	def updateSentiment(self,sentiment):
		self.overallSentiment = sentiment


class FileClient(object):
	
	def __init__(self):
		self.fileObjects = []
		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()
And I am getting the result as expected.

I want to ask a question that if I declare a class variable and an instance variable having same name like I have done in "FileClass" class, are they different or same ?

and thanks again.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Is it possible to avoid 2 loops inside another loop? SvetlanaofVodianova 2 171 Nov-27-2019, 02:30 PM
Last Post: Gribouillis
  Using loop for getting data in several List quyenca 4 170 Nov-26-2019, 04:36 PM
Last Post: quyenca
  print all method and property of list object engmoh 4 284 Oct-26-2019, 05:33 PM
Last Post: engmoh
  Loop through tags inside tags in Selenium/Python xpack24 1 136 Oct-23-2019, 10:15 AM
Last Post: Larz60+
  How does a single object see all the values inside it? jenniferruurs 1 80 Oct-01-2019, 04:57 PM
Last Post: stullis
  Loop files - Extract List Data To Individual Columns in CSV dj99 5 432 May-19-2019, 10:29 AM
Last Post: dj99
  Getting list inside of a list; how to clean it and retrieve value mrapple2020 5 403 Apr-10-2019, 09:47 PM
Last Post: Larz60+
  plotting inside the loop is not working jenya56 4 389 Apr-10-2019, 08:11 PM
Last Post: perfringo
  sorted object in list trois 2 370 Mar-04-2019, 09:12 AM
Last Post: trois
  How I can recognize that member is classmethod of staticmethod? AlekseyPython 0 315 Feb-17-2019, 07:01 AM
Last Post: AlekseyPython

Forum Jump:


Users browsing this thread: 1 Guest(s)