在python中搜索文档中的关键字
我正在尝试编写一个Python脚本,以便它可以在文档中搜索关键字,并检索关键字所在的整个句子。根据我的研究,我发现 acora 可以使用,但我仍然发现它不成功。
I am trying to write a python script so that it can search for a keyword in a document, and retrieve the entire sentence where the keyword is. From my research i saw that acora can be used but i still found it unsuccessful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这就是你可以在 shell 中简单地完成它的方法。你应该自己把它写成脚本。
在这里,我用
.split('.')
分割text
并迭代,然后用单词and
控制,如果包含,则打印它。您还应该考虑到这是区分大小写的。您应该在解决方案中考虑很多事情,例如以
!
和?
结尾的东西也是句子(但有时不是)将被拆分为
That's how you can simply do it in shell. You should write it in script yourself.
Here I splitted
text
with.split('.')
and iterated, then controlled with wordand
and if it contains, printed it.You should also consider that this is case-sensitive. You should consider many things on your solution, such as things ending with
!
and?
are also sentences (but sometimes they aren't)is going to be splitted as
我对此没有太多经验,但您可能正在寻找
nltk
。尝试这个;使用
span_tokenize
找到您的单词索引属于哪个范围,然后查找该句子。I don't have much experience with this but you might be looking for
nltk
.Try this; use
span_tokenize
and find which span the index of your word falls under, then look that sentence up.使用grep或egrep命令与python的子进程模块,它可能会帮助你。
例如:
use grep or egrep commands with subprocess module of python, it may help you.
e.g: