File size: 1,937 Bytes
1b0882b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import gzip
from collections import defaultdict
from spellchecker import SpellChecker
import json
from tqdm import tqdm

config = [
    {'title': 'English', 'lang': 'en', 'out': 'out/en.tsv'},
    {'title': 'Spanish', 'lang': 'es', 'out': 'out/es.tsv'},
    {'title': 'French', 'lang': 'fr', 'out': 'out/fr.tsv'},
    {'title': 'German', 'lang': 'de', 'out': 'out/de.tsv'},
    {'title': 'Russian', 'lang': 'ru', 'out': 'out/ru.tsv'}
]


def get_file_dict(file_path, language):
    spell = SpellChecker(language=language)
    count_dict = defaultdict(int)
    with gzip.open(file_path, mode="rt") as f:
        file_content = f.readlines()

        for line in tqdm(file_content):
            parts = [el for el in line.split('\t')]
            word = parts[0].split("_")[0]

            if word in spell.known([word]):
                count_dict[word] += int(parts[2])

    return count_dict


def url_to_path(url):
    parts = url.rsplit('/', 1)
    return 'data/' + parts[-1]


def get_paths(title):
    with open('raw/dict.jsonl', 'r') as f:
        items = [json.loads(line) for line in f]

        links = [
            link['url']
            for item in items if item['title'] == title
            for section in item['sections'] if section['name'] == '1-grams'
            for link in section['links'] if link['name'].isalpha() and len(link['name']) == 1
        ]

        return list(map(url_to_path, links))


for single_set in config:
    set_dict = defaultdict(int)

    paths = get_paths(single_set['title'])

    print(paths)

    # continue

    for path in tqdm(paths):
        new_dict = get_file_dict(path, single_set['lang'])
        set_dict.update(new_dict)

    with open(single_set['out'], "a") as f:
        for key, value in set_dict.items():
            line = f"{key}\t{value}\n"
            f.write(line)

    # print(set_dict)


# print(get_file_dict('data/googlebooks-eng-all-1gram-20120701-a.gz', 'en'))