Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用conllu库创建令牌列表?_Python_Nlp_Conll_Universal Pos Tag - Fatal编程技术网

Python 如何使用conllu库创建令牌列表?

Python 如何使用conllu库创建令牌列表?,python,nlp,conll,universal-pos-tag,Python,Nlp,Conll,Universal Pos Tag,我正在尝试使用conllu库创建一个CoNLL-U文件,作为我正在进行的通用依赖项标记项目的一部分 我在python列表中有许多句子。其中包含令牌、lemmata、POS标签、功能等的子列表。例如: sentence = [['The', 'the', 'DET', ... ], ['big', big', 'ADJ', ... ], ['dog', 'dog', 'NOUN', ...], ...] 我想将这些转换成CoNLL-U解析句子的过程自动化,因此我编写了以下函数: from col

我正在尝试使用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()
方法会将其转换回CoNLL-U格式字符串,如上例中的
data
。但是,当您尝试在普通python列表上运行它时,它会中断


如何创建这样的
TokenList
而不是普通的python列表对象?

将您的
sent\u列表从普通列表更改为
TokenList

from conllu import TokenList
from collections import OrderedDict

def compile_sent(sent):
    sent_list = TokenList()
    # ... etc ...
您可以使用REPL中的
help(TokenList)
查看
TokenList
上的函数

from conllu import TokenList
from collections import OrderedDict

def compile_sent(sent):
    sent_list = TokenList()
    # ... etc ...