mardi 15 mars 2016

Get random sentences from a set of text files

I am not much of a programmer but have been asked to help a professor of mine with a research project which requires a basic level of programming knowledge. We are pulling 1000 sentences from a set of 22 text files. Each text file contains thousands of sentences, all strung together in one paragraph, like so:

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 .

Pretty ugly. Anyway, I am unsure how to pull random sentences from a block of text like this. I am fairly confident that these are the steps my program must follow:

  1. Select a random text file from the set of 22 text files.
  2. Select a random sentence from the text file.
    • I think this could be done by choosing a random number and "counting" fullstops (.) up to that number, then choosing the sentence after that fullstop as the random sentence, stopping at the next fullstop.
  3. Write the selected sentence to a separate textfile.
  4. Repeat 999 more times.

Here is the code I have written so far. It does not "count" the fullstops as I considered doing above, because I don't know how to do that, but I tried to start outlining the basic pieces I know. I know my code is pretty ugly, but I have never really programmed anything before. Thanks for the help!

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++




Aucun commentaire:

Enregistrer un commentaire