<jupyter_start><jupyter_text>Just run these once, following [importing tlg and phi5](http://docs.cltk.org/en/latest/importing_corpora.htmlimporting-a-corpus), then processing with [tlgu](http://docs.cltk.org/en/latest/greek.htmlconverting-tlg-texts-with-tlgu).  Lemmatize corpora  Greek (TLG)<jupyter_code>import os
import re
import time

from cltk.corpus.utils.formatter import assemble_tlg_author_filepaths
from cltk.corpus.utils.formatter import tlg_plaintext_cleanup
from cltk.stem.lemma import LemmaReplacer
from cltk.stop.greek.stops import STOPS_LIST as greek_stops
from greek_accentuation.characters import base  # pip install greek-accentuation
# Import the Greek models dir, which contains (among other things) the lemmatizer mappings
from cltk.corpus.utils.importer import CorpusImporter
corpus_importer = CorpusImporter('greek')
corpus_importer.import_corpus('greek_models_cltk')
# This takes ~25 mins on a good server

# make working dir
user_dir = os.path.expanduser('~/cltk_data/user_data/tlg_lemmatized_no_accents_no_stops')
if not os.path.isdir(user_dir):
    os.makedirs(user_dir, exist_ok=True)

# rm numbers (eg, αʹβʹ)
comp_numbers = re.compile(r'.ʹ+?')

# load lemmatizer map into memory
# http://docs.cltk.org/en/latest/greek.html#lemmatization
lemmatizer = LemmaReplacer('greek')

# get filepaths
# http://docs.cltk.org/en/latest/greek.html#tlg-indices
filepaths = assemble_tlg_author_filepaths()

# open each original file, clean, lemmatize, and write into new file
for filepath in filepaths:
    t0 = time.time()
    # open original
    with open(filepath) as fo:
        text = fo.read()
    
    # cleanup tlg texts
    # http://docs.cltk.org/en/latest/greek.html#text-cleanup
    text_cleaned = tlg_plaintext_cleanup(text, rm_punctuation=True, rm_periods=True)
    
    # rm numbers
    text_cleaned = comp_numbers.sub('', text_cleaned)
    
    # do lemmatization
    text_cleaned = text_cleaned.lower()
    tokens = lemmatizer.lemmatize(text_cleaned, return_string=False)
    
    # rm stops
    # http://docs.cltk.org/en/latest/greek.html#stopword-filtering
    tokens = [w for w in tokens if not w in greek_stops]
    
    # rm words less than 3 chars
    tokens = [w for w in tokens if len(w) > 2]
    
    #TODO: rm accents
    tokens_no_accents = []
    for word in tokens:
        word_no_accents = []
        word = ''.join([base(char) for char in word])
        tokens_no_accents.append(word)

    #TODO: ''.join()
    text_no_accents = ' '.join(tokens_no_accents)

    # write file
    file_name = os.path.split(filepath)[1]
    lemmatized_fp = os.path.join(user_dir, file_name)
    with open(lemmatized_fp, 'w') as fo:
        fo.write(text_no_accents)
#     print('Processing time for {0}: {1} secs.'.format(filepath, time.time() - t0))
# now see the texts lemmatized and with all markup, and stopwords, removed

with open('/root/cltk_data/user_data/tlg_lemmatized_no_accents_no_stops/TLG0007.TXT') as file_open:
    text_snippet = file_open.read()[:1500]
print(text_snippet)<jupyter_output>ωσπερ γεωγραφιαις ειμι σοσσιε σενεκιων ιστορικοι διαφευγοντα γιγνωσκω αὐτος εσχατοις μερεσι πινακων πιεζω αιτιας παραγραφουσιν επεκεινα θις ανυδρος θηριωδεις πηλος αιδνης σκυθικον κρυος πελαγος πεπηγος εμοι βιων παραλληλων γραφην εφικτον εικοτι λογω βασιμον ιστορια πραγματων εχομενη χρονον διελθοντι ανωτερω καλος εχω ειπον επεκεινα τερατωδη τραγικα ποιηται μυθογραφοι νεμονται ουκετ εχω πιστιν σαφηνειαν επει λυκουργου νομοθετου νομαζω βασιλεως λογον εκδοντες δοκεω οὐ αλογως ρωμυλω προσαναβαινω πλησιον χρονων αὐτος ιστορια γεγονοτες σκοπαω εγω τοιοσδε φωτι ξυμβησεται τιν αντιταξω φερεγγυος εφαινετο καλεω αοιδιμων οικιστην αθηνη ανθιστημι παραβαλλω πατρι ανικητου μεγαλοδοξου ρωμης ειμι οὖν εγω εκκαθαιρομενον λογω μυθωδης υπακουω λαμβανω ιστοριας οψις οπου αὐθαδης πιθανος περιφρονεω δεχηται εικος μεῖξις ευγνωμονων ακροατης δεησομεθα πρᾶος αρχαιολογιαν προσδεχομενων εδοκει οὖν θησευς ρωμυλω πολλας εναρμοττειν ομοιοτητας αμφω ανεγγυω σκοτιω γενομενοι δοξαν εχω θεαω γεγονεναι αμφω α[...]<jupyter_text>Latin (phi5)<jupyter_code>from cltk.corpus.utils.formatter import assemble_phi5_author_filepaths
from cltk.corpus.utils.formatter import phi5_plaintext_cleanup
# make working dir
user_dir = os.path.expanduser('~/cltk_data/user_data/phi5_lemmatized')
if not os.path.isdir(user_dir):
    os.makedirs(user_dir, exist_ok=True)

# load lemmatizer map into memory
# http://docs.cltk.org/en/latest/latin.html#lemmatization
lemmatizer = LemmaReplacer('latin')

# get filepaths
# http://docs.cltk.org/en/latest/latin.html#phi-indices
filepaths = assemble_phi5_author_filepaths()

# open each original file, clean, lemmatize, and write into new file
for filepath in filepaths:
    # open original
    with open(filepath) as fo:
        text = fo.read()
    
    # cleanup phi5 texts
    # http://docs.cltk.org/en/latest/latin.html#text-cleanup
    text_cleaned = phi5_plaintext_cleanup(text, rm_punctuation=True, rm_periods=False)
    
    # do lemmatization
    text_lemmatized = lemmatizer.lemmatize(text_cleaned, return_string=True)
    
    # write file
    file_name = os.path.split(filepath)[1]
    lemmatized_fp = os.path.join(user_dir, file_name)
    with open(lemmatized_fp, 'w') as fo:
        fo.write(text_lemmatized)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08943396226415094, 'fraction_numerical': 0.004905660377358491, 'mean_word_length': 4.603594080338266, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '660533', 'license': 'MIT', 'n_tokens_mistral': 2597, 'n_tokens_neox': 2198, 'n_words': 445, 'path': 'public_talks/2016_10_26_harvard/1 Preprocessing.ipynb', 'repo_name': 'kylepjohnson/ipython_notebooks', 'size': '5300'}