Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.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 将文本段落拆分为句子_Python - Fatal编程技术网

Python 将文本段落拆分为句子

Python 将文本段落拆分为句子,python,Python,我正在尝试拆分一个文本文件。这是一个很大的段落。我想把它分成几个小句子,让每个句子都成为一个列表。从那里我可以找出哪些列表包含特定的单词 这是我目前的代码: import string Done = False while not Done: try: File = input("Enter your file: ") Open_File = open(File, "r") Info = Open_File.readline()

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

这是我目前的代码:

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)
导入字符串
完成=错误
虽然没有这样做:
尝试:
文件=输入(“输入您的文件:”)
打开文件=打开(文件“r”)
Info=Open_File.readline()
打印(信息)
打开_文件。关闭()
完成=正确
除FileNotFoundError外:
打印(“对不起,该文件不存在!”)
Info_Str=Str(Info)
Info_Str=Info_Str.lower()
Info\u Str=Info\u Str.replace(“,”)
Info\u Str=Info\u Str.replace(“-”,“”)
Info_Str=Info_Str.split()
信息列表=信息列表
信息列表=[''.join(如果c不在字符串中,则c表示s中的c。标点符号)表示信息列表中的s]
New_List=[如果不是item.isdigit(),则信息列表中的项对应项]
对于新列表中的单词[:]:
如果len(word)<3:
新列表。删除(word)
打印(新列表)
如果我输入一个文本文件,它只返回文本文件的第一行作为单词列表


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

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

  • 使用
    with
    语句打开文件。使用语句的好处是,您不必显式关闭文件
  • 可以使用“.”或“?”将段落拆分为一行
  • 可以使用单个空格将每行拆分为列表
  • 然后,您可以在该列表中搜索所需的单词
  • 代码:

    #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)


    你的确切要求是什么?如果您只想获取文件中的单词列表,您可以读取所有行并使用空格分隔符拆分。我基本上必须找出特定单词出现在哪些行号中。每一行都是一个单独的句子。检查我发布的代码片段。这应该会有帮助。我试了一下,然后我意识到:不是所有的句子都必须以句号结尾(?,!,等等)。我认为导致“它只返回文本文件的第一行作为单词列表”的最初错误是这一行:
    Info=Open_file.readline()
    在您的情况下,每一行都不是以“.”分隔的行。假设我有
    Hello.new line\n同一行。
    “new line”和“same line”将在不同的列表中。我尝试改为使用“Info=Open_File.read()”,但它只是将整个段落作为一个大的单词列表返回,而不是在每个新句子处将其拆分。@JacobIRR-使用“?”或"!“还是别的什么
    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")