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

Python 按列表中匹配的任何第一项拆分文本,python,list,parsing,Python,List,Parsing,我正在寻找一种优雅的方法,从文本中的介词列表中找到第一个匹配项,这样我就可以解析像“在窗口后面添加鞋子”这样的文本,结果应该是[“鞋子”,“在窗口后面”] 只要文本中没有多个介词,它就可以工作 我的钥匙在窗口后面之前:我的钥匙在之后:在 窗口 我的钥匙在厨房桌子下面 桌子在厨房里 我的钥匙在厨房桌子下的盒子里 钥匙后:在厨房桌子下的盒子里 在第二个示例中,结果应该是[“我的钥匙”,“厨房桌子下面”] 找到列表中任何单词的第一个匹配项的优雅方法是什么 def get_text_after_prep

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

只要文本中没有多个介词,它就可以工作

我的钥匙在窗口后面之前:我的钥匙在之后:在 窗口

我的钥匙在厨房桌子下面 桌子在厨房里

我的钥匙在厨房桌子下的盒子里 钥匙:在厨房桌子下的盒子里

在第二个示例中,结果应该是[“我的钥匙”,“厨房桌子下面”]

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

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
def get_text_在位置(text)的介词_之后:
“”“返回[0]之前和[1]之后的文本。”“”
介词的位置=[“前面”、“后面”、“在”、“在”、“在”、“在”、“在”、“在”、“在”、“在”、“在”、“在”、“在”、“在”、“在”、“在”、“在”、“在”、“在”、“在”、“在”、“在”、“在”、“在”、“靠近”、“在旁边”]
textres=[“”,“”]
对于输入介词的位置:
如果textres[0]==“”:
如果输入文本:
textres[0]=text.split(键,1)[0].strip()
textres[1]=key+“”+text.split(key,1)[1].strip()
返回文本库
您可以使用以下方法执行此操作:

重新导入
def get_text_在_位置的介词_之后(文本):
“”“返回[0]之前和[1]之后的文本。”“”
介词的位置=[“前面”、“后面”、“在”、“在”、“在”、“在”、“在”、“在”、“在”、“在”、“在”、“在”、“在”、“在”、“在”、“在”、“在”、“在”、“在”、“在”、“在”、“在”、“在”、“在”、“靠近”、“在旁边”]
preps_re=re.compile(r'\b('+'|'.join(介词位置)+r')\b')
拆分=准备拆分(文本,maxsplit=1)
返回拆分[0],拆分[1]+拆分[2]
打印(在\u位置的\u介词\u(“桌上盒子里的猫”)后获取\u文本\u)
#(“猫”,“在桌子上的盒子里”)
首先,我们创建一个类似于
(in | on | under)
的正则表达式。注意括号:它们允许我们捕获拆分字符串的字符串,以便将它们保留在输出中


至此,我们分裂,允许最多1个分裂,并连接最后两个部分:介词和字符串的其余部分。

在正则表达式前后,你想添加<代码> \b>代码>,以确保你在和<>代码> <代码> >中间的单词。谢谢你。我编辑了答案!
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')