ZeroDivisionError ,但我找不到错误
ZeroDivisionError , but I can't find the error
我遇到了一个小零错误但是找不到。我的目的是比较包含这些词的文本文件。
secondly
pardon
woods
secondly
我写的脚本是这样比较两个值的:
secondly, pardon
secondly, woods
secondly, secondly
pardon, woods
pardon, secondly
woods, secondly
我的代码执行以下操作:
1) 如果单词相同则给1分,否则为gensim向量模型计算的分数
2)有一个计数器,当第一个for循环移动到下一个单词时,计数器将重置。例如,secondly,pardon > secondly, woods > secondly, secondly(此时计数为3)
代码
from __future__ import division
import gensim
textfile = 'businessCleanTxtUniqueWords'
model = gensim.models.Word2Vec.load("businessSG")
count = 0 # keep track of counter
score = 0
avgScore = 0
SentenceScore = 0
externalCount = 0
totalAverageScore = 0
with open(textfile, 'r+') as f1:
words_list = f1.readlines()
for each_word in words_list:
word = each_word.strip()
for each_word2 in words_list[words_list.index(each_word) + 1:]:
count = count + 1
try:
word2 = each_word2.strip()
print(word, word2)
# if words are the same
if (word == word2):
score = 1
else:
score = model.similarity(word,word2) # when words are not the same
# if word is not in vector model
except KeyError:
score = 0
# to keep track of the score
SentenceScore=SentenceScore + score
print("the score is: " + str(score))
print("the count is: " + str(count))
# average score
avgScore = round(SentenceScore / count,5)
print("the avg score: " + str(SentenceScore) + '/' + str(count) + '=' + str(avgScore))
# reset counter and sentence score
count = 0
SentenceScore = 0
错误信息:
Traceback (most recent call last):
File "C:/Users/User/Desktop/Complete2/Complete/TrainedTedModel/LatestJR.py", line 41, in <module>
avgScore = round(SentenceScore / count,5)
ZeroDivisionError: division by zero
('secondly', 'pardon')
the score is: 0.180233083443
the count is: 1
('secondly', 'woods')
the score is: 0.181432347816
the count is: 2
('secondly', 'secondly')
the score is: 1
the count is: 3
the avg score: 1.36166543126/3=0.45389
('pardon', 'woods')
the score is: 0.405021005657
the count is: 1
('pardon', 'secondly')
the score is: 0.180233083443
the count is: 2
the avg score: 0.5852540891/2=0.29263
('woods', 'secondly')
the score is: 0.181432347816
the count is: 1
the avg score: 0.181432347816/1=0.18143
我已经为除法添加了“from __future__ import division
”,但它似乎没有解决
我的文件可以在下面找到link:
Gensim 模型:
文本文件:
谢谢。
因为第一个for
循环已经到了最后一个字,第二个for
循环不会执行所以count
等于0(在最后一次迭代)。只需更改第一个 for
循环以忽略最后一个单词(因为没有必要):
for each_word in words_list[:-1]:
出错的那一行直接在错误信息中说明:
Traceback (most recent call last):
File "C:/Users/User/Desktop/Complete2/Complete/TrainedTedModel/LatestJR.py", line 41, in <module>
avgScore = round(SentenceScore / count,5)
ZeroDivisionError: division by zero
所以我假设 SentenceScore / count
是有问题的分区,所以很明显 count
是 0,我建议在该行之前添加如下内容:
print("SentenceScore is",SentenceScore, "and count is",count)
所以你可以自己看看这个,现在因为内部循环:
对于 each_word2 在 words_list[words_list.index(each_word) + 1:] 中:
计数 = 计数 + 1
是唯一增加计数的东西,计数在外循环的每次迭代结束时被重置为零,这意味着内循环在某些时候根本不是 运行,这意味着 words_list[words_list.index(each_word) + 1:]
是一个空序列。当 each_word
是 words_list
中的最后一个单词时会发生这种情况。
我遇到了一个小零错误但是找不到。我的目的是比较包含这些词的文本文件。
secondly
pardon
woods
secondly
我写的脚本是这样比较两个值的:
secondly, pardon
secondly, woods
secondly, secondly
pardon, woods
pardon, secondly
woods, secondly
我的代码执行以下操作:
1) 如果单词相同则给1分,否则为gensim向量模型计算的分数 2)有一个计数器,当第一个for循环移动到下一个单词时,计数器将重置。例如,secondly,pardon > secondly, woods > secondly, secondly(此时计数为3)
代码
from __future__ import division
import gensim
textfile = 'businessCleanTxtUniqueWords'
model = gensim.models.Word2Vec.load("businessSG")
count = 0 # keep track of counter
score = 0
avgScore = 0
SentenceScore = 0
externalCount = 0
totalAverageScore = 0
with open(textfile, 'r+') as f1:
words_list = f1.readlines()
for each_word in words_list:
word = each_word.strip()
for each_word2 in words_list[words_list.index(each_word) + 1:]:
count = count + 1
try:
word2 = each_word2.strip()
print(word, word2)
# if words are the same
if (word == word2):
score = 1
else:
score = model.similarity(word,word2) # when words are not the same
# if word is not in vector model
except KeyError:
score = 0
# to keep track of the score
SentenceScore=SentenceScore + score
print("the score is: " + str(score))
print("the count is: " + str(count))
# average score
avgScore = round(SentenceScore / count,5)
print("the avg score: " + str(SentenceScore) + '/' + str(count) + '=' + str(avgScore))
# reset counter and sentence score
count = 0
SentenceScore = 0
错误信息:
Traceback (most recent call last):
File "C:/Users/User/Desktop/Complete2/Complete/TrainedTedModel/LatestJR.py", line 41, in <module>
avgScore = round(SentenceScore / count,5)
ZeroDivisionError: division by zero
('secondly', 'pardon')
the score is: 0.180233083443
the count is: 1
('secondly', 'woods')
the score is: 0.181432347816
the count is: 2
('secondly', 'secondly')
the score is: 1
the count is: 3
the avg score: 1.36166543126/3=0.45389
('pardon', 'woods')
the score is: 0.405021005657
the count is: 1
('pardon', 'secondly')
the score is: 0.180233083443
the count is: 2
the avg score: 0.5852540891/2=0.29263
('woods', 'secondly')
the score is: 0.181432347816
the count is: 1
the avg score: 0.181432347816/1=0.18143
我已经为除法添加了“from __future__ import division
”,但它似乎没有解决
我的文件可以在下面找到link:
Gensim 模型:
文本文件:
谢谢。
因为第一个for
循环已经到了最后一个字,第二个for
循环不会执行所以count
等于0(在最后一次迭代)。只需更改第一个 for
循环以忽略最后一个单词(因为没有必要):
for each_word in words_list[:-1]:
出错的那一行直接在错误信息中说明:
Traceback (most recent call last):
File "C:/Users/User/Desktop/Complete2/Complete/TrainedTedModel/LatestJR.py", line 41, in <module>
avgScore = round(SentenceScore / count,5)
ZeroDivisionError: division by zero
所以我假设 SentenceScore / count
是有问题的分区,所以很明显 count
是 0,我建议在该行之前添加如下内容:
print("SentenceScore is",SentenceScore, "and count is",count)
所以你可以自己看看这个,现在因为内部循环:
对于 each_word2 在 words_list[words_list.index(each_word) + 1:] 中: 计数 = 计数 + 1
是唯一增加计数的东西,计数在外循环的每次迭代结束时被重置为零,这意味着内循环在某些时候根本不是 运行,这意味着 words_list[words_list.index(each_word) + 1:]
是一个空序列。当 each_word
是 words_list
中的最后一个单词时会发生这种情况。