をフロートしていていても、以下のコードは、私のテストケースに細かい動作しますが、手動でプログラムを起動したとき、それは次のエラーになり:Pythonの例外TypeError私はint型での作業と
bm25 = tf * (k + 1)
bm25 = bm25/(k * (1 - b + b * dl/avdl) + tf)
bm25 = bm25 * math.log(n/df, 2)
File "inverted_index.py", line 116, in read_from_file
bm25 = tf * (k + 1)
TypeError: cannot concatenate 'str' and 'int' objects
をすべてのシンボルが変数です(floatまたはintのいずれか)。私はストリングを含む何かをしようとしていません。
# bm25 = tf * (k + 1)
bm25 = bm25/(k * (1 - b + b * dl/avdl) + tf)
bm25 = bm25 * math.log(n/df, 2)
File "inverted_index.py", line 117, in read_from_file
bm25 = bm25/(k * (1 - b + b * dl/avdl) + tf)
TypeError: unsupported operand type(s) for -: 'int' and 'str'
ノートBM25がゼロに初期化される:私はエラーの原因となっている行をコメントアウトした場合、私はちょうど計算をしたい...
エラーは、計算の次の行に伝播します。当初、私は計算全体を1行にしましたが、それは私のスタイルチェッカーにとっては長すぎました。
私はPythonには新しく、これを解決する方法はありません。
私は間違っていますか?
EDIT 1:
完全なコードは非常に長い。
def read_from_file(self, file_name, b, k):
"""
>>> ii = InvertedIndex()
>>> ii.read_from_file("example.txt", b=0, k="inf")
>>> sorted(ii.inverted_lists.items())
[('animated', [(1, 0.415), (2, 0.415), (4, 0.415)]), \
('animation', [(3, 2.0)]), ('film', [(2, 1.0), (4, 1.0)]), \
('movie', [(1, 0.0), (2, 0.0), (3, 0.0), (4, 0.0)]), \
('non', [(2, 2.0)]), \
('short', [(3, 1.0), (4, 2.0)])]
"""
"""
>>> ii = InvertedIndex()
>>> ii.read_from_file("example.txt", b=0.75, k=1.75)
>>> sorted(ii.inverted_lists.items())
[('animated', [(1, 0.459), (2, 0.402), (4, 0.358)]), \
('animation', [(3, 2.211)]), ('film', [(2, 0.969), (4, 0.863)]), \
('movie', [(1, 0.0), (2, 0.0), (3, 0.0), (4, 0.0)]), \
('non', [(2, 1.938)]), \
('short', [(3, 1.106), (4, 1.313)])]
"""
dls = []
avdl = 0
with open(file_name, "r") as file:
record_id = 1
for line in file:
tf = 1
line = line.strip()
# Store the record as a tuple (title, description).
self.records.append(tuple(line.split("\t")))
dl = 0
for word in re.split("[^A-Za-z]+", line):
word = word.lower().strip()
# Ignore the word if it is empty.
if len(word) == 0:
continue
dl = dl + 1
if word not in self.inverted_lists:
# The word is seen for first time, create a new list.
tf = 1
self.inverted_lists[word] = [(record_id, tf)]
elif self.inverted_lists[word][-1] == (record_id, tf):
tf = tf + 1
self.inverted_lists[word][-1] = (record_id, tf)
tf = 1
elif self.inverted_lists[word][-1] != (record_id, tf):
# Make sure that the list contains the id at most once.
self.inverted_lists[word].append((record_id, tf))
record_id += 1
avdl = avdl + dl
dls.append(dl)
n = record_id - 1
avdl = avdl/n
bm25 = 0
for key in self.inverted_lists:
df = len(self.inverted_lists[key])
counter = 0
for value in self.inverted_lists[key]:
counter = counter + 1
tf = value[1]
dl = dls[value[0] - 1]
if k == "inf":
bm25 = tf * math.log(n/df, 2)
else:
bm25 = float(tf) * (float(k) + float(1))
bm25 = bm25/(float(k) * (float(1) - float(b)
+ float(b) * float(dl)/
float(avdl)) + float(tf))
bm25 = bm25 * math.log(n/df, 2)
self.inverted_lists[key][counter - 1] = (
self.inverted_lists[key][counter - 1][0],
round(bm25, 3))
b
とk
が試験により、この場合には、ユーザによって提供されます。
tf
およびdl
は、それぞれの文書内の頻繁な単語の頻度を記録する単純なカウンタです。
n
,およびdf
も、並べ替えられているデータに関するメトリックです。
おかげ@N:
私は、プログラムを起動したときに異なる扱いをされている唯一の変数は手動で、I(ユーザ)が、これらの値を入力していますので...
EDIT 2 b
とk
です。イワノフは、フロートの使用は、この問題を解決しましたが、私は過度に山車を使用した場合にのみ:
bm25 = float(tf) * (float(k) + float(1))
bm25 = bm25/(float(k) * (float(1) - float(b)
+ float(b) * float(dl)/
float(avdl)) + float(tf))
bm25 = bm25 * math.log(n/df, 2)
あなたは完全なコードを提供できますか?変数の型を浮動小数点に変換する(数値の場合のみ) –
変数 'tf'と' k'をどのように宣言しますか? –
'tf'か' k'のように見える文字列です – DavidG