Skip to content Skip to sidebar Skip to footer

Python Code Flow Does Not Work As Expected?

I am trying to process various texts by regex and NLTK of python -which is at http://www.nltk.org/book-. I am trying to create a random text generator and I am having a slight prob

Solution 1:

How about this?

  1. You find longest word in trigger
  2. You find longest word in the longest sentence containing word found in 1.
  3. The word of 1. is the longest word of the sentence of 2.

What happens? Hint: answer starts with "Infinite". To correct the problem you could find set of words in lower case to be useful.

BTW when you think MontyPython becomes False and the program finish?

Solution 2:

Mr. Hankin's answer is more elegant, but the following is more in keeping with the approach you began with:

import sys
import string
import nltk
from nltk.corpus import gutenberg

deflongest_element(p):
    """return the first element of p which has the greatest len()"""
    max_len = 0
    elem = Nonefor e in p:
        iflen(e) > max_len:
            elem = e
            max_len = len(e)
    return elem

defdowncase(p):
    """returns a list of words in p shifted to lower case"""returnmap(string.lower, p)


defunique_words():
    """it turns out unique_words was never referenced so this is here
       for pedagogy"""# there are 2.6 million words in the gutenburg corpus but only ~42k unique# ignoring case, let's pare that down a bitfor word in gutenberg.words():
        words.add(word.lower())
    print'gutenberg.words() has', len(words), 'unique caseless words'return words

print'loading gutenburg corpus...'
sentences = []
for sentence in gutenberg.sents():
    sentences.append(downcase(sentence))

trigger = sys.argv[1:]
target = longest_element(trigger).lower()
last_target = Nonewhile target != last_target:
    matched_sentences = []
    for sentence in sentences:
        if target in sentence:
            matched_sentences.append(sentence)

    print'===', target, 'matched', len(matched_sentences), 'sentences'
    longestSentence = longest_element(matched_sentences)
    print' '.join(longestSentence)

    trigger = longestSentence
    last_target = target
    target = longest_element(trigger).lower()

Given your sample sentence though, it reaches fixation in two cycles:

$ python nltkgut.py Thane of code loading gutenburg corpus... === target thane matched 24 sentences norway himselfe , with terrible numbers , assisted by that most disloyall traytor , the thane of cawdor , began a dismall conflict , till that bellona ' s bridegroome , lapt in proofe , confronted him with selfe - comparisons , point against point , rebellious arme ' gainst arme , curbing his lauish spirit : and to conclude , the victorie fell on vs === target bridegroome matched 1 sentences norway himselfe , with terrible numbers , assisted by that most disloyall traytor , the thane of cawdor , began a dismall conflict , till that bellona ' s bridegroome , lapt in proofe , confronted him with selfe - comparisons , point against point , rebellious arme ' gainst arme , curbing his lauish spirit : and to conclude , the victorie fell on vs

Part of the trouble with the response to the last problem is that it did what you asked, but you asked a more specific question than you wanted an answer to. Thus the response got bogged down in some rather complicated list expressions that I'm not sure you understood. I suggest that you make more liberal use of print statements and don't import code if you don't know what it does. While unwrapping the list expressions I found (as noted) that you never used the corpus wordlist. Functions are a help also.

Solution 3:

You are assigning "split_str" outside of the loop, so it gets the original value and then keeps it. You need to assign it at the beginning of the while loop, so it changes each time.

import nltk

from nltk.corpus import gutenberg

triggerSentence = raw_input("Please enter the trigger sentence: ")#get input str

longestLength = 0

longestString = ""

montyPython = 1while montyPython:
    #so this is run every time through the loop
    split_str = triggerSentence.split()#split the sentence into words#code to find the longest word in the trigger sentence inputfor piece in split_str:
        iflen(piece) > longestLength:
            longestString = piece
            longestLength = len(piece)


    listOfSents = gutenberg.sents() #all sentences of gutenberg are assigned -list of list format-

    listOfWords = gutenberg.words()# all words in gutenberg books -list format-# I tip my hat to Mr.Alex Martelli for this part, which helps me find the longest sentence
    lt = longestString.lower() #this line tells you whether word list has the longest word in a case-insensitive way. 

    longestSentence = max((listOfWords for listOfWords in listOfSents ifany(lt == word.lower() for word in listOfWords)), key = len)
    #get longest sentence -list format with every word of sentence being an actual element-

    longestSent=[longestSentence]

    for word in longestSent:#convert the list longestSentence to an actual string
        sstr = " ".join(word)
    print triggerSentence + " "+ sstr
    triggerSentence = sstr

Post a Comment for "Python Code Flow Does Not Work As Expected?"