我正在研究名称实体识别(NER),以识别文本的一些标签。
我想使用nltk
,问题是我有这种格式的数据(元组列表的列表),基本上看起来像这样(4个示例):
df[0:5]:
[[('Appendix', 'None'), ('B', 'None')],
[('On', 'None'),
('the', 'None'),
('Table', 'None'),
('of', 'None'),
('Oppositions', 'None'),
('in', 'None'),
('Chapter', 'None'),
('15', 'NUM')],
[('by', 'None'),
('Yaakov', 'None'),
('Zik', 'None'),
('Table', 'None'),
('i', 'None')],
[('Initial', 'None'),
('positions', 'None'),
('of', 'None'),
('Mars', 'None'),
('in', 'None'),
('Chapter 15 ', 'None'),
('computed', 'None'),
('with', 'None'),
('Guide 9 ', 'None'),
('using', 'None'),
('JPL', 'GEOM'),
('DE430', 'GEOM')],
[('General', 'None'), ('notes', 'None')]]
我想在不更改数据结构的情况下将其添加到每个元组pos_tag。
所需的结果应该是这样
[[('Appendix','CS', 'None'), ('B', 'NC', 'None')],
[('On', 'NC', 'None'),
('the', 'NC', 'None'),
('Table', 'NC', 'None'),
('of', 'Fp' 'None'),
('Oppositions','Fp', 'None'),
('in', 'Fp' 'None'),
('Chapter', 'Fp', 'None'),
('15', 'Fp', 'NUM')],
[('by', 'None'),
('Yaakov', 'Fp', 'None'),
('Zik', 'None'),
('Table', 'Fp', 'None'),
('i', 'Fp', 'None')],
[('Initial', 'Fp', 'None'),
('positions', 'Fp', 'None'),
('of', 'Fp', 'None'),
('Mars', 'Fp', 'None'),
('in', 'Fp', 'None'),
('Chapter 15 ', 'Z', 'None'),
('computed', 'Fp', 'None'),
('with', 'Fp', 'None'),
('Guide 9 ', 'Fp', 'None'),
('using', 'None'),
('JPL', 'Fp', 'GEOM'),
('DE430', 'Fp', 'GEOM')],
[('General', 'Z', 'None'), ('notes', ''Fp' 'None')]]
正如给出的,我想通过nltk.pos_tag(sent)在每个元组中添加pos-tag
一般来说,如何将组件添加到元组列表列表中,而结果又是相同的元组列表列表?
答案 0 :(得分:1)
您的问题有点含糊,但是根据我的理解,这是一个快速的解决方案。假设您想保持订单完整,并想在元组的位置插入项目:
to_add = '*' # Replace this value with the actual data you want to insert such as pos_tag
position_to_add = 1 # Replace this value with the actual position to insert into
result = []
for lst in df:
ret_li = []
for tpl in lst:
# new_tpl = [*tpl]
# new_tpl.append('None')
new_tpl = tuple([*tpl[0:position_to_add]] + [to_add] + [*tpl[position_to_add:]])
ret_li.append(new_tpl)
result.append(ret_li)