从一组文本文件中获取随机句子

时间:2016-03-13 17:34:31

标签: python random linguistics

我不是一个程序员,但是被要求帮助我的一位教授进行一项研究项目,该项目需要基本的编程知识水平。我们从一组22个文本文件中提取1000个句子。每个文本文件包含数千个句子,所有句子都串在一个段落中,如下所示:

By this time , the resistance was severe . Why ? Consider the houses that were coming down , to be replaced by modern flats . They are typical of the row houses for workers of the dreary industrial towns of England . These " cottages , " as they were called in Sunderland , were built for the working class by unnamed builders one hundred years ago . Many were rented , but most were by this time owned by their inhabitants . The houses sit directly on the sidewalk . There are no trees on the street . There is no front yard .

非常丑陋。无论如何,我不确定如何从这样的文本块中提取随机句子。我相信这些是我的计划必须遵循的步骤:

  1. 从22个文本文件集中选择一个随机文本文件。
  2. 从文本文件中选择一个随机句子。
    • 我认为这可以通过选择一个随机数和"计数" fullstops(。)直到那个数字,然后选择句子后面的句子作为随机句子,停在下一个句号。
  3. 将选定的句子写入单独的文本文件。
  4. 重复999次。
  5. 这是我到目前为止编写的代码。它没有"计数"我考虑过上面做的那些全程,因为我不知道该怎么做,但我试着开始概述我所知道的基本部分。我知道我的代码非常难看,但我以前从未真正编写任何程序。谢谢你的帮助!

    fileNumber = 0
    sentenceNumber = 0
    i = 0
    fname = "x"
    sentence = "x"
    
    for i < 1000
    
        fileNumber = random.randint(0, 22)          #chooses a random number in order to assign one of the text files
        sentenceNumber = random.randint(0, NUMBER OF FULLSTOPS) #chooses a random number to select a random sentence from the previously selected text file
    
        if fileNumber == 0              #assigns the file to search based on the random number stored in fileNumber
            fname = "w_acad_1990.txt"
        else if fileNumber == 1
            fname = "w_acad_1991.txt"
        else if fileNumber == 2
            fname = "w_acad_1992.txt"
        else if fileNumber == 3
            fname = "w_acad_1993.txt"
        else if fileNumber == 4
            fname = "w_acad_1994.txt"
        else if fileNumber == 5
            fname = "w_acad_1995.txt"
        else if fileNumber == 6
            fname = "w_acad_1996.txt"
        else if fileNumber == 7
            fname = "w_acad_1997.txt"
        else if fileNumber == 8
            fname = "w_acad_1998.txt"
        else if fileNumber == 9
            fname = "w_acad_1999.txt"
        else if fileNumber == 10
            fname = "w_acad_2000.txt"
        else if fileNumber == 11
            fname = "w_acad_2001.txt"
        else if fileNumber == 12
            fname = "w_acad_2002.txt"
        else if fileNumber == 13
            fname = "w_acad_2003.txt"
        else if fileNumber == 14
            fname = "w_acad_2004.txt"
        else if fileNumber == 15
            fname = "w_acad_2005.txt"
        else if fileNumber == 16
            fname = "w_acad_2006.txt"
        else if fileNumber == 17
            fname = "w_acad_2007.txt"
        else if fileNumber == 18
            fname = "w_acad_2008.txt"
        else if fileNumber == 19
            fname = "w_acad_2009.txt"
        else if fileNumber == 20
            fname = "w_acad_2010.txt"
        else if fileNumber == 21
            fname = "w_acad_2011.txt"
        else if fileNumber == 22
            fname = "w_acad_2012.txt"
        else
            fname = "x" 
    
        #select a random sentence
        #write that sentence to a text file
    
        i++
    

3 个答案:

答案 0 :(得分:3)

我建议这样的事情:

import re
from random import sample

sentences = []
for i in range(23):
    with open('w_acad_{}.txt'.format(i)) as f:
        sentences += re.findall(r".*?[\.\!\?]+", f.read())

selected = sample(sentences, 1000)
with open('out.txt', 'w') as f:
    f.write(''.join(selected))

首先,如果您在句子之前选择文件,则所有句子之间都没有等概率。最好在选择之前检索所有句子。

不要打开文件1000次。这很重!

请勿按照他人的建议使用split因为您将丢失分隔符。 在这里,我使用findall模块中的re,以便我可以使用多个分隔符(不仅.而且!等),并且在检索句子时保留它。

最后,您可以使用sample模块中的random来选择1000个句子。

答案 1 :(得分:0)

import random

# Do this 1000 times:
for i in range(1000):
    fileNumber = random.randint(0,22)
    # calculate file name instead of long condition chain:
    with open("w_acad_{}.txt".format(1990+fileNumber)) as f:
        # read file and split sentences on full stop:
        sentences = f.read().split(".")
    with open("sentence_{}.txt".format(i), "w") as f:
        # write a random sentence from the file:
        f.write(random.choice(sentences) + ".")

答案 2 :(得分:0)

如果这对其他任何人都有用:这是一种修改,可以从txt中删除一个句子,并返回剩余的文本和删除的句子

def remove_random_sentence(txt):
    sentences = []
    sentences += re.findall(r".*?[\.\!\?]+", txt)
    if len(sentences) > 1:
        removed_sentence = sample(sentences, 1)[0]  # sample returns list, accsess first element to get string
        return txt.replace(removed_sentence, ''), removed_sentence
    else:
        return 'ONLY ONE SENTENCE', 'REMOVE'