gpt4 book ai didi

python - 按列表中匹配的任何第一项拆分文本

转载 作者:太空宇宙 更新时间:2023-11-04 00:11:42 24 4
gpt4 key购买 nike

我正在寻找一种优雅的方式来从文本中的介词列表中找到第一个匹配项,以便我可以解析像“在 window 后面添加鞋子”这样的文本,结果应该是 ["shoes","behind窗口"]

只要文本中没有多个介词,它就有效

my keys behind the window before: my keys after: behind the window

my keys under the table in the kitchen before: my keys under the table after: in the kitchen

my keys in the box under the table in the kitchen before: my keys after: in the box under the table in the kitchen

在第二个例子中,结果应该是["my keys","under the table in the kitchen"]

找到列表中任何单词的第一个匹配项的优雅方法是什么?

def get_text_after_preposition_of_place(text):
"""Returns the texts before[0] and after[1] <preposition of place>"""

prepositions_of_place = ["in front of","behind","in","on","under","near","next to","between","below","above","close to","beside"]
textres = ["",""]

for key in prepositions_of_place:
if textres[0] == "":
if key in text:
textres[0] = text.split(key, 1)[0].strip()
textres[1] = key + " " + text.split(key, 1)[1].strip()
return textres

最佳答案

您可以使用 re.split 来做到这一点:

import re

def get_text_after_preposition_of_place(text):
"""Returns the texts before[0] and after[1] <preposition of place>"""

prepositions_of_place = ["in front of","behind","in","on","under","near","next to","between","below","above","close to","beside"]
preps_re = re.compile(r'\b(' + '|'.join(prepositions_of_place) + r')\b')

split = preps_re.split(text, maxsplit=1)
return split[0], split[1]+split[2]

print(get_text_after_preposition_of_place('The cat in the box on the table'))
# ('The cat ', 'in the box on the table')

首先,我们创建一个看起来像(in|on|under) 的正则表达式。请注意括号:它们将允许我们捕获我们拆分字符串的字符串,以便将它们保留在输出中。

然后,我们拆分,最多允许拆分 1 次,并连接最后两部分:介词和字符串的其余部分。

关于python - 按列表中匹配的任何第一项拆分文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52343181/

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