首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获取python或R中最常见的短语或单词

如何获取python或R中最常见的短语或单词
EN

Stack Overflow用户
提问于 2018-03-31 16:27:52
回答 2查看 2.7K关注 0票数 0

给出一些文本,我如何才能在n=1中得到最常见的n克到6?我见过一次只取3克,或2克的方法,但有没有办法提取最有意义的最大长度短语,还有其他的方法呢?

例如,在本文中,仅用于演示目的:fri evening commute can be long. some people avoid fri evening commute by choosing off-peak hours. there are much less traffic during off-peak.

N克及其计数器的理想结果是:

代码语言:javascript
复制
fri evening commute: 3,
off-peak: 2,
rest of the words: 1

任何建议都很感激。谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-03-31 21:44:15

票数 1
EN

Stack Overflow用户

发布于 2018-03-31 21:28:51

Python

考虑NLTK库,它提供了一个ngram函数,您可以使用它来迭代n的值。

粗略的实现将遵循以下思路,其中rough是此处的关键字:

代码语言:javascript
复制
from nltk import ngrams
from collections import Counter

result = []
sentence = 'fri evening commute can be long. some people avoid fri evening commute by choosing off-peak hours. there are much less traffic during off-peak.'
# Since you are not considering periods and treats words with - as phrases
sentence = sentence.replace('.', '').replace('-', ' ')

for n in range(len(sentence.split(' ')), 1, -1):
    phrases = []

    for token in ngrams(sentence.split(), n):
        phrases.append(' '.join(token))

    phrase, freq = Counter(phrases).most_common(1)[0]
    if freq > 1:
        result.append((phrase, n))
        sentence = sentence.replace(phrase, '')

for phrase, freq in result:
    print('%s: %d' % (phrase, freq))

至于R

这可能会有帮助

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49589974

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档