【发布时间】:2021-09-08 09:12:46
【问题描述】:
我有句子在其中引用文本,例如:
Why did the author use three sentences in a row that start with the words, "it spun"?
Why did the queen most likely say “I would have tea instead.”
Why did the fdsfdsf repeat the phrase "he waited" so many times?
Why were "the lights of his town growing smaller below them"?
What is a fdsfdsf for the word "adjust"?
Reread this: "If anybody had asked trial of answered at once, 'My nose.'" What is the correct definition of the word "trial" as it is used here?
Reread these sentences: "This was his courtship, and it lasted all through the summer." What does the word "courtship" mean?
我试图用 REGEX 掩盖引用的部分,但它不准确。比如最后一句:
txt = 'Reread these sentences: "This was his courtship, and it lasted all through the summer." What does the word "courtship" mean?'
print(re.sub(r"(?<=\").{20,}(?=\")", "<quote>", txt))
输出是:
Reread these sentences: "<quote>" mean?
相反,它应该是:
Reread these sentences: "<quote>" What does the word "courtship" mean?
由于我有超过 10k 个实例,因此很难找到适用于所有情况的通用 REGEX 模式。
我的问题是,是否有任何库(可能基于神经网络实现?)或方法来解决这个问题?
【问题讨论】:
-
您需要将此类问题视为“匹配一个引号,然后匹配任意数量的非引号,然后匹配一个引号”。如果你认为它是“匹配一个引用,然后是任何东西,然后是另一个引用”,你会因为正则表达式的贪婪而失败。
-
@anubhava,它对这个不起作用:“重读这个:“如果有人要求试用,马上回答,'我的鼻子。'”“试用”这个词的正确定义是什么“就像这里使用的那样?”
-
@TimRoberts 抱歉,不清楚。你能澄清你的答案吗?
-
@anubhava,当它是用双引号括起来的文本时。
标签: python regex machine-learning quotes