python - NLTK:同现矩阵
问题描述
我已经预处理了一个评论数据集并对其进行了标记。这是df的样子:
然后我从列中创建两个列表:
cent_vocab - 包含前 1000 个最常用的名词(中心词)
cont_vocab - 包含前 1000 个最常用的动词/形容词(上下文词)
使用这两个 1,000 词的词汇表,我想创建一个共现矩阵,在该矩阵中,对于每个中心词,您可以跟踪有多少上下文词与它同时出现。以这个只有一个句子的简短评论为例,我们希望获得中心词餐厅的动词和形容词的共现:
一个。'大餐厅提供美味佳肴' </p>
{'餐厅':{'大':2,'服务':1,'美味':1}}
该lower_tagged
列包含大约 95,000 行
def get_coocs(df, cent_vocab, cont_vocab):
coocs = {}
reviews = df["tokenized"]
for cent in cent_vocab:
cont_coocs = {}
for review in reviews:
if cent in review:
for cont in cont_vocab:
if cont in review:
if cont in cont_coocs:
cont_coocs[cont] += 1
else:
cont_coocs[cont] = 1
else:
cont_coocs[cont] = 0
coocs[cent] = cont_coocs
return coocs
解决方案
推荐阅读
- scala - 无法使用 scala 案例类读取 google.protobuf.Timestamp
- mongodb - MongoDB sortByCount 运算符
- json - 如何将带有从 HTTP 请求收到的天数的 Json 解析为 Scala 和 Play 框架中的列表
- git - 为什么我不能合并这些远程分支?
- azure - 使用 Power Shell 7 在 Visual Studio 中验证/部署 ARM 模板
- jenkins - 在 Jenkins 管道上使用 phpunit (symphony) 和 Allure 报告
- javascript - 如何获取每张卡上的按钮 javascript
- flutter - 如何在颤动中添加背景图像?
- git - Reposurgeon 使所有分支都过时,但在 git 树中掌握
- http - Nginx,多服务中的https到http重定向