import os 
import textstat
import csv

readme_wd = "/data/users/mgaughan/kkex/time_specific_files/partitioned_readme"
contributing_wd = "/data/users/mgaughan/kkex/time_specific_files/partitioned_contributing"

csv_fieldnames = ['subdir', 'filename', 'flesch_reading_ease', 'flesch_kincaid_grade', 'linsear_write_formula', 'dale_chall_readability_score', 'mcalpine_eflaw', 'reading_time', 'char_count', 'word_count']

'''
getting readability scoring for each type of document
'''
def generate_file(output_csv, wdirectory, document_type):
    with open(output_csv, 'w') as csvfile: 
        writer = csv.DictWriter(csvfile, fieldnames = csv_fieldnames) 
        writer.writeheader() 
        subdirs = os.listdir(wdirectory)
        print(document_type)
        for dir in subdirs: 
            print(dir)
            files = os.listdir(wdirectory + "/" + dir)
            count = 0
            for file in files:
                file_dict = {"subdir": dir, "filename": file}
                print(file)
                full_address = wdirectory + "/" + dir + "/" + file
                file_dict = get_readibility(full_address, file_dict)
                writer.writerow(file_dict)
'''
gets the 3 readability scores for each individual textfile
'''
def get_readibility(file_address, file_dict):
    file = open(file_address, "r")
    document = file.read()
    file_dict['flesch_reading_ease'] = textstat.flesch_reading_ease(document)
    file_dict['flesch_kincaid_grade'] = textstat.flesch_kincaid_grade(document)
    file_dict['linsear_write_formula'] = textstat.linsear_write_formula(document)
    file_dict['dale_chall_readability_score'] = textstat.dale_chall_readability_score(document)
    file_dict['mcalpine_eflaw'] = textstat.mcalpine_eflaw(document)
    file_dict['reading_time']  = textstat.reading_time(document, ms_per_char=14.69)
    file_dict['char_count'] = textstat.char_count(document, ignore_spaces=True)
    file_dict['word_count'] = textstat.lexicon_count(document, removepunct=True)
    return file_dict

if __name__ == "__main__":
    #generate_file('draft_readability_readme.csv', readme_wd, "readme")
    generate_file('draft_readability_contributing.csv', contributing_wd, "contributing")