Python中多词典POS恶搞

问题描述 投票:2回答:1

我有POS标记的文本文件。例如:

“DT的NN狗VB跳......”

我需要创建一个字典,其中条目的键字和值是另一个字典的标签为键和标签作为值的频率。因此,我需要是这样的:

{ '的':{ 'DT':47}},{ '狗':{ 'VB':32}} ...

我在一个总损失是现在。我已经站在我的文本文件,并将其分成与字符串列表,所以,它就像一个列表开始

“DT上的”'NN狗“VB跳”

我不知道这是否是即使正确的第一步,还是什么。请帮忙!

python python-3.x nlp pos-tagger
1个回答
0
投票

这种方法应该给你你要寻找的结构,与POS计数是提出的躯体内该标签的完整计数。

注:RETAIN_PUNCTUATION_FLAGRETAIN_CASE_FLAG让你评价之前的行为切换要么带标点符号,使案件均匀或保留上/下壳,或者干脆两者都做。在这里,他们都分配False,所有的话会为小写来处理,并且所有ASCII标点符号将评估之前被剥离。

我已经添加word_listpos_list替代上市。

from string import punctuation

RETAIN_PUNCTUATION_FLAG = False
RETAIN_CASE_FLAG = False

string = "DT The NN dog VB jumps DT the NN sofa. DT The NN cat VB pages DT the NN page."

punctuation_strip_table = str.maketrans('', '', punctuation)
if RETAIN_CASE_FLAG and RETAIN_PUNCTUATION_FLAG:
    pass
elif RETAIN_CASE_FLAG and not RETAIN_PUNCTUATION_FLAG:
    string = string.translate(punctuation_strip_table)
elif not RETAIN_CASE_FLAG and RETAIN_PUNCTUATION_FLAG:
    string = string.casefold()
elif not RETAIN_CASE_FLAG and not RETAIN_PUNCTUATION_FLAG:
    string = string.casefold().translate(punctuation_strip_table)

list_all = string.split(' ')
pos_word_pairs = set(zip(
            list_all[0:][::2],
            list_all[1:][::2]))

pos_list = {pos.upper(): {
    'count': list_all.count(pos),
    'words': [
        word
        for match_pos, word in pos_word_pairs
        if match_pos == pos]
    }
    for pos in set(list_all[0:][::2])}
word_list = {word: {
    'count': list_all.count(word),
    'pos': [
        pos.upper()
        for pos, match_word in pos_word_pairs
        if match_word == word]
    }
    for word in set(list_all[1:][::2])}
paired = {
        word: {
            pos.upper():
            list_all.count(pos)}
        for pos, word
        in pos_word_pairs}

print('pos_list:', pos_list)
print()
print('word_list:', word_list)
print()
print('paired:',paired)

输出:

pos_list: {'VB': {'count': 2, 'words': ['pages', 'jumps']}, 'NN': {'count': 4, 'words': ['page', 'dog', 'sofa', 'cat']}, 'DT': {'count': 4, 'words': ['the']}}

word_list: {'dog': {'count': 1, 'pos': ['NN']}, 'cat': {'count': 1, 'pos': ['NN']}, 'jumps': {'count': 1, 'pos': ['VB']}, 'the': {'count': 4, 'pos': ['DT']}, 'page': {'count': 1, 'pos': ['NN']}, 'sofa': {'count': 1, 'pos': ['NN']}, 'pages': {'count': 1, 'pos': ['VB']}}

paired: {'pages': {'VB': 2}, 'jumps': {'VB': 2}, 'the': {'DT': 4}, 'page': {'NN': 4}, 'dog': {'NN': 4}, 'sofa': {'NN': 4}, 'cat': {'NN': 4}}
© www.soinside.com 2019 - 2024. All rights reserved.