Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python根据搜索词筛选sentencs?_Python_Text_Nltk - Fatal编程技术网

使用python根据搜索词筛选sentencs?

使用python根据搜索词筛选sentencs?,python,text,nltk,Python,Text,Nltk,我有一个包含许多句子的文本文件。我想用搜索词查询文本文件,并返回那些包含查询词的句子 迄今为止的努力: h = input("Enter search word: ") with open("file.txt") as openfile: for line in openfile: for part in line.split(): if h in part: print (

我有一个包含许多句子的文本文件。我想用搜索词查询文本文件,并返回那些包含查询词的句子

迄今为止的努力:

h = input("Enter search word: ")
with open("file.txt") as openfile:
    for line in openfile:
        for part in line.split():
            if  h in part:
                print (part)
file.txt包含以下句子

On the Insert tab, the galleries include items that are designed to coordinate with the overall look of your document. 
You can use these galleries to insert tables, headers, footers, lists, cover pages, and other document building blocks. 
When you create pictures, charts, or diagrams, they also coordinate with your current document look.
You can easily change the formatting of selected text in the document text by choosing a look for the selected text from the Quick Styles gallery on the Home tab.
You can also format text directly by using the other controls on the Home tab.
Most controls offer a choice of using the look from the current theme or using a format that you specify directly.
To change the overall look of your document, choose new Theme elements on the Page Layout tab. To change the looks available in the Quick Style gallery, use the Change Current Quick Style Set command.
Both the Themes gallery and the Quick Styles gallery provide reset commands so that you can always restore the look of your document to the original contained in your current template.
输出:对于“图库”搜索,它会返回两次图库,但我需要返回句子

如何查询多个单词搜索并返回包含这些组合的句子(不一定是n克或顺序),例如,如果我键入“总体”作为一个单词,键入“布局”作为另一个搜索单词,则应返回以下句子。搜索词不区分大小写

To change the overall look of your document, choose new Theme elements on the Page Layout tab.

救命啊

这适用于多个搜索词:

myfile = "queryfile.txt"
search_wordlist = input("Enter search words, separated by a comma\n")
mylist = search_wordlist.split(",")

with open(myfile) as openfile:
   for line in openfile:
      for term in mylist:
         if term in line:
             print(line)

使用
nltk
word\u-tokinzizer
句子\u-tokinzizer
它可以工作。但是你可以让搜索不区分大小写,并考虑多个关键字搜索。