python正则表达式匹配句子。

valerenxy 2014-03-18 10:52:59
如何在python里用正则表达式匹配一段话里的一些关键搭配并将关键搭配所在句子抽取出来。
如:
Hello everyone,my name is Bob.This is my friend Alice.We both like movies.
怎么在这样一段话里匹配关键搭配“We like”并且将We both like movies.这句话提取出来(存在一个列表或者元组里都行)。
...全文
654 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
lsjfdjoijvtghu 2014-04-02
  • 打赏
  • 举报
回复
正则匹后group[0],我的最爱
QIAOXINGXING 2014-03-30
  • 打赏
  • 举报
回复
学习了。。。。。。。。。
zengna_com 2014-03-26
  • 打赏
  • 举报
回复
"\bwe\b.*?\blike\b.*?\."
至于你说的其他词语可以用列表循环一下。
libralibra 2014-03-19
  • 打赏
  • 举报
回复
如果已经split过了,每个元素都是一个句子;直接用loop循环检测是否每个关键字都在句子中出现就可以了,不用正则 简单例子
s = '''Hello everyone,my name is Bob.This is my friend Alice.We both like movies. '''
ss = s.split('.')
key_words = "we like"
words_list = [x.lower() for x in key_words.split()]
for item in ss:
    if all([word in item.lower() and True or False for word in words_list]):
        print item
>>> ================================ RESTART ================================
>>> 
We both like movies
>>> 
libralibra 2014-03-19
  • 打赏
  • 举报
回复
引用 5 楼 valeren 的回复:
[quote=引用 3 楼 libralibra 的回复:] 如果已经split过了,每个元素都是一个句子;直接用loop循环检测是否每个关键字都在句子中出现就可以了,不用正则 简单例子
s = '''Hello everyone,my name is Bob.This is my friend Alice.We both like movies. '''
ss = s.split('.')
key_words = "we like"
words_list = [x.lower() for x in key_words.split()]
for item in ss:
    if all([word in item.lower() and True or False for word in words_list]):
        print item
>>> ================================ RESTART ================================
>>> 
We both like movies
>>> 
这个方法在匹配关键词少的情况下很实用!非常感谢! 同时请问如果想匹配更多的关键字组呢,例如现在key_words 里只有 "we like"这一个搭配,如果还想扩展为在一段话内对更多的搭配抽取的话应该如何完善这个key_words呢。比如想抽取一段话内包含如下关键字的句子——we propose、we address、We present、we mine、We use、We conduct、we demonstrate、we study、We perform、we integrate、we consider、we select、we classified、we integrate……等等等等 如何写,才能key_words包含这些搭配。(是否该用正则表达式了呢?)[/quote] 这种时候是不是考虑做关键字set,然后句子set与关键字set做intersection,如果结果不为空就说明有关键字
valeren 2014-03-19
  • 打赏
  • 举报
回复
引用 4 楼 moon7421 的回复:
str1 = r'Hello everyone,my name is Bob.This is my friend Alice.We both like movies.' pattern = r'\bWe.*like.*' tRet = re.findall(pattern,str1); print tRet ['We both like movies.']
非常感谢!正则表达式匹配很完美!刚刚进行了一个尝试,将句子扩展后(在We both like movies后增添了若干句子。)这个正则表达式会将We both like movies后的所有句子一起打印出来,这个如何解决呢。 另外请问如果想匹配更多的关键字组呢,例如现在只有 "we like"这一个搭配,如果还想扩展为在一段话内对更多的搭配抽取的话应该如何完善这个正则表达式呢。比如想抽取一段话内包含如下关键字的句子——we propose、we address、We present、we mine、We use、We conduct、we demonstrate、we study、We perform、we integrate、we consider、we select、we classified、we integrate……等等等等 谢谢!
valeren 2014-03-19
  • 打赏
  • 举报
回复
引用 3 楼 libralibra 的回复:
如果已经split过了,每个元素都是一个句子;直接用loop循环检测是否每个关键字都在句子中出现就可以了,不用正则 简单例子
s = '''Hello everyone,my name is Bob.This is my friend Alice.We both like movies. '''
ss = s.split('.')
key_words = "we like"
words_list = [x.lower() for x in key_words.split()]
for item in ss:
    if all([word in item.lower() and True or False for word in words_list]):
        print item
>>> ================================ RESTART ================================
>>> 
We both like movies
>>> 
这个方法在匹配关键词少的情况下很实用!非常感谢! 同时请问如果想匹配更多的关键字组呢,例如现在key_words 里只有 "we like"这一个搭配,如果还想扩展为在一段话内对更多的搭配抽取的话应该如何完善这个key_words呢。比如想抽取一段话内包含如下关键字的句子——we propose、we address、We present、we mine、We use、We conduct、we demonstrate、we study、We perform、we integrate、we consider、we select、we classified、we integrate……等等等等 如何写,才能key_words包含这些搭配。(是否该用正则表达式了呢?)
ahumoon7421 2014-03-19
  • 打赏
  • 举报
回复
str1 = r'Hello everyone,my name is Bob.This is my friend Alice.We both like movies.' pattern = r'\bWe.*like.*' tRet = re.findall(pattern,str1); print tRet ['We both like movies.']
jeky_zhang2013 2014-03-18
  • 打赏
  • 举报
回复
看下自带的python文档,re模块
valerenxy 2014-03-18
  • 打赏
  • 举报
回复
我已经用split将段落分句了,下一步是做匹配,求高手指导。

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧