1

简单示例:功能性-> 功能性

故事是我得到了一个 Microsoft Word 文档,它是从 PDF 格式转换而来的,并且有些单词仍然带有连字符(例如func-tional,由于 PDF 中的换行而损坏)。我想在保留正常单词的同时恢复那些破碎的单词(即“-”不用于断字)。

为了更清楚,添加了一个长示例(源文本):

研讨会结束后,基金会和 FCF 指导团队继续他们的工作,并创建了功能检查飞行纲要。本纲要包含可用于降低功能检查飞行风险的信息。指导文件中包含的信息是通用的,可能需要调整以适用于您的特定飞机。如果对纲要中的任何信息有疑问,请联系您的制造商以获得进一步的指导。

有人可以就这个问题给我一些建议吗?

4

1 回答 1

2

我会使用正则表达式。这个小脚本搜索带有连字符的单词并将连字符替换为空。

import re


def replaceHyphenated(s):
    matchList = re.findall(r"\w+-\w+",s) # find combination of word-word 
    sOut = s
    for m in matchList:
        new = m.replace("-","")
        sOut = sOut.replace(m,new)
    return sOut



if __name__ == "__main__":

    s = """After the symposium, the Foundation and the FCF steering team continued their work and created the Func-tional Check Flight Compendium. This compendium contains information that can be used to reduce the risk of functional check flights. The information contained in the guidance document is generic, and may need to be adjusted to apply to your specific aircraft. If there are questions on any of the information in the compendi-um, contact your manufacturer for further guidance."""    
    print(replaceHyphenated(s))

输出将是:

研讨会结束后,基金会和 FCF 指导团队继续他们的工作,并创建了功能检查飞行纲要。本纲要包含可用于降低功能检查飞行风险的信息。指南文件中包含的信息是通用的,可能需要进行调整以适用于您的特定飞机。如果对纲要中的任何信息有疑问,请联系您的制造商以获得进一步的指导。

如果你不习惯 RegExp,我推荐这个网站: https ://regex101.com/

于 2018-09-03T08:47:43.087 回答