为了账号安全,请及时绑定邮箱和手机立即绑定

使用文本搭配计算 ngram 词频

使用文本搭配计算 ngram 词频

翻翻过去那场雪 2021-10-26 16:32:59
我想计算已转换为标记的文本文件中特定单词前后三个单词的频率。from nltk.tokenize import sent_tokenizefrom nltk.tokenize import word_tokenizefrom nltk.util import ngramswith open('dracula.txt', 'r', encoding="ISO-8859-1") as textfile:    text_data = textfile.read().replace('\n', ' ').lower()tokens = nltk.word_tokenize(text_data)text = nltk.Text(tokens)grams = nltk.ngrams(tokens, 4)freq = Counter(grams)freq.most_common(20)我不知道如何搜索字符串 'dracula' 作为过滤词。我也试过:text.collocations(num=100)text.concordance('dracula')所需的输出看起来像这样的计数:“dracula”之前的三个词,排序计数(('and', 'he', 'saw', 'dracula'), 4),(('one', 'cannot', 'see', 'dracula'), 2)'dracula' 后面的三个词,排序计数(('dracula', 'and', 'he', 'saw'), 4),(('dracula', 'one', 'cannot', 'see'), 2)中间包含 'dracula' 的三元组,排序计数(('count', 'dracula', 'saw'), 4),(('count', 'dracula', 'cannot'), 2)预先感谢您的任何帮助。
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 194 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号