加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

使用Python中的re包将句子分成子句

发布时间:2020-12-20 13:09:28 所属栏目:Python 来源:网络整理
导读:我有一个很多句子的数据,关于一个例子作为下面的句子,我想把它分成2个子句子: Both whole plasma and the d 1.006 g/ml density fraction of plasma from 2/2 mice show this broad beta-migration pattern (Fig. 1 B) |T:**1SP3E3| ; |I:**1SP3E3| |L:**1S
我有一个很多句子的数据,关于一个例子作为下面的句子,我想把它分成2个子句子:

Both whole plasma and the d < 1.006 g/ml density fraction of plasma
from 2/2 mice show this broad beta-migration pattern (Fig. 1 B)
|T:**1SP3E3| ; |I:**1SP3E3| |L:**1SP3E3| in contrast,3/3 plasma shows
virtually no lipid staining at the beta-position. |T:**1SN3E3|
|I:**1SN3E3| |L:**1SN3E3|

将它拆分为:

Both whole plasma and the d < 1.006 g/ml density fraction of plasma
from 2/2 mice show this broad beta-migration pattern (Fig. 1 B)

in contrast,3/3 plasma shows virtually no lipid staining at the
beta-position.

我的代码是:

newData =[]
for item in Data:
    test2= re.split(r" (?:|.*?| ?)+",item[0])
    test2 =test2[:-1]
    for tx in test2:
        newData.append(tx)
print len(newData)
print newData

但是,我在结果中得到了3个项目,包括;我查了一下原来的句子,发现了,在| T:** 1SP3E3 | ; | I:** 1SP3E3 |,所以我需要删除它;从结果出来.我修改了我的代码

test2= re.split(r" (?:|.*?| ?;?)+",item[0])

但我无法得到正确的结果.有人可以帮忙吗?非常感谢.

解决方法

[i.strip() for i in re.sub(r'|w:**w*|','',re.sub(r' +',r' ',s.strip())).split(';')]

返回

['Both whole plasma and the d < 1.006 g/ml density fraction of plasma from 2/2 mice show this broad beta-migration pattern (Fig. 1 B)','in contrast,3/3 plasma shows virtually no lipid staining at the beta-position.']

但是要考虑一下,因为它取决于你的文字是否与你的例子一致.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读