把文本段落分成句子

2024-04-24 03:41:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图分割一个文本文件。这是一个大段落。我想把它分成几个小句子,让每个句子都列一张单子。从那里我可以找出哪些列表包含一个特定的单词。

这是我目前的代码:

import string

Done = False
while not Done:
    try:
        File = input("Enter your file: ")
        Open_File = open(File, "r")
        Info = Open_File.readline()
        print(Info)
        Open_File.close()
        Done = True
    except FileNotFoundError:
        print("Sorry that file doesn't exist!")


Info_Str = str(Info)
Info_Str = Info_Str.lower()
Info_Str = Info_Str.replace("'", "")
Info_Str = Info_Str.replace("-", "")
Info_Str = Info_Str.split()
Info_List = Info_Str
Info_List = [''.join(c for c in s if c not in string.punctuation) for s in  Info_List]
New_List = [item for item in Info_List if not item.isdigit()]
for word in New_List[:]:
    if len(word) < 3:
        New_List.remove(word)
print(New_List)

如果我放入一个文本文件,它只返回文本文件的第一行作为单词列表。

如何将每个句子转换为单独的单词列表?提前谢谢。


Tags: ininfo列表newfornotopen单词
2条回答

你写的代码有点大。您可以用更少的代码行来完成此任务。让我们先来看看如何实现这一目标:

  1. 使用with语句打开文件。with语句的好处是不必显式关闭文件。
  2. 段落可以使用“.”或“?”拆分成一行。
  3. 每一行可以用一个空格分成一个列表。
  4. 然后,你可以在列表中搜索你想要的单词。

代码:

#open File
with open("a.txt") as fh:
    for line in fh:
        #Split Paragraph on basis of '.' or ? or !.

        for l in re.split(r"\.|\?|\!",line):
            #Split line into list using space.
            tmp_list = l.split(" ")
            #Search word and if found print that line
            if "Dinesh" in tmp_list:
                print l

注意:我的代码也可以优化。我想,既然你刚开始,这对你有好处。

这将打印句子编号(0索引)。

with open("sample.txt") as f:
    content = f.read() # Read the whole file
    lines = content.split('.') # a list of all sentences
    for num,line in enumerate(lines): # for each sentence
           if 'word' in line:
               print(num)
           else:
               print("Not present") 

相关问题 更多 >