2014-07-07 02:02:55 +04:00
|
|
|
import os
|
|
|
|
from os import path
|
|
|
|
import codecs
|
2014-08-21 18:37:13 +04:00
|
|
|
import json
|
2014-07-07 02:02:55 +04:00
|
|
|
|
|
|
|
DATA_DIR = path.join(path.dirname(__file__), '..', 'data')
|
|
|
|
|
|
|
|
|
2014-07-05 22:51:42 +04:00
|
|
|
def utf8open(loc, mode='r'):
|
|
|
|
return codecs.open(loc, mode, 'utf8')
|
|
|
|
|
|
|
|
|
|
|
|
def load_case_stats(data_dir):
|
2014-07-07 09:36:43 +04:00
|
|
|
case_loc = path.join(data_dir, 'case')
|
2014-07-05 22:51:42 +04:00
|
|
|
case_stats = {}
|
|
|
|
with utf8open(case_loc) as cases_file:
|
|
|
|
for line in cases_file:
|
|
|
|
word, upper, title = line.split()
|
|
|
|
case_stats[word] = (float(upper), float(title))
|
|
|
|
return case_stats
|
|
|
|
|
|
|
|
|
2014-08-21 18:37:13 +04:00
|
|
|
def read_dist_info(lang):
|
|
|
|
dist_path = path.join(DATA_DIR, lang, 'distribution_info.json')
|
|
|
|
if path.exists(dist_path):
|
|
|
|
with open(dist_path) as file_:
|
|
|
|
dist_info = json.load(file_)
|
|
|
|
else:
|
|
|
|
dist_info = {}
|
2014-08-19 04:40:37 +04:00
|
|
|
return dist_info
|
|
|
|
|
|
|
|
|
2014-07-07 02:02:55 +04:00
|
|
|
def read_tokenization(lang):
|
|
|
|
loc = path.join(DATA_DIR, lang, 'tokenization')
|
2014-07-05 22:51:42 +04:00
|
|
|
entries = []
|
|
|
|
seen = set()
|
2014-07-07 02:02:55 +04:00
|
|
|
with utf8open(loc) as file_:
|
|
|
|
for line in file_:
|
2014-07-05 22:51:42 +04:00
|
|
|
line = line.strip()
|
|
|
|
if line.startswith('#'):
|
|
|
|
continue
|
|
|
|
if not line:
|
|
|
|
continue
|
2014-07-07 02:02:55 +04:00
|
|
|
pieces = line.split()
|
|
|
|
chunk = pieces.pop(0)
|
|
|
|
assert chunk not in seen, chunk
|
|
|
|
seen.add(chunk)
|
2014-08-18 21:14:00 +04:00
|
|
|
entries.append((chunk, list(pieces)))
|
2014-07-07 07:07:21 +04:00
|
|
|
if chunk[0].isalpha() and chunk[0].islower():
|
|
|
|
chunk = chunk[0].title() + chunk[1:]
|
2014-08-18 21:14:00 +04:00
|
|
|
pieces[0] = pieces[0][0].title() + pieces[0][1:]
|
2014-07-07 07:07:21 +04:00
|
|
|
seen.add(chunk)
|
2014-08-18 21:14:00 +04:00
|
|
|
entries.append((chunk, pieces))
|
2014-07-05 22:51:42 +04:00
|
|
|
return entries
|