python重新匹配,findall或搜索,然后再进行NLP(怎么办?) [英] python re match, findall or search and then NLP (what to do with it?)

查看:103
本文介绍了python重新匹配,findall或搜索,然后再进行NLP(怎么办?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始编写捕获部分句子类型"的代码,如果它们符合条件,则启动一个处理类型"的特定python脚本.我正在寻找" :)发现所有类型的作品都可以更好地满足我的工作需求:

I am starting to write code that would capture part of sentence "types" and if they match a criteria, start a specific python script that deals with the "type." I am "finding":) that findall kind of works better for what i am doing hence:

m = re.compile(r'([0-9] days from now)')
m.match("i think maybe 7 days from now i hope")
print m.match("i think maybe 7 days from now i hope")
None
f= m.findall("i think maybe 7 days from now i hope")
print f[0]
7 days from now

这似乎给了我我所要寻找的那部分句子.然后,我可以举个例子-pyparsing模块使用其示例datetime转换脚本,该脚本从类似的NL语句返回日期时间(我知道还有其他模块,但是它们在可以处理的输入语句中很严格).
然后,如果句子的其他部分与另一个类型"匹配,例如,我可以在我的在线日记中或在托管的Web应用程序中进行数据库插入.约会,截止日期等.
我只是在这里修补,但是我正在慢慢地构建一些有用的东西.这个结构/过程是否合乎逻辑,或者是否有更好的方法/方式:这就是我现在要问自己的问题.感谢您提供任何反馈意见

This seems to give me the part of sentence that i was looking for. I can then give this to for example - the pyparsing module using its example datetime conversion script that returns a datetime from a similar NL statement (I know there are other modules but they are rigid in input statements they can handle) .
Then I could do a db insert into my online diary for example or on a hosted web app if other parts of the sentence matched another "type" ie. appointments, deadlines etc.
I am just tinkering here but slowly i am building something useful. Is this structure /process logical or are there better methods/ ways: that is what i am asking myself now. Any feedback is appreciated

推荐答案

m.match()失败的原因是,它希望匹配从字符串的开头开始.

The reason why m.match() fails is that it expects the match to start at the beginning of the string.

findall()才有意义.否则,请使用search()方法(该方法将返回它找到的第一个匹配项).

findall() makes sense if you expect more than one (non-overlapping) match in your string. Otherwise, use the search() method (which will return the first match it finds).

文档文档中的所有内容都很好.

这篇关于python重新匹配,findall或搜索,然后再进行NLP(怎么办?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆