To start you off (untested):
def find_longest_word(trigger_sentence):
""" 2. = find the longest word in the trigger sentence
"""
## container for the longest word or words
longest_list = [0]
## assumes no punctuation
for word in trigger_sentence.split():
## new word found so re-initialize the list
if len(word) > longest_list[0]:
longest_list = [len(word), word]
elif len(word) == longest_list[0]:
longest_list.append(word)
## element[0] = length, [1]+ = word or words
return longest_list
If you find that you have to access this function more than once, then change the list to a dictionary, with key=word length pointing to a list of words, then if you want the next longest word, you have it without going through the sentence again.
def find_sentence(longest_word_list, list_of_sents):
"""3- Find all the sentences of the corpus that contain the word at step2
"""
sentences_found = []
for sentence in list_of_sentences:
## if we are looking for "the" we do not want "there", so split the sentence
sentence_list = sentence.split()
## multiple longest words can be found in the same sentence
found = False
for word in longest_word_list[1:]:
if (not found) and (word in sentence_list):
found = True
sentences_found.append(sentence)
return sentences_found