【问题标题】:Unique word frequency using NLTK使用 NLTK 的唯一词频
【发布时间】:2018-10-01 01:00:03
【问题描述】:

使用 NLTK 获取以下唯一词频的代码。

Seq Sentence
1 Let's try to be Good.
2 Being good doesn't make sense.
3 Good is always good.

输出:
{'good':3, 'let':1, 'try':1, 'to':1, 'be':1, 'being':1, 'doesn':1, 't':1, 'make':1, 'sense':1, 'is':1, 'always':1, '.':3, ''':2, 's':1}

【问题讨论】:

    标签: python nltk token


    【解决方案1】:

    如果你对使用 nltk 非常讲究,请参考以下代码 sn-p

    import nltk
    
    text1 = '''Seq Sentence 
    1   Let's try to be Good.
    2   Being good doesn't make sense.
    3   Good is always good.'''
    
    words = nltk.tokenize.word_tokenize(text1)
    fdist1 = nltk.FreqDist(words)
    
    filtered_word_freq = dict((word, freq) for word, freq in fdist1.items() if not word.isdigit())
    
    print(filtered_word_freq)
    

    希望对你有帮助。

    引用了一些部分来自:

    How to check if string input is a number?

    Dropping specific words out of an NLTK distribution beyond stopwords

    【讨论】:

      【解决方案2】:

      试试这个

      from collections import Counter
      import pandas as pd
      import nltk
      
      sno = nltk.stem.SnowballStemmer('english')
      s = "1   Let's try to be Good. 2   Being good doesn't make sense. 3   Good is always good."
      s1 = s.split(' ')
      d = pd.DataFrame(s1)
      s2 = d[0].apply(lambda x: sno.stem(x))
      counts =  Counter(s2)
      print(counts)
      

      输出将是:

      Counter({'': 6, 'be': 2, 'good.': 2, 'good': 2, '1': 1, 'let': 1, 'tri': 1, 'to': 1, '2': 1, "doesn't": 1, 'make': 1, 'sense.': 1, '3': 1, 'is': 1, 'alway': 1})
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-11-12
        • 1970-01-01
        • 2012-06-15
        • 2013-06-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多