【问题标题】:Rearrange Text blocks such that each ends with a complete sentence重新排列文本块,使每个块都以完整的句子结尾
【发布时间】:2019-07-22 04:07:44
【问题描述】:

我有三组文本块(实际上更多...),它们显示了完整文本的一部分。然而,由于一些句子在两个文本块之间分割,原始文本的划分没有正确完成。

text1 = {"We will talk about data about model specification parameter \
estimation and model application and the context where we will apply \
the simple example.Is an application where we would like to analyze \
the market for electric cars because"};

text2 = {"we are interested in the market of electric cars.The choice \
that we are interested in is the choice of each individual to \
purchase an electric car or not And we will see how"};

text3 = {"to address this question. Furthermore, it needs to be noted that this is only a model text and there is no content associated with it. "};

例如text2 以“我们对电动汽车市场感兴趣”开头。这是一个不完整的第一句,实际上是从文本块 1 开始的(见最后一句)。

我想确保每个文本块都以完整的句子结尾。所以我想将不完整的第一句话移到最后一个文本块。例如这里的结果是:

 text1corr = {"We will talk about data about model specification parameter \
    estimation and model application and the context where we will apply \
    the simple example.Is an application where we would like to analyze \
    the market for electric cars because we are interested in the market of electric cars."};

text2corr = {"The choice that we are interested in is the choice of each individual to purchase an electric car or not And we will see how to address this question."};

text3corr = {"Furthermore, it needs to be noted that this is only a model text and there is no content associated with it. "};

如何在 Python 中做到这一点?这甚至可能吗?

【问题讨论】:

  • 句号是否表示句尾?
  • @paradox 确实如此。

标签: python string text


【解决方案1】:

您可以使用函数zip_longest() 来迭代字符串对:

from itertools import zip_longest
import re

l = [text1, text2, text3]
new_l = []

for i, j in zip_longest(l, l[1:], fillvalue=''):
    # remove leading and trailing spaces
    i, j = i.strip(), j.strip()
    # remove leading half sentence
    if i[0].islower():
        i = re.split(r'[.?!]', i, 1)[-1].lstrip()
    # append half sentence from next string
    if i[-1].isalpha():
        j = re.split(r'[.?!]', j, 1)[0]
        i = f"{i} {j}."
    new_l.append(i)

for i in new_l:
    print(i)

输出:

We will talk about data about model specification parameter estimation and model application and the context where we will apply the simple example.Is an application where we would like to analyze the market for electric cars because we are interested in the market of electric cars.
The choice that we are interested in is the choice of each individual to purchase an electric car or not And we will see how to address this question.
Furthermore, it needs to be noted that this is only a model text and there is no content associated with it.

【讨论】:

  • 非常感谢您的回答。 zip_longest 是做什么的?您介意在您的代码中添加一些 cmets 以获得总体思路吗?谢谢。
  • @james 欢迎您。 zip_longest 生成对:text1, text2; text2, text3; text3, ‘‘。我添加了一些 cmets。
【解决方案2】:
text1 = "We will talk about data about model specification parameter \
estimation and model application and the context where we will apply \
the simple example.Is an application where we would like to analyze \
the market for electric cars because"

text2 = "we are interested in the market of electric cars.The choice \
that we are interested in is the choice of each individual to \
purchase an electric car or not And we will see how"

text3 = "to address this question. Furthermore, it needs to be noted that this is only a model text and there is no content associated with it. "

textList = [text1,text2,text3]

corrected_list = []
prev_incomplete_sentece = ''
for index , text in enumerate(textList):
    if(len(prev_incomplete_sentece) > 0):
        corrected_text =  text[len(prev_incomplete_sentece) + 1:]
    else:
        corrected_text = text
    if(index +1 < len(textList)):
        corrected_text += ' '+ textList[index+1].split('.')[0]
        prev_incomplete_sentece = textList[index+1].split('.')[0]
    corrected_list.append(corrected_text)    

输出:

['We will talk about data about model specification parameter estimation and model application and the context where we will apply the simple example.Is an application where we would like to analyze the market for electric cars because we are interested in the market of electric cars',
 'The choice that we are interested in is the choice of each individual to purchase an electric car or not And we will see how to address this question',
 ' Furthermore, it needs to be noted that this is only a model text and there is no content associated with it. ']

【讨论】:

  • 它在“example”之后分割第一个块。请查看我的问题中的预期结果。不幸的是不一样。
  • 您要精确输出还是要根据完整句子拆分文本?
  • 我想要准确的输出。每个文本块都应该以一个完整的句子结尾。如果不是这样,那么句子的剩余部分应该取自下一个文本块。
  • 很好的答案!非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-09
  • 2022-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多