Python Forum
List data-member of object not updating inside loop.
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
Reply
#2
you mix class and instance variables. This is common gotcha when one is inexperienced with classes - don't worry :-)
Reply
#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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 317 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  Variable definitions inside loop / could be better? gugarciap 2 373 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  How to create a variable only for use inside the scope of a while loop? Radical 10 1,524 Nov-07-2023, 09:49 AM
Last Post: buran
  Class member become static Quasar999 1 643 Sep-16-2023, 12:52 PM
Last Post: deanhystad
  Possible to create an object inside another object that is visible outside that objec MeghansUncle2 17 2,088 May-25-2023, 02:08 PM
Last Post: MeghansUncle2
  How do I call sys.argv list inside a function, from the CLI? billykid999 3 752 May-02-2023, 08:40 AM
Last Post: Gribouillis
  Reading All The RAW Data Inside a PDF NBAComputerMan 4 1,274 Nov-30-2022, 10:54 PM
Last Post: Larz60+
  add object and name in list 3lnyn0 4 1,226 Nov-24-2022, 07:33 PM
Last Post: buran
  Help adding a loop inside a loop Extra 31 4,333 Oct-23-2022, 12:16 AM
Last Post: Extra
  AttributeError: 'list' object has no attribute 'upper' Anldra12 4 4,721 Apr-27-2022, 09:27 AM
Last Post: Anldra12

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020