import re
class HashTable:
def __init__(self, keyword, averageScore = 0, occurences = 0):
self.size = 20011
self.keyword = keyword
self.averageScore = averageScore
self.occurences = occurences
self.slots = [None] * self.size
self.data = [None] * self.size
def getAverageScore(self) :
return self.averageScore
def getWord(self) :
return self.keyword
def getOccurences(self) :
return self.occurences
def addNewScore(self, newScore) :
oldScoreSum = self.averageScore * self.occurences
self.occurences = self.occurences + 1
self.averageScore = (oldScoreSum + newScore) / (self.occurences)
def put(self, key, data):
hash_value = self.hash_function(key,len(self.slots))
if self.slots[hash_value] == None:
self.slots[hash_value] = key
self.data[hash_value] = data
else:
if self.slots[hash_value] == key:
self.data[hash_value] = data #replace
else:
next_slot = self.rehash(hash_value, len(self.slots))
while self.slots[next_slot] != None and \
self.slots[next_slot] != key:
next_slot = self.rehash(next_slot, len(self.slots))
if self.slots[next_slot] == None:
self.slots[next_slot] = key
self.data[next_slot] = data
else:
self.data[next_slot] = data #replace
def hash_function(self, key, size):
return key % size
def rehash(self, old_hash, size):
return (old_hash + 1) % size
def get(self, key):
start_slot = self.hash_function(key, len(self.slots))
data = None
stop = False
found = False
position = start_slot
while self.slots[position] != None and not found and not stop:
if self.slots[position] == key:
found = True
data = self.data[position]
else:
position=self.rehash(position, len(self.slots))
if position == start_slot:
stop = True
return data
def __getitem__(self, key):
return self.get(key)
def __setitem__(self, key, data):
self.put(key, data)
def printWordStatistic(self) :
print("Word : ", self.keyword)
print("Occurences : ", self.occurences)
print("Average Score : ", self.occurences, "\n\n")
def printAllWordStatistic(wordDictionary) :
for wordStatistic in table.getWord :
wordStatistic.printWordStatistic()
# To rate a review based on the training data
fileInstance = open("movieReviews.txt",'r')
fileText = fileInstance.read()
table = HashTable(fileText)
# Assuming, that each review is seperated by following delimeter
reviewSplits = fileText.splitlines()
for review in reviewSplits :
#succesfully splits review into lines
#print(review)
review = review.strip()
if review == "" :
continue
# In each review, first line contains the score and the
# subsequent lines contains the text
score = float(review[0])
print(score)
for i in range(1, len(review)) :
# Splitting out the words in each line of the review
wordSplits = re.split("\t| ", review[i])
for word in wordSplits :
if word == "" :
continue
# If it is already present, then update the score and count
# Otherwise just add the new entry to the dictionary
if table.keyword(word) :
wordStatistic = table.getWord(word)
wordStatistic.addNewScore(score)
else :
wordStatistic = HashTable(word, score, 1)
table[word] = wordStatistic
# Training Phase Completed
# To print the statistics of all words in the dictionary
def calculateAverageOfReview(review) :
review.replace("\t", " ")
review.replace("\n", " ")
wordSplits = review.split(" ")
averageScore = 0.0
totalCount = 0;
for word in wordSplits :
if table.getWord():
averageScore += table.getWord().getAverageScore().split(" ")
totalCount = totalCount + 1
if totalCount != 0 :
return averageScore / totalCount
return -1
while (True) :
print("\nEnter a review, (enter empty-line to save) : ")
multiLines = []
while True:
line = input()
if line:
multiLines.append(line)
else:
break
inputReview = '\n'.join(multiLines)
averageScore = calculateAverageOfReview(inputReview)
if averageScore != -1 :
if averageScore >= 2.50 :
print("Positive Review")
else :
print("Negative Review")
else :
print("Unable to rate the review")
if input("\nDo you want to continue ? (Y/N) : ") != "Y" :
print("Quitting the session. Good Bye !")
exit()
我正在写一个电影评论情感分析代码。我有以下输入和输出
1-输入评论-按return退出:
以一个又快又无聊的结局结尾的软弱的剧本
审查的平均值为1.79128
消极情绪
2-输入评论-按return退出:
爱它的每一分钟
审查的平均值为2.39219
积极情绪
因此,当我运行代码时,会得到如下属性错误
averageScore+=表格.getWord().getAverageScore().split(“”)
AttributeError:“str”对象没有“getAverageScore”属性
有什么问题吗
目前没有回答
相关问题 更多 >
编程相关推荐