import string import functools import sys from nltk.stem import PorterStemmer def cleanup(word): """ returns the left half of word if there is an apostrophe or a hyphen. """ result = [] for letter in word: if letter not in '\'_-': result.append(letter) return ''.join(result) def count(): counter = {} with open('../Data/alice.txt') as infile: for line in infile: for word in line.strip().split(): word = cleanup(word.lower().strip(string.punctuation)) if len(word)>7: if word in counter: counter[word]+=1 else: counter[word]=1 return counter def present(counter): for word in sorted(counter.keys()): if counter[word]>2: print("{:15s}\t{:5d}".format(word, counter[word])) def count2(): counter = {} porter = PorterStemmer() with open('../Data/alice.txt') as infile: for line in infile: for word in line.strip().split(): word = cleanup(word.lower().strip(string.punctuation)) word = porter.stem(word) if len(word)>5: if word in counter: counter[word]+=1 else: counter[word]=1 return counter @functools.lru_cache def fib(n): sys.setrecursionlimit(10000) if n <= 1: return n else: return fib(n-1)+fib(n-2)