【问题标题】:Python function to find # of unique words/total words not working...why?Python 函数查找唯一单词数/总单词数不起作用……为什么?
【发布时间】:2013-11-10 02:41:54
【问题描述】:

为什么这段代码不起作用?

def hapax_legomana_ratio(text):
''' Return the hapax_legomana ratio for this text.
This ratio is the number of words that occur exactly once divided
by the total number of words.
text is a list of strings each ending in \n.
At least one line in text contains a word.'''

uniquewords=dict()
words=0
for line in text:
    line=line.strip().split()
    for word in line:
        words+=1
        if word in words:
            uniquewords[word]-=1
        else:
            uniquewords[word]=1
HLR=len(uniquewords)/words

print (HLR)

当我测试它时,它给了我错误的答案。例如,当 9 个字符串中有 3 个唯一单词时,它给我 0.20454545454545456 而不是 .33333。

【问题讨论】:

    标签: python python-3.x unique


    【解决方案1】:

    要找到比率:文本中唯一单词的数量与总单词的数量:

    from collections import Counter
    
    def hapax_legomana_ratio(text):
        words = text.split() # a word is anything separated by a whitespace
        return sum(count == 1 for count in Counter(words).values()) / len(words)
    

    它假定text 是一个字符串。相反,如果您有一个行列表,那么您可以获得words 列表,如下所示:

    words = [word for line in all_lines for word in line.split()]
    

    【讨论】:

      【解决方案2】:

      您的代码中有很多谬误。我认为if word in words 行中有错字,因为它应该是uniquewords(dict)而不是words(这是计数)。

      更多的是,您提供的文本应该被分成几行并且应该是这些行的列表。我宁愿建议这样做

      for line in text.splitlines():
      

      这样您就不必担心被传递的文本是list

      此外,您正在做len(uniquewords) 这是错误的,因为您将 all 单词存储在 dict 中,而不管它们是否唯一。单词的唯一性由从字典中获得的value 给出,通过将单词作为key 传递,即1 或-1。因此,您应该遍历字典的项目并计算值为1 的键。

      另外,你没有注意标点符号!假设这是文本

      这是一个测试,
      是的,这是一个测试。

      注意test,test. 在单词dict 中的存储方式有何不同?

      稍作修正的代码如下。

      def hapax_legomana_ratio(text):
          ''' Return the hapax_legomana ratio for this text.
          This ratio is the number of words that occur exactly once divided
          by the total number of words.
          text is a list of strings each ending in \n.
          At least one line in text contains a word.'''
      
          uniquewords = dict()
          words = 0
          for line in text:
              line = line.strip().split()
              for word in line:
                  words += 1
                  word = word.replace(',', '').strip()
                  if word in uniquewords:
                      uniquewords[word] -= 1
                  else:
                      uniquewords[word] = 1
      
          unique_count = 0
          for each in uniquewords:
              if uniquewords[each] == 1:
                  unique_count += 1
          HLR = unique_count/words
      
          print (HLR)
      

      最后,如果这是一个非常大的项目和/或您将来也需要它,我宁愿建议使用 collection.Counter 库来完成所有这些,而不是这样做。

      【讨论】:

      • 使用同样的测试,这个方法给了我0.045454545454545456。
      • 抱歉,我认为分割线部分是 line.strip 和分割的替代品。完美运行,谢谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-02
      • 1970-01-01
      • 2021-02-16
      • 2020-03-21
      • 2021-10-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多