Python 将字符串与文本进行比较以在正确的位置设置标点符号

Python 将字符串与文本进行比较以在正确的位置设置标点符号,python,python-3.x,string,slice,Python,Python 3.x,String,Slice,因此,本文中有一个文本和短语,我们需要将标点符号匹配到: text = 'i like plums, apples, and donuts. if i had a donut, i would eat it' phrases = [['apples and donuts'], ['a donut i would']] 我需要的输出是: output = [['apples, and donuts'], ['a donut, i would']] 我是一名初学者,所以我考虑使用.replace

因此,本文中有一个文本和短语,我们需要将标点符号匹配到:

text = 'i like plums, apples, and donuts. if i had a donut, i would eat it'
phrases = [['apples and donuts'], ['a donut i would']]
我需要的输出是:

output = [['apples, and donuts'], ['a donut, i would']]

我是一名初学者,所以我考虑使用.replace(),但我不知道如何切分字符串并从文本中获取所需的确切部分。你能帮我吗?(我不允许使用任何库)

你可以试试正则表达式

import re

text = 'i like plums, apples, and donuts. if i had a donut, i would eat it'
phrases = [['apples and donuts'], ['a donut i would']]
print([re.findall(i[0].replace(" ", r"\W*"), text) for i in phrases])
输出

[['apples, and donuts'], ['a donut, i would']]

通过迭代
短语
列表并用
\W*
替换空格,regex
findall
方法将能够检测搜索词并忽略标点符号。

您可以尝试使用regex

import re

text = 'i like plums, apples, and donuts. if i had a donut, i would eat it'
phrases = [['apples and donuts'], ['a donut i would']]
print([re.findall(i[0].replace(" ", r"\W*"), text) for i in phrases])
输出

[['apples, and donuts'], ['a donut, i would']]

通过迭代
短语
列表并用
\W*
替换空格,regex
findall
方法将能够检测搜索词并忽略标点。

您可以删除文本中的所有标点,然后只使用普通子字符串搜索。那么,您唯一的问题就是如何将找到的文本还原或映射到原始文本

您可以通过记住在搜索文本中保留的每个字母在文本中的原始位置来实现。这里有一个例子。我只是删除了每个短语周围的嵌套列表,因为它看起来毫无用处,如果需要,您可以很容易地解释它

from pprint import pprint

text = 'i like plums, apples, and donuts. if i had a donut, i would eat it'
phrases = ['apples and donuts', 'a donut i would']

def find_phrase(text, phrases):
    clean_text, indices = prepare_text(text)
    res = []
    for phr in phrases:
        i = clean_text.find(phr)
        if i != -1:
            res.append(text[indices[i] : indices[i+len(phr)-1]+1])

    return res

def prepare_text(text, punctuation='.,;!?'):
    s = ''
    ind = []
    for i in range(len(text)):
        if text[i] not in punctuation:
            s += text[i]
            ind.append(i)
    return s, ind

if __name__ == "__main__":
    pprint(find_phrase(text, phrases))

['apples,and donuts.','a donuts,i would']

您可以删除文本中的所有标点符号,然后只使用简单的子字符串搜索。那么,您唯一的问题就是如何将找到的文本还原或映射到原始文本

您可以通过记住在搜索文本中保留的每个字母在文本中的原始位置来实现。这里有一个例子。我只是删除了每个短语周围的嵌套列表,因为它看起来毫无用处,如果需要,您可以很容易地解释它

from pprint import pprint

text = 'i like plums, apples, and donuts. if i had a donut, i would eat it'
phrases = ['apples and donuts', 'a donut i would']

def find_phrase(text, phrases):
    clean_text, indices = prepare_text(text)
    res = []
    for phr in phrases:
        i = clean_text.find(phr)
        if i != -1:
            res.append(text[indices[i] : indices[i+len(phr)-1]+1])

    return res

def prepare_text(text, punctuation='.,;!?'):
    s = ''
    ind = []
    for i in range(len(text)):
        if text[i] not in punctuation:
            s += text[i]
            ind.append(i)
    return s, ind

if __name__ == "__main__":
    pprint(find_phrase(text, phrases))

[“苹果和甜甜圈。”,“我要一个甜甜圈”]

似乎是一个有趣的家庭作业问题。到目前为止,你在这个问题上的企图是什么?你在做什么和/或理解什么方面有困难?@costaparas到目前为止,我正在尝试创建一个字典:'apples':'apples',但当我做text.split('')时,它也会删除所有空格,所以匹配后,我会得到'apples,and donuts'。这就是为什么我认为这可能是一个相当简单的方法,或者我只是不理解一些东西,你可以发布你现有的代码,以获得帮助。否则,你可以在下面找到答案。这似乎是一个有趣的家庭作业问题。到目前为止,你在这个问题上的企图是什么?你在做什么和/或理解什么方面有困难?@costaparas到目前为止,我正在尝试创建一个字典:'apples':'apples',但当我做text.split('')时,它也会删除所有空格,所以匹配后,我会得到'apples,and donuts'。这就是为什么我认为这可能是一个相当简单的方法,或者我只是不理解一些东西,你可以发布你现有的代码,以获得帮助。否则,您可以在下面查看答案。