【问题标题】:Using findall regex expression method python使用findall正则表达式方法python
【发布时间】:2018-04-28 20:39:23
【问题描述】:

我正在使用正则表达式 python 模块从法律文件中查找所有关键短语。其中之一低于 5 U.S.C. § 8452(a) 但它只打印并找到句子并在第一个句点处停止;所以不是我的输出读数:

委员会根据 5 U.S.C. 对此上诉拥有管辖权。 § 8452(a)

,它读作

委员会根据 5 U 对此上诉拥有管辖权。

相反。这是我的代码

  ruling_corpora  = map(lambda x: x[0], re.findall('([^.]*?(I find|In sum|agree|affirm|disagree|I conclude|In light of| under| this appeal| The ALJ| I determine| we| based on| for the reasons| pursuant to| the decision is| jurisidiction|section|§+\d |conclude)[^.]*\.)', tokenized, re.I | re.DOTALL | re.M))

    reduce = 0
    for r in ruling_corpora:#*
      reduce -=5
      big_list=[]
      big_list.extend(ruling_corpora)
      rc_list=[]
      rc_list.append(set(r))
      big_string= "".join(str(x)for x in  big_list)
      if len(big_string.split('.'))<= 3:
        while len(big_string.split())<=200:
          print("Ruling Content: {} \n".format(big_string))
          break
        break
    else:                                  
      summary=summarize(big_string,word_count=250+reduce)
      print("Summarized Ruling: {}\n".format(summary))
      break
   break

【问题讨论】:

  • 您的正则表达式是这样编写的,它在一个点处停止匹配。显然,这不是您希望它停止的地方。那么您希望它在哪里 停止?
  • 我希望它匹配到句子的结尾。然而,在某些句子中有多个点,例如委员会根据 5 U.S.C. 对此上诉具有管辖权。 § 8452(a)。我想使用关键字“under”来捕捉整个句子。我该怎么做?

标签: python regex list


【解决方案1】:

您的正则表达式在第一个文字点处停止。

([^.]*?( _snipped lots of text_ )[^.]*\.
                                # ^^^^^^

标记的(^^^^)部分捕获所有不是点+文字点的文本,然后就完成了。

那是The Board has jurisdiction over this appeal under 5 U.

您不显示真实文本,您可以更改此特殊情况以捕获除) 之外的任何内容,然后是)

'([^.]*?(I find|In sum|agree|affirm|disagree|I conclude|In light of| under| this appeal| The ALJ| I determine| we| based on| for the reasons| pursuant to| the decision is| jurisidiction|section|§+\d |conclude)[^)]*\))', tokenized, re.I | re.DOTALL | re.M))

【讨论】:

  • 那么括号是用来包含所有文本和字符的吗?我试图在我的代码中运行它,它似乎通过我的输出关闭。然后我必须将这些句子连接在一起,以便可以通过 Gensim Summarize 模块对它们进行总结,其中至少需要两个句子作为输入。
  • 将代码更改为您的代码时出现以下错误:
  • raise ValueError("input must have more than one sentence") ValueError: input must have more than one sentence
  • @user9608799 使用类似 regex101 的东西来了解正则表达式的作用。使用regex101.com/r/aTvKGf/1 并查看说明。它查找不是点的字符,后跟 ( ...| ...| ... ) | == or 中的任何字符,然后是更多不是点的字符,后跟一个点。您需要更改此正则表达式,使其适合您的需求。提供更多关于您想要匹配的文本和准确数据 - 用它编辑您的问题,不要将其放入 cmets,我们也许可以提供帮助。
  • 你的正则表达式在一个双捕获组和一个较小的捕获组中捕获所有内容(这可能应该是一个非分组 (?: ,,,, | ...| .... ) 所以它当您只对长句感兴趣时,不会创建新组。
猜你喜欢
  • 2015-08-13
  • 2011-12-06
  • 1970-01-01
  • 1970-01-01
  • 2011-07-18
  • 1970-01-01
  • 2013-06-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多