【问题标题】:Splitting an item list using regex使用正则表达式拆分项目列表
【发布时间】:2019-06-19 17:29:02
【问题描述】:

首先,我有一个由项目列表组成的字符串,这些项目可以通过 enumerator(逗号/'and')或 文章('a'/'an'/'the')。请注意,如果有枚举数,则可以省略冠词,反之亦然。

例如让我们看看这个输入:

a paper, leaf the clock and an angel

这必须分为:

  • a paper
  • leaf
  • the clock
  • an angel

第一个示例只有单个名称的项目,所以让我们看另一个示例:

a paper with some letters, a torn leaf and clock and an angel doll

这必须分为:

  • a paper with some letters
  • torn leaf
  • clock
  • an angel doll

我已经为此尝试了一些正则表达式,而我最接近的是使用:

(?:\b(?P<article>the|an|a)\b)\s(?P<object>\b.+?\b(?=\b(?:the|an|a|$)\b))

当然,我没有考虑到 ','/'and' 拆分,因为我无法弄清楚,很遗憾。

最后如你所见,我使用组来识别/分离 objectarticle。如果可以这样,那就太好了。你有什么建议?

【问题讨论】:

  • 我建议使用适当的工具,例如 NLTK,来解析自然语言。
  • 您在某些情况下保留了该文章,而在其他情况下将其丢弃。这是一个错误吗?还是有一些您遗漏的规则?
  • 如果有枚举数(and or comma),文章可以有也可以省略! @迈克
  • @georg 我理解你所说的,这可能是一个更好的主意......但是 NLTK 上的许可证仅用于非商业用途.. 我可能需要一段时间才能实际使用它用于商业用途。
  • @xDGameStudios:NLTK 在 Apache 2.0 下获得许可。 (github.com/nltk/nltk/wiki/FAQ)。如果确实允许任何商业用途并且是非 Copyleft,那么您不必按照相同的条款分发您的软件。

标签: python regex regex-group


【解决方案1】:

按正则表达式中与re.split()匹配的内容的降序排列所有小案例:

import re

s = "a paper with some letters, a torn leaf and clock and an angel doll"

re.split(r'^an |^a |^the |, and a |, and an |, and the |, and |, and an |, an |, the |, a | and an | and | an | the', s)
# ['', 'paper with some letters', 'torn leaf', 'clock', 'angel doll']

剩下的就是清理'',等等。

要保留匹配的内容,请按照文档将正则表达式括在括号中:

re.split(r'(^an |^a |^the |, and a |, and an |, and the |, and |, and an |, an |, the |, a | and an | and | an | the )', s)

# ['', 'a ', 'paper with some letters', ', a ', 'torn leaf', ' and ', 'clock', ' and an ', 'angel doll']

【讨论】:

  • 如果有的话,我需要把文章保留在那里(用于后期处理)
【解决方案2】:

只需使用re.split()

import re

a = "a paper with some letters, a torn leaf and clock and an angel doll"

### put every separator you want to remove after a |
re.split(', |and |a ',a)
# result:
['', 'paper with some letters', '', 'torn leaf ', 'clock ', '', 'angel doll']

如果您需要保留分隔符,只需使用括号:

[i for i in re.split('(, |and |a )',a) if i]
# result:
['a ', 'paper with some letters', ', ', 'a ', 'torn leaf ', 'and ', 'clock ', 'and ', 'an angel doll']

【讨论】:

  • 正如我所说,我需要将文章保留在那里(a/an/the)进行一些后期处理!
  • 好的,我明白了...但话又说回来...我需要赶上组 articleobject
【解决方案3】:

关于我想解决的具体任务,我想到了另一个想法, 步骤如下:

  • 只要有“and”或“,”后面没有and冠词,就添加默认冠词(the)
"( and|,) (?!the|an|a)|^(?!the|an|a)" # replace with " the "
  • 从输入文本中删除每个“and”或“,”(现在每个对象都应该用文章分隔)
"( and|,) " # replace with " "
  • 将输入分离为文章+除文章之外的所有内容
"(?P<article>the|an|a) (?P<object>.+?(?= (?:the|an|a)\b)|[^$]*)"

PS:如果有人知道最后一个正则表达式的替代方法,请随时发布! :)

【讨论】:

    【解决方案4】:

    通过使用 re.sub() 我们可以用新行替换特定的字符串。 在 re.sub() 中,您可以添加任何需要用新行替换的文章。

    示例代码:

    s = 'a paper with some letters, a torn leaf and clock and an angel doll'
    
    print(re.sub(r'(and|,)\s', r"\0\n", s))
    

    输出:

    a paper with some letters
    a torn leaf 
    clock 
    an angel doll
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-07
      • 2021-04-23
      • 1970-01-01
      • 2020-08-25
      • 1970-01-01
      • 2018-04-06
      • 2017-10-03
      • 1970-01-01
      相关资源
      最近更新 更多