python - 在烧瓶应用程序中提取带有关键词的句子
问题描述
我正在尝试使用用户输入中的单词来提取句子。我有一个index.html
表格和一个results.html
渲染句子列表。
所以这就是我尝试过的:
/*all the imports (flask, re, nltk, etc.)*/
letexte = "Welcome In The Details
about
Well... pretty simple. Do you want to know the amount of paragraphs, sentences or words in a text? Do
you want to know how many times a word or a sentences appear in your text? Or maybe the 10, 30 or 50
words that appear the most ? It's all In The Details
just enter:
the text you want to analyze.
the word you want to have the number of appearition.
and a title for researches."
input_two = "text"
lesphrases = re.findall(r"([^.]*?%s.*?\.)(?!\d)" % input_two, letexte)
print(lesphrases)
Usingre.findall
返回一个空列表[]
(我不知道为什么)并且nltk.sent_tokenize
可以工作,但是在某些情况下它\n
包含在句子列表中,我想避免这种情况。
就我而言,我觉得re.findall
这是正确的解决方案,但也存在“Dr. name”的问题,所以......我不知道。
感谢您的任何帮助。
解决方案
不使用 re,你可以做这样的事情:[i for i in letexte.split('.') if input_two in i]
.
如果要避免“\n”,请将“\n”[print(i) for i in letexte.split('.') if input_two in i]
呈现为“新行”。
否则,这可能会对您有所帮助:[i.replace('\n', '') for i in letexte.split('.') if input_two in i]
.
推荐阅读
- css - 在 CSS Grid 中,如何删除第 1 列与第 2 列的匹配高度?
- delphi - 下载大文件时出现 Indy TIdFTP“内存不足”错误
- javascript - 如何为 MaterialUI 组件中使用的“颜色”道具使用主题颜色
- android - 单击文本视图时根据文本视图形状更改文本视图的背景颜色
- c# - LINQ:如何选择唯一行:具有多列的 max(id)?
- javascript - 如何将图表添加到 Firebase?
- php - 父类中的私有属性可以通过 PHP 中的子类的实例进行访问和重新分配,但受保护的不能
- spring - Spring Boot as API Rest 不会反序列化整个对象
- jquery-ui - 关于伪禁用元素的 jQuery UI 工具提示
- reactjs - 如果我不使用云功能,如何测试我的 react js + firestore 项目?