[英]How to create a TokenList using the conllu library?
I'm trying to create a CoNLL-U file using the conllu library as part of a Universal Dependency tagging project I'm working on.我正在尝试使用 conllu 库创建一个 CoNLL-U 文件,作为我正在处理的通用依赖标记项目的一部分。
I have a number of sentences in python lists.我在 python 列表中有很多句子。 These contain sub-lists of tokens, lemmata, POS tags, features, etc. For example:这些包含令牌、词条、POS 标签、特征等的子列表。例如:
sentence = [['The', 'the', 'DET', ... ], ['big', big', 'ADJ', ... ], ['dog', 'dog', 'NOUN', ...], ...]
I want to automate the process of turning these into CoNLL-U parsed sentences, so I wrote the following function:我想自动化将这些转换为 CoNLL-U 解析句子的过程,所以我写了以下 function:
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))
When I try to run this code I get the following error:当我尝试运行此代码时,出现以下错误:
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'
The problem is that I'm trying to create a normal list and run the serialize()
method on that.问题是我正在尝试创建一个普通列表并在其上运行serialize()
方法。 I don't know how to create the type of TokenList
that is created by the library when the parse()
function is run on string in the CoNLL-U file format.当parse()
function 在 CoNLL-U 文件格式的字符串上运行时,我不知道如何创建库创建的TokenList
类型。
When you try to print that type of list you get the following output:当您尝试打印该类型的列表时,您会得到以下 output:
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, .>
Running the serialize()
method on this type of list will turn it back into a CoNLL-U format string like data
in the example above.在这种类型的列表上运行serialize()
方法会将其重新转换为 CoNLL-U 格式的字符串,如上例中的data
。 However, it breaks when you try to run it on a normal python list.但是,当您尝试在正常的 python 列表上运行它时,它会中断。
How can I create a TokenList
like this instead of a normal python list object?如何创建这样的TokenList
而不是普通的 python 列表 object?
Change your sent_list
from a normal list to a TokenList
.将您的sent_list
从普通列表更改为TokenList
。
from conllu import TokenList
from collections import OrderedDict
def compile_sent(sent):
sent_list = TokenList()
# ... etc ...
You can view the functions on TokenList
by using help(TokenList)
in a REPL.您可以在 REPL 中使用help(TokenList)
查看TokenList
上的函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.