Python类型错误,即使我只处理int和

2024-05-15 17:40:13 发布

您现在位置:Python中文网/ 问答频道 /正文

下面的代码在我的测试用例中运行良好,但在手动启动程序时会导致以下错误:

                    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初始化为零。起初,我在一行中完成了整个计算,但这对于我的样式检查器来说太长了。你知道吗

我是python新手,不知道如何解决这个问题。你知道吗

我做错什么了?你知道吗

编辑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))

bk在本例中由用户通过测试提供。你知道吗

tfdl是简单的计数器,用于跟踪单词在各自文档中的频率。你知道吗

navdldf也是有关正在排序的数据的度量。你知道吗

当我手动启动程序时,唯一被区别对待的变量是bk,因为我(用户)正在输入这些值。。。你知道吗

编辑2:

感谢@N.Ivanov,使用floats确实解决了这个问题,但只有当我过度使用floats时:

                    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)

Tags: inselfiddftflinefloatrecord
1条回答
网友
1楼 · 发布于 2024-05-15 17:40:13

好吧,我想我找到了字符串的来源:

file_name = sys.argv[1]
b = sys.argv[2]
k = sys.argv[3]

ii = InvertedIndex()
ii.read_from_file(file_name, b, k)

我的main方法获取用户输入并将其传递给我的方法。我认为它不会将用户输入解释为字符串-我仍然需要习惯动态键入。。。你知道吗

@谢谢大家的帮助!你知道吗

相关问题 更多 >