首页 > 解决方案 > 在烧瓶应用程序中提取带有关键词的句子

问题描述

我正在尝试使用用户输入中的单词来提取句子。我有一个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”的问题,所以......我不知道。

感谢您的任何帮助。

标签: pythonflask

解决方案


不使用 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].


推荐阅读