【问题标题】:Python NLTK: Count list of word and make probability with valid English wordsPython NLTK:计算单词列表并使用有效英文单词生成概率
【发布时间】:2013-03-12 12:42:22
【问题描述】:

我有一个脏文档,其中包含无效的英文单词、数字等。 我只想取所有有效英文单词,然后计算我的单词列表与有效英文单词总数的比率。

例如,如果我的文档有句:

sentence= ['eishgkej he might be a good person. I might consider this.']

我只想数 "he might be a good person. I might consider this" 和数 "might"

所以,我在 2/10 得到了答案。

我正在考虑使用以下代码。但是,我需要更改的不是 features[word] = 1 行,而是功能数量...

 all_words = nltk.FreqDist(w.lower() for w in reader.words() if w.lower() not in english_sw)

 def document_features(document):
     document_words = set(document)
     features = {}
     for word in word_features:
         if word in document_words:
             features[word] = 1
         else:
             features[word]=0
     return features

【问题讨论】:

  • 您是否尝试过在字典中查找单词,例如词网?
  • english_swword_features 定义在哪里?
  • 哦,english_sw 是“字典”,如 lasmans 所说的 WordNet..
  • 并且 word_features 在这一点上是“可能”。我只是假设有什么..

标签: python nlp nltk


【解决方案1】:

根据the documentation,您可以使用count(self, sample) 返回FreqDist 对象中单词的计数。所以我认为你想要这样的东西:

 for word in word_features:
     if word in document_words:
         features[word] = all_words.count(word)
     else:
         features[word]= 0

或者您可以使用索引,即 all_words[word] 应该返回与 all_words.count(word) 相同的结果

如果你想要单词的频率你可以做all_words.freq(word)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多