【发布时间】:2020-12-22 13:12:03
【问题描述】:
我正在尝试编写一个代码,该代码将创建一个包含键(单词)、值(单词出现的 POS 标记 + 相应计数)的字典。 最终目标是了解给定单词最常用的 POS 标签
一个例子:
most_common({"NOUN": 2, "DET": 5, "ADP": 1 }) returns "DET" 因为给定的单词作为限定词出现频率最高。
首先,我想在一个带注释的小型语料库上训练我的代码。这是我目前所拥有的:
import pprint
trainfile = open("small_train.connlu")
list_of_lists = []
for line in trainfile:
stripped_line = line.strip()
line_list = stripped_line.split()
list_of_lists.append(line_list)
list_keys = [] #a list that will contain all the keys (including duplicates)
list_values = [] #a list that will contain all the values
for line in list_of_lists:
if line == []:
pass
elif line != []:
list_keys.append(line[1]) # second column of the file contains all words
list_values.append(line[3]) # fourth column of the file contains all POS tags (see below)
list_keys = [key.lower() for key in list_keys] #lowercase all keys - 'The' and 'the' should be assigned the same POS
我被困在这一点上。我现在需要创建一个字典,其中包含出现在语料库中的所有单词,然后是它们出现的相应 POS 标签(以及每个单词与某个 POS 标签出现的次数)。这是我得到的最接近的:
dict = {}
for key in range(len(list_keys)):
dict[list_keys[key]] = list_values[key]
pprint.pprint(dict)
这会返回带有正确 POS 标签的键,但是,我不知道如何实现计数。我尝试过的任何事情都会导致错误。
这是训练数据的格式 (small_train.connlu)
1 The _ DET _ _ _ _ _ _
2 hottest _ ADJ _ _ _ _ _ _
3 item _ NOUN _ _ _ _ _ _
4 on _ ADP _ _ _ _ _ _
5 Christmas _ PROPN _ _ _ _ _ _
6 wish _ NOUN _ _ _ _ _ _
7 lists _ NOUN _ _ _ _ _ _
8 this _ DET _ _ _ _ _ _
9 year _ NOUN _ _ _ _ _ _
10 is _ AUX _ _ _ _ _ _
11 nuclear _ ADJ _ _ _ _ _ _
12 weapons _ NOUN _ _ _ _ _ _
13 . _ PUNCT _ _ _ _ _ _
1 I _ PRON _ _ _ _ _ _
2 wish _ VERB _ _ _ _ _ _
3 you _ PRON _ _ _ _ _ _
4 all _ DET _ _ _ _ _ _
5 of _ ADP _ _ _ _ _ _
6 the _ DET _ _ _ _ _ _
7 best _ ADJ _ _ _ _ _ _
如果有人能提供帮助,我将不胜感激。非常感谢:)
【问题讨论】:
-
您的预期输出究竟是什么?是
{'the': 'DET', 'hottest': 'ADJ' ...},然后您想要其他一些带有计数和其他聚合的ke,还是只是计数而没有确切的词?
标签: python python-3.x dictionary nlp pos-tagger