在python中创建代码以从列表中获取最频繁的标记和值对 - python

我有一个包含3列的.txt文件:单词位置,单词和标记(NN,VB,JJ等)。

txt文件示例:

1   i   PRP

2   want    VBP

3   to  TO

4   go  VB

我想在列表中成对地找到单词和标签的频率,以便找到最常分配给单词的标签。
结果示例:
3(食物,NN),2(勇敢,ADJ)

我的想法是从文件夹中打开文件,逐行读取文件并拆分,使用字典设置计数器,然后按降序打印最常见或不常见的内容。

我的代码非常粗糙(我几乎不好意思发布它):

file=open("/Users/Desktop/Folder1/trained.txt")
wordcount={}
for word in file.read().split():
    from collections import Counter
    c = Counter()
    for d in dicts.values():
        c += Counter(d)

print(c.most_common())

file.close()

显然,我没有任何结果。一切都会有帮助的。谢谢。

更新:

所以我在这里发布了这段有效的代码,但是我的结果有点时髦。这是代码(作者删除了它,所以我不知道该归谁):

file=open("/Users/Desktop/Folder1/trained.txt").read().split('\n')

d = {}
for i in file:
    if i[1:] in d.keys():
        d[i[1:]] += 1
    else:
        d[i[1:]] = 1

print (sorted(d.items(), key=lambda x: x[1], reverse=True))

这是我的结果:

[('', 15866), ('\t.\t.', 9479), ('\ti\tPRP', 7234), ('\tto\tTO', 4329), ('\tlike\tVB', 2533), ('\tabout\tIN', 2518), ('\tthe\tDT', 2389), ('\tfood\tNN', 2092), ('\ta\tDT', 2053), ('\tme\tPRP', 1870), ('\twant\tVBP', 1713), ('\twould\tMD', 1507), ('0\t.\t.', 1427), ('\teat\tVB', 1390), ('\trestaurant\tNN', 1371), ('\tuh\tUH', 1356), ('1\t.\t.', 1265), ('\ton\tIN', 1237), ("\t'd\tMD", 1221), ('\tyou\tPRP', 1145), ('\thave\tVB', 1127), ('\tis\tVBZ', 1098), ('\ttell\tVB', 1030), ('\tfor\tIN', 987), ('\tdollars\tNNS', 959), ('\tdo\tVBP', 956), ('\tgo\tVB', 931), ('2\t.\t.', 912), ('\trestaurants\tNNS', 899),

好的结果似乎与单词混合在一起,而其他结果则与空格或随机数混合在一起,有人知道一种去除不是真单词的方法吗?另外,我知道\ t应该表示一个标签,还有没有办法删除它?你们真的帮了很多

参考方案

每个单词都需要有一个单独的collections.Counter。此代码使用defaultdict创建计数器字典,而无需检查每个单词是否已知。

from collections import Counter, defaultdict

counts = defaultdict(Counter)
for row in file:           # read one line into `row`
    if not row.strip():
        continue           # ignore empty lines
    pos, word, tag = row.split()
    counts[word.lower()][tag] += 1

就是这样,您现在可以检查任何单词的最常用标签:

print(counts["food"].most_common(1))
# Prints [("NN", 3)] or whatever

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…

Python exchangelib在子文件夹中读取邮件 - python

我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。

Python ThreadPoolExecutor抑制异常 - python

from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED def div_zero(x): print('In div_zero') return x / 0 with ThreadPoolExecutor(max_workers=4) as execut…

如何用'-'解析字符串到节点js本地脚本? - python

我正在使用本地节点js脚本来处理字符串。我陷入了将'-'字符串解析为本地节点js脚本的问题。render.js:#! /usr/bin/env -S node -r esm let argv = require('yargs') .usage('$0 [string]') .argv; console.log(argv…