# coding: utf-8

from __future__ import print_function
import re
import os


# Use Linux dict to valida data
with open('/usr/share/dict/american-english', 'r') as f:
    DICT = set(f.read().split())


def get_words_from_dir(dirpath):
    file_list = []
    words = []
    for root, dirs, files in os.walk(dirpath):
        [file_list.append(os.path.join(root, f)) for f in files]
    for f in file_list:
        with open(f, 'r') as f:
            words.extend(re.findall(r'[a-zA-Z]+', f.read()))
    words = [i.lower() for i in words]
    return set(words) & set(DICT)


def get_words_from_file(filepath):
    with open(filepath) as f:
        words = re.findall(r'[a-zA-Z]+', f.read())
    words = [i.lower() for i in words]
    return set(words) & set(DICT)


def write_to_file(filepath, data):
    with open(filepath, 'w') as f:
        f.write('\n'.join(sorted(data)))


if __name__ == '__main__':
    # write_to_file('pydoc2.txt', get_words_from_dir('py2/'))
    # write_to_file('college4.txt', get_words_from_file('4.txt'))
