#!/usr/bin/python3 import os.path import gzip import urllib.request # слова вводим в единственном числе не в женском роде # т.е. чтобы окончание состояло из двух букв words = ['guten', ] def roots(word): # выбираем корни, которые могут быть у этого слова res = [] word = word.lower() if word.endswith('esten'): res.append(word[:-5]) if word.endswith('este'): res.append(word[:-4]) if word.endswith(('ten', 'ere', 'ste')): res.append(word[:-3]) if word.endswith(('st', 'er', 'en', 'em', 'es')): res.append(word[:-2]) if word.endswith('e'): res.append(word[:-1]) return res for word in words: print("Searching for {}".format(word)) url = "http://storage.googleapis.com/books/ngrams/books/googlebooks-ger-all-2gram-20120701-{}.gz".format(word[:2]) print("Downloading googlebooks-ger-all-2gram-20120701-{}.gz...".format(word[:2])) urllib.request.urlretrieve(url, "data.csv") f = gzip.open("data.csv") res = {} print("Analyzing googlebooks-ger-all-2gram-20120701-{}.gz...".format(word[:2])) for line_raw in f: line = line_raw.decode('utf-8') #print(type(line)) gram, year, count, tom = line.split('\t') word1, word2 = gram.split(' ') #print(roots(word1), '===', word.lower()[-2:]) if word.lower()[:-2] in roots(word1): if word2 not in res: res[word2] = 0 res[word2] += int(count) print('Found match! {} ({} times)'.format(gram, count)) f.close() os.remove("data.csv") if not os.path.exists(word[:2]): os.mkdir(word[:2]) f = open(os.path.join(word[:2], word), 'w') for key in res: f.write("{};{}\n".format(key, res[key])) f.close()