平平无奇哈斯特 2024-03-24 23:56 采纳率: 0%
浏览 6
已结题

对于具有相似性但不具有可确定性的文本的文本清理

一篇文章中,我要去除的不只是Figure x.y,而是包括了它的上下连接词,比如Figure 4.34 shows, as shown in Figure 4.45,In Figure 4.55,Figure 6.2 summarizes等等,这类句子又是一个意思,但我又不知道总共有多少类,直接使用正则表达式不太方便。在python中这种需要如何处理,问问各位编程大师🥺

  • 写回答

3条回答 默认 最新

  • 码农阿豪@新空间代码工作室 Java领域优质创作者 2024-03-24 23:56
    关注
    让阿豪来帮你解答,本回答参考chatgpt3.5编写提供,如果还有疑问可以评论或留言
    问题描述: 如何去除一篇文章中所有包含"Figure x.y"以及其上下连接词的句子? 解决方案:
    1. 定义正则表达式 首先需要定义一个正则表达式,能够匹配到包含"Figure x.y"的句子以及其上下连接词。可以使用如下正则表达式:
    import re
    figure_pattern = re.compile(r'(.*[^\w])?Figure\s\d(\.\d+)?([^\w].*)?')
    

    该正则表达式匹配的模式为: 1. 匹配以Figure开头的字符串 2. 中间可能包含一个小数(例如:6.25) 3. 匹配结束后可能包含一个非字母的字符 该正则表达式匹配的字符串示例: 1. "Figure 4.2 shows" 2. " as shown in Figure 6.5, " 3. "In Figure 2.1:" 2. 去除所有匹配到的句子 可以使用如下代码,遍历文章中的每一个句子,判断该句子是否匹配到了上述正则表达式。如果匹配到了,则将该句子从文章中去除。

    def remove_figure_sentences(article: str) -> str:
        figure_pattern = re.compile(r'(.*[^\w])?Figure\s\d(\.\d+)?([^\w].*)?')
        sentences = re.split(r'(?<=[^A-Z].[.?]) +(?=[A-Z])', article)
        cleaned_article = ""
        for sentence in sentences:
            if not bool(figure_pattern.match(sentence)):
                cleaned_article += sentence + " "
        return cleaned_article
    

    该函数使用了正则表达式将文章拆分成一个个句子,并遍历每个句子,判断句子是否匹配到了上述正则表达式。如果没有匹配到,则该句子是需要保留的,将其添加到新的文章中。 案例: 原文章:

    Figure 6.2 summarizes the study design. As shown in Figure 4.3, the results indicate that... In Figure 4.7, we can see... Figure 3.1 presents a comparison between... However, in Figure 2.2, we see a different pattern. Figure 5.5 shows... In Figure 6.8, the changes in... 
    

    清理后的文章:

    summarizes the study design. The results indicate that... we can see... presents a comparison between... we see a different pattern. shows... the changes in... 
    

    该清理函数去除了原文章中所有包含"Figure x.y"以及其上下连接词的句子,保留了文中其他的内容。

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 3月27日
  • 创建了问题 3月24日

悬赏问题

  • ¥20 WPF MVVM模式 handycontrol 框架, hc:SearchBar 控件 Text="{Binding NavMenusKeyWords}" 绑定取不到值
  • ¥15 需要手写数字信号处理Dsp三个简单题 不用太复杂
  • ¥15 数字信号处理考试111
  • ¥100 关于#audobe audition#的问题,如何解决?
  • ¥15 allegro17.2生成bom表是空白的
  • ¥15 请问一下怎么打通CAN通讯
  • ¥20 如何在 rocky9.4 部署 CDH6.3.2?
  • ¥35 navicat将excel中的数据导入mysql出错
  • ¥15 rt-thread线程切换的问题
  • ¥15 高通uboot 打印ubi init err 22