我正在尝试使用conllu库创建一个CoNLL-U文件,作为我正在进行的通用依赖项标记项目的一部分
我在python列表中有许多句子。其中包含令牌、lemmata、POS标签、功能等的子列表。例如:
sentence = [['The', 'the', 'DET', ... ], ['big', big', 'ADJ', ... ], ['dog', 'dog', 'NOUN', ...], ...]
我想将这些转换成CoNLL-U解析句子的过程自动化,因此我编写了以下函数:
from collections import OrderedDict
def compile_sent(sent):
sent_list = list()
for i, tok_data in enumerate(sent):
tok_id = i + 1
tok = tok_data[0]
lemma = tok_data[1]
pos = tok_data[2]
feats = tok_data[3]
compiled_tok = OrderedDict({'id': tok_id, 'form': tok, 'lemma': lemma, 'upostag': pos, 'xpostag': None, 'feats': feats, 'head': None, 'deprel': None, 'deps': None, 'misc': None})
sent_list.append(compiled_tok)
sent_list = sent_list.serialize()
return sent_list
print(compile_sent(sentence))
当我尝试运行此代码时,出现以下错误:
Traceback (most recent call last):
File "/Users/me/PycharmProjects/UDParser/Rough_Work.py", line 103, in <module>
print(compile_sent(sentence))
File "/Users/me/PycharmProjects/UDParser/Rough_Work.py", line 99, in compile_sent
sent_list = sent_list.serialize()
AttributeError: 'list' object has no attribute 'serialize'
问题是,我试图创建一个普通列表,并在该列表上运行serialize()
方法。我不知道如何创建TokenList
类型,当parse()
函数在CoNLL-U文件格式的字符串上运行时,库会创建该类型
当您尝试打印该类型的列表时,会得到以下输出:
data = """
# text = The big dog
1 The the DET _ Definite=Def|PronType=Art _ _ _ _
2 big big ADJ _ Degree=Pos _ _ _ _
3 dog dog NOUN _ Number=Sing _ _ _ _
"""
sentences = data.parse()
sentence = sentences[0]
print(sentence)
TokenList<The, quick, brown, fox, jumps, over, the, lazy, dog, .>
在这种类型的列表上运行serialize()
方法将把它转换回CoNLL-U格式的字符串,如上面示例中的data
。但是,当您尝试在普通python列表上运行它时,它会中断
如何创建这样的TokenList
而不是普通的python列表对象
将
sent_list
从普通列表更改为TokenList
通过在REPL中使用
help(TokenList)
,可以查看TokenList
上的函数相关问题 更多 >
编程相关推荐