<commit_before>from os import listdir

from collections import defaultdict
import pprint


DOC_DIR = 'docs-bosh/'
DOC_EXT = '.html.md.erb'
STOPS_FILE = 'data/stop_words.txt'
STOP_WORDS = [line.strip() for line in open(STOPS_FILE)]


def getDocs(root=DOC_DIR, ext=DOC_EXT):
    for file in listdir(root):
        if file.endswith(ext):
            yield file


def getTitle(doc, root=DOC_DIR):
    with open(DOC_DIR + doc) as f:
        for i, line in enumerate(f):
            if i == 1:
                return [w.strip() for w in line.lower().split(':')][1]


def getWords(text, stops=STOP_WORDS):
    for word in text.split():
        if word not in STOP_WORDS:
            yield word


if __name__ == '__main__':
    index = defaultdict(set)

    for doc in getDocs():
        for word in getWords(getTitle(doc)):
            index[doc].add(word)

    pp = pprint.PrettyPrinter(indent=2)
    pp.pprint(index)<commit_msg>Add buildIndex()<commit_after>from os import listdir

from collections import defaultdict
import pprint


DOC_DIR = 'docs-bosh/'
DOC_EXT = '.html.md.erb'
STOPS_FILE = 'data/stop_words.txt'
STOP_WORDS = [line.strip() for line in open(STOPS_FILE)]


def getDocs(root=DOC_DIR, ext=DOC_EXT):
    for file in listdir(root):
        if file.endswith(ext):
            yield file


def getTitle(doc, root=DOC_DIR):
    with open(DOC_DIR + doc) as f:
        for i, line in enumerate(f):
            if i == 1:
                return [w.strip() for w in line.lower().split(':')][1]


def getWords(text, stops=STOP_WORDS):
    for word in text.split():
        if word not in STOP_WORDS:
            yield word


def buildIndex():
    index = defaultdict(set)
    for doc in getDocs():
        for word in getWords(getTitle(doc)):
            index[doc].add(word)
    return index


if __name__ == '__main__':
    index = buildIndex()
    pp = pprint.PrettyPrinter(indent=2)
    pp.pprint(index)
{'fraction_non_alphanumeric': 0.12048192771084337, 'fraction_numerical': 0.003143006809848088, 'mean_word_length': 2.7377690802348336, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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}}
{'dir': 'git-commits-cleaned', 'id': '4504005', 'n_tokens_mistral': 688, 'n_tokens_neox': 647, 'n_words': 178}