【问题标题】:Regular Expressions with python使用 python 的正则表达式
【发布时间】:2016-06-10 06:58:44
【问题描述】:

问题是:

编写一个脚本,读取文件中的文本,将其拆分为句子,然后 一个接一个地在屏幕上打印句子。不使用 为您进行分句的库。

以下是我的代码:

import re
fr=open('input.txt')
text=fr.read().strip()
fr.close()
Ms=re.finditer(' +([A-Z].+?\.) ',text)
for i in Ms:
    print i.group(1)

结果什么也没显示。实际上我知道可能是什么问题,因为文件的第一句前面没有多个空格,但我不知道如何解决它。

以下是我的输入:

二甲双胍将在 6-8 周内达到完全有效。它具有三个主要效果 (http://en.wikipedia.org/wiki/Metformin#Mechanism4of_action)。

首先,它(经常)降低肝脏产生的血糖量,这可能会减少您的基础需求并帮助您减少禁食次数。

第二,二甲双胍增加胰岛素,信号传导导致胰岛素敏感性增加:http://care.diabetesjournals.org/content/27/1/281.full。 效果主要是对你身体的肌肉质量。 胰岛素抵抗也会影响其他各种因素,但胰岛素最大的用途是肌肉吸收葡萄糖。

第三,二甲双胍会减少消化过程中葡萄糖的吸收。我认为正是这种作用导致了一些胃部问题。

【问题讨论】:

  • 你做过调试吗?有什么结果?文件中的是什么
  • runfile('C:/Users/Air/Desktop/660/week6/assignment.py', wdir='C:/Users/Air/Desktop/660/week6') 什么也没显示.
  • 那只是运行它,而不是调试它。请给minimal reproducible example
  • 您使用.strip() 剥离了您阅读的内容。这就是开头没有空格的原因。
  • 请说明句子的结尾。只有.!?" 可能吗?里面可能有Dr. 吗?

标签: python regex


【解决方案1】:

假设文件input.txt有以下内容:

二甲双胍将在 6-8 周内达到完全有效。它具有三个主要效果 (http://en.wikipedia.org/wiki/Metformin#Mechanism4of_action)。

首先,它(经常)降低肝脏产生的血糖量,这可能会降低你的基础需求并帮助你减少禁食次数。

第二,二甲双胍增加胰岛素,信号导致胰岛素敏感性增加:http://care.diabetesjournals.org/content/27/1/281.full。效果主要是对你身体的肌肉质量。胰岛素抵抗也会影响其他各种因素,但胰岛素最大的用途是肌肉吸收葡萄糖。

第三,二甲双胍会减少消化过程中葡萄糖的吸收。我认为正是这种作用会导致一些胃部问题。

代码如下:

import re
with open('input.txt','r') as f: fin = f.read()
print re.sub('\.\s+', '.\n', fin)

输出:

Metformin will reach full effectiveness in 6-8 weeks.
It has three primary effects (http://en.wikipedia.org/wiki/Metformin#Mechanism4of_action).
First, it (frequently) reduces the amount of blood sugar produced by your liver, this presumably will decrease your basal needs and help your fasting numbers.
Second, metformin increases the insulin, signaling resulting in increased insulin sensitivity: http://care.diabetesjournals.org/content/27/1/281.full.
The effect is primarily on the muscle mass in your body.
Insulin resistance also affects all kinds of other stuff, but the biggest utilization of insulin is in the uptake of glucose to muscles.
Third, Metformin decreases the absorption of glucose during digestion.It is this effect that I believe causes some of the gastric issues.

一个句子解析不正确,因为格式不好(两个句子之间缺少空格),应该在文本文件中修复。

更新话虽如此,请在文本文件不变的情况下尝试以下操作:

import re
with open('input.txt','r') as f: fin = f.read()
print re.sub('\.\s*([A-Z])', '.\n\g<1>', fin)

【讨论】:

  • 最后两句还是有问题。因为最后两个之间没有空格所以不能分开。
【解决方案2】:

很难在没有看到您的输入的情况下发表评论,但请注意,您需要注意前导和尾随空格。在下面的示例中,第一个单词因为没有前导空格而被遗漏,如果您需要尾随空格,则第二个句子将被遗漏。

>>> text = "See Spot run. Run, Spot, run."

>>> re.findall(' +([A-Z].+?\.)',text)

['Spot run.',' Run, Spot, run.']

>>> re.findall(' +([A-Z].+?\.) ',text)

['Spot run. ']

我们可以在字符类上做得稍微好一些,但您需要确定句子的准确划分方式。

>>> re.findall('([\w, ]+\.)',text)

['See Spot run.', ' Run, Spot, run.']

>>> re.findall('[^.]+\.',text)

['See Spot run.', ' Run, Spot, run.']

但在许多情况下,按句点拆分会失败,例如示例输入中的 URL,或以下内容:

>>> re.findall('[^.]+\.',"See Dr. Spock run. Run, Spock, run.")

['See Dr.', ' Spock run.', ' Run, Spock, run.']

【讨论】:

  • 谢谢您,先生。我输入的内容已添加到屏幕上。
【解决方案3】:

试试这个:

import re

with open('input.txt', 'r') as f:
    data = f.read()

print('\n'.join(re.split(r'\n', data, re.M)))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-30
    • 2010-09-12
    • 2011-08-06
    • 2013-03-24
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多