【问题标题】:Word frequency count based on two words using python使用python基于两个单词的词频计数
【发布时间】:2013-09-23 06:21:49
【问题描述】:

网上有很多资源展示了如何计算单个单词的字数 像 thisthisthis 和其他...
但是我找不到两个单词计数频率的具体示例。

我有一个包含一些字符串的 csv 文件。

FileList = "I love TV show makes me happy, I love also comedy show makes me feel like flying"

所以我希望输出是这样的:

wordscount =  {"I love": 2, "show makes": 2, "makes me" : 2 }

当然,我必须去掉所有的逗号、问号……{!, , ", ', ?, ., (,), [, ], ^, %, #, @, &, *, -, _, ;, /, \, |, }

我还将删除一些我发现的停用词 here 只是为了从文本中获取更具体的数据。

如何使用 python 实现这个结果?

谢谢!

【问题讨论】:

    标签: csv python-2.7 count frequency-analysis word-frequency


    【解决方案1】:
    >>> from collections import Counter
    >>> import re
    >>> 
    >>> sentence = "I love TV show makes me happy, I love also comedy show makes me feel like flying"
    >>> words = re.findall(r'\w+', sentence)
    >>> two_words = [' '.join(ws) for ws in zip(words, words[1:])]
    >>> wordscount = {w:f for w, f in Counter(two_words).most_common() if f > 1}
    >>> wordscount
    {'show makes': 2, 'makes me': 2, 'I love': 2}
    

    【讨论】:

    • 非常感谢@falsetru!它就像魅力一样!非常感谢!
    猜你喜欢
    • 2019-02-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-22
    • 1970-01-01
    • 1970-01-01
    • 2015-01-07
    • 1970-01-01
    相关资源
    最近更新 更多