#!/usr/bin/python3
# -*- coding: utf-8 -*-
# 2023/02/12
# Author: DarkRanger <darkranger.no-ip.org>
import os, sys

INPUT_DICT_FILES = ["data.adj", "data.adv", "data.noun", "data.verb", "en_US.dic"]
OUTPUT_DICT_FILE = "dict.txt"

def dict_maker():
    word_list = []
    
    for dict_file in INPUT_DICT_FILES:
        if not os.path.isfile(dict_file):
            print("{0} does not exist.".format(dict_file))
            continue
        
        print("Loading {0}".format(dict_file))
        input_file = open(dict_file, "r")

        for line in input_file.readlines():
            words = []

            if dict_file.endswith(".dic"):
                words.append(line.split("/")[0].strip())
            else:
                items = line.split()

                if len(items) < 5:
                    continue
                if len(items[0]) < 8:
                    continue
                if not items[0].isdigit():
                    continue

                words.append(items[4])
                words.append(items[6])

            for word in words:
                if not word.isalpha():
                    continue
                if word.upper() == word:
                    continue
                if len(word) != 5:
                    continue

                word_list.append(word.lower() + "\n")

    input_file.close()
    output_file = open(OUTPUT_DICT_FILE, "w")
    word_list = list(dict.fromkeys(word_list))
    word_list[-1] = word_list[-1].strip()
    output_file.writelines(word_list)
    output_file.close()
    print("{0} words collected.".format(len(word_list)))
    print("{0} has been saved.".format(OUTPUT_DICT_FILE))

if __name__ == "__main__":
    appdir = os.path.abspath(os.path.dirname(sys.argv[0]))
    os.chdir(appdir)
    dict_maker()