gpt4 book ai didi

python - 使用正则表达式来约束元组列表

转载 作者:太空宇宙 更新时间:2023-11-03 15:22:45 25 4
gpt4 key购买 nike

给定单词元组列表及其句子中的词性:

[('We', 'PRP'),
('took', 'VBD'),
('advantage', 'NN'),
('of', 'IN'),
('the', 'DT'),
('half', 'JJ'),
('price', 'NN'),
('sushi', 'NN'),
('deal', 'NN'),
('on', 'IN'),
('saturday', 'NN')]

我想使用正则表达式提取具有某些 PoS 序列的术语。这将类似于 ('JJ')*('NN')+ 所以我有一个 [('advantage', 'halfprice sushi deal', 'saturday') 列表]。考虑到我将执行数百个句子,执行此类任务的最合适方法是什么?

谢谢!

最佳答案

我认为这可能会起作用:

a = [('We', 'PRP'),
('took', 'VBD'),
('advantage', 'NN'),
('of', 'IN'),
('the', 'DT'),
('half', 'JJ'),
('price', 'NN'),
('sushi', 'NN'),
('deal', 'NN'),
('on', 'IN'),
('saturday', 'NN')]

b = iter(a[1:])

my_list = []
inner_list = []
accepted = ['JJ', 'NN']

for item in a:
word = item[0]
check = item[1]
try:
against = next(b)
if check in accepted:
if against[1] not in accepted:
inner_list.append(word)
my_list.append(inner_list)
inner_list = []
else:
inner_list.append(word)
except StopIteration:
if check in accepted:
inner_list.append(word)
my_list.append(inner_list)
final = [' '.join(item) for item in my_list]

关于python - 使用正则表达式来约束元组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43394735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com