import sys from nltk.corpus import stopwords from nltk.tokenize import sent_tokenize STOP_WORDS = set(stopwords.words('english')) def get_words(txt): return filter( lambda x: x not in STOP_WORDS, re.findall(r'\b(\w+)\b', txt) ) def parse_sentence_words(input_file_names): """Returns a list of a list of words. Each sublist is a sentence.""" sentence_words = [] for file_name in input_file_names: for line in open(file_name): line = line.strip().lower() line = line.decode('unicode_escape').encode('ascii','ignore') sent_words = map(get_words, sent_tokenize(line)) sent_words = filter(lambda sw: len(sw) > 1, sent_words) if len(sent_words) > 1: sentence_words += sent_words return sentence_words # You would see five .txt files after unzip 'a_song_of_ice_and_fire.zip' input_file_names = ["001ssb.txt", "002ssb.txt", "003ssb.txt", "004ssb.txt", "005ssb.txt"] GOT_SENTENCE_WORDS= parse_sentence_words(input_file_names)