从解析的pdf中加入句子

时间:2018-05-14 19:26:50

标签: python join indexing pdf-parsing

我从pdf中删除了一些文本,我已经解析了文本,并且当前在列表中将所有内容都作为字符串。我想将由于pdf页面上的中断而作为单独字符串返回的句子连接在一起。例如,

list = ['I am a ', 'sentence.', 'Please join me toge-', 'ther. Thanks for your help.'] 

我想:

list = ['I am a sentence.', 'Please join me together. Thanks for your help.'] 

我目前有以下代码连接一些句子,但仍然返回加入第一个句子的第二个子句。我知道这是由于索引,但我不知道如何解决这个问题。

new = []

def cleanlist(dictlist):
    for i in range(len(dictlist)):

    if i>0:

        if dictlist[i-1][-1:] != ('.') or dictlist[i-1][-1:] != ('. '):
            new.append(dictlist[i-1]+dictlist[i])

        elif dictlist[i-1][-1:] == '-':
            new.append(dictlist[i-1]+dictlist[i])

        else:
            new.append[dict_list[i]] 

1 个答案:

答案 0 :(得分:1)

您可以使用生成器方法:

def cleanlist(dictlist):
    current = []
    for line in dictlist:
        if line.endswith("-"):
            current.append(line[:-1])
        elif line.endswith(" "):
            current.append(line)
        else:
            current.append(line)
            yield "".join(current)
            current = []

像这样使用:

dictlist = ['I am a ', 'sentence.', 'Please join me toge-', 'ther. Thanks for your help.']
print(list(cleanlist(dictlist)))
# ['I am a sentence.', 'Please join me together. Thanks for your help.']