NLP 获取单词最常见的 POS 标记,并在带有训练数据的字典中使用它
NLP Getting the most common POS tag for a word and using it in the dictionary with Training Data
我有一个具有以下格式(pos、word、tag)的训练文本文件:
1 个 PRP
2'd MD
3 个赞 VB
4 至
5 去 VB
6。 .
1 个 PRP
我正在尝试构建一个字典,以便当我输入具有以下格式(pos,单词)的新语料库时:
1 谁
2个知道
3 什么
4 将
5 次发生
6。
我将能够从我用训练数据构建的字典中标记这些。
我使用的方法是在默认词典中使用一个计数器来查找单词最常见的标记。从我的柜台,我得到这样的打印结果:
我的 PRP 7905
MD 1262
赞VB2706
喜欢VBP 201
喜欢 UH 95
喜欢 IN 112
至 4822
至 IN 922
所以对于单词 "like",计数最高的标签是 'VB',位于 2706。我想让我的字典把计数最高的标签附加到我的单词上,所以如果我将测试数据集与 (pos, word) 放在一起,它将 return 该标签。到目前为止,这是我的代码:
file=open("/Users/Desktop/training.txt").read().split('\n')
from collections import Counter, defaultdict
word_tag_counts = defaultdict(Counter)
for row in file:
if not row.strip():
continue
pos, word, tag = row.split()
word_tag_counts[word.lower()][tag] += 1
stats = word_tag_counts
max(stats, key=stats.get)
with open('/Users/Desktop/training.txt','r') as file:
for line in file.readlines():
column = line.split('\t')
with open('/Users/Desktop/output.txt','w') as file:
for tag, num in d.items():
file.write("\t".join([column[0], column[1], tag])+"\n")
我收到错误:TypeError: '>' 在 'Counter' 和 'Counter'
的实例之间不受支持
我的输出目标与原始训练文件的格式相同(从原始 txt 文件提取 pos,从原始 txt 文件提取单词,从我的词典中提取标签):
不确定我能做什么,我也尝试使用 lambda,但它不起作用。任何事情都会有所帮助。谢谢。
如果我理解正确,你现在想要实现的是 dict
将每个单词的小写形式映射到其最常见的词性标记。在 stats
你有多少次每个词的每个 POS 标记出现在训练数据中,存储在 Counter
.
第max(stats, key=stats.get)
行是你做错的地方。 stats.get(word)
returns 与单词 word
相关的 Counter
和 Counter
s 在 Python 3 中不可比较(但是,它们在Python 2,但实际上没有意义)。更重要的是,即使Counter
是可比的,max
函数也只是return最大Counter
的词,这不是你想要的。
我们要做的就是使用Counter
s的most_common()
方法。对于每个单词 word
、get()
它的 Counter
(我们将其命名为 c
)并调用 c.most_common(1)[0][0]
以获取其最频繁的 POS 标签。我们需要下标 [0][0]
的原因是 most_common(k)
return 是 top-k
频繁项的列表,并且对于每个这样的项,它 return 是一个元组包含项目本身及其频率。所以代码看起来像这样:
pos_tags = {word: stats[word].most_common(1)[0][0] for word in stats}
而pos_tags
是您想要的映射。您需要做的就是完成其余代码(将此 POS 标记方法应用于其他文件)。
我有一个具有以下格式(pos、word、tag)的训练文本文件:
1 个 PRP
2'd MD
3 个赞 VB
4 至
5 去 VB
6。 .
1 个 PRP
我正在尝试构建一个字典,以便当我输入具有以下格式(pos,单词)的新语料库时:
1 谁
2个知道
3 什么
4 将
5 次发生
6。
我将能够从我用训练数据构建的字典中标记这些。
我使用的方法是在默认词典中使用一个计数器来查找单词最常见的标记。从我的柜台,我得到这样的打印结果:
我的 PRP 7905
MD 1262
赞VB2706
喜欢VBP 201
喜欢 UH 95
喜欢 IN 112
至 4822
至 IN 922
所以对于单词 "like",计数最高的标签是 'VB',位于 2706。我想让我的字典把计数最高的标签附加到我的单词上,所以如果我将测试数据集与 (pos, word) 放在一起,它将 return 该标签。到目前为止,这是我的代码:
file=open("/Users/Desktop/training.txt").read().split('\n')
from collections import Counter, defaultdict
word_tag_counts = defaultdict(Counter)
for row in file:
if not row.strip():
continue
pos, word, tag = row.split()
word_tag_counts[word.lower()][tag] += 1
stats = word_tag_counts
max(stats, key=stats.get)
with open('/Users/Desktop/training.txt','r') as file:
for line in file.readlines():
column = line.split('\t')
with open('/Users/Desktop/output.txt','w') as file:
for tag, num in d.items():
file.write("\t".join([column[0], column[1], tag])+"\n")
我收到错误:TypeError: '>' 在 'Counter' 和 'Counter'
的实例之间不受支持我的输出目标与原始训练文件的格式相同(从原始 txt 文件提取 pos,从原始 txt 文件提取单词,从我的词典中提取标签):
不确定我能做什么,我也尝试使用 lambda,但它不起作用。任何事情都会有所帮助。谢谢。
如果我理解正确,你现在想要实现的是 dict
将每个单词的小写形式映射到其最常见的词性标记。在 stats
你有多少次每个词的每个 POS 标记出现在训练数据中,存储在 Counter
.
第max(stats, key=stats.get)
行是你做错的地方。 stats.get(word)
returns 与单词 word
相关的 Counter
和 Counter
s 在 Python 3 中不可比较(但是,它们在Python 2,但实际上没有意义)。更重要的是,即使Counter
是可比的,max
函数也只是return最大Counter
的词,这不是你想要的。
我们要做的就是使用Counter
s的most_common()
方法。对于每个单词 word
、get()
它的 Counter
(我们将其命名为 c
)并调用 c.most_common(1)[0][0]
以获取其最频繁的 POS 标签。我们需要下标 [0][0]
的原因是 most_common(k)
return 是 top-k
频繁项的列表,并且对于每个这样的项,它 return 是一个元组包含项目本身及其频率。所以代码看起来像这样:
pos_tags = {word: stats[word].most_common(1)[0][0] for word in stats}
而pos_tags
是您想要的映射。您需要做的就是完成其余代码(将此 POS 标记方法应用于其他文件)。