2015-04-08 09:20:15 +03:00
|
|
|
"""Set up a model directory.
|
|
|
|
|
|
|
|
Requires:
|
|
|
|
|
|
|
|
lang_data --- Rules for the tokenizer
|
|
|
|
* prefix.txt
|
|
|
|
* suffix.txt
|
|
|
|
* infix.txt
|
|
|
|
* morphs.json
|
|
|
|
* specials.json
|
|
|
|
|
|
|
|
corpora --- Data files
|
|
|
|
* WordNet
|
|
|
|
* words.sgt.prob --- Smoothed unigram probabilities
|
|
|
|
* clusters.txt --- Output of hierarchical clustering, e.g. Brown clusters
|
|
|
|
* vectors.tgz --- output of something like word2vec
|
|
|
|
"""
|
2015-04-08 08:46:53 +03:00
|
|
|
import plac
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
from shutil import copyfile
|
2015-04-12 05:45:31 +03:00
|
|
|
from shutil import copytree
|
2015-04-08 08:46:53 +03:00
|
|
|
import codecs
|
2015-07-01 19:48:59 +03:00
|
|
|
from collections import defaultdict
|
2015-07-04 18:24:32 +03:00
|
|
|
import json
|
2015-04-08 08:46:53 +03:00
|
|
|
|
|
|
|
from spacy.en import get_lex_props
|
2015-07-01 19:48:59 +03:00
|
|
|
from spacy.en.lemmatizer import Lemmatizer
|
2015-04-08 08:46:53 +03:00
|
|
|
from spacy.vocab import Vocab
|
2015-04-08 09:20:15 +03:00
|
|
|
from spacy.vocab import write_binary_vectors
|
2015-04-08 08:46:53 +03:00
|
|
|
|
2015-07-03 14:28:39 +03:00
|
|
|
from spacy.parts_of_speech import NOUN, VERB, ADJ, ADV
|
2015-07-01 19:48:59 +03:00
|
|
|
|
|
|
|
import spacy.senses
|
2015-07-04 18:24:32 +03:00
|
|
|
from spacy.munge import read_wordnet
|
2015-07-01 19:48:59 +03:00
|
|
|
|
2015-04-08 08:46:53 +03:00
|
|
|
|
|
|
|
def setup_tokenizer(lang_data_dir, tok_dir):
|
|
|
|
if not tok_dir.exists():
|
|
|
|
tok_dir.mkdir()
|
|
|
|
|
|
|
|
for filename in ('infix.txt', 'morphs.json', 'prefix.txt', 'specials.json',
|
|
|
|
'suffix.txt'):
|
|
|
|
src = lang_data_dir / filename
|
|
|
|
dst = tok_dir / filename
|
|
|
|
if not dst.exists():
|
2015-04-12 05:45:31 +03:00
|
|
|
copyfile(str(src), str(dst))
|
2015-04-08 08:46:53 +03:00
|
|
|
|
|
|
|
|
|
|
|
def _read_clusters(loc):
|
|
|
|
clusters = {}
|
|
|
|
for line in codecs.open(str(loc), 'r', 'utf8'):
|
|
|
|
try:
|
|
|
|
cluster, word, freq = line.split()
|
|
|
|
except ValueError:
|
|
|
|
continue
|
2015-04-17 05:44:52 +03:00
|
|
|
# If the clusterer has only seen the word a few times, its cluster is
|
|
|
|
# unreliable.
|
|
|
|
if int(freq) >= 3:
|
|
|
|
clusters[word] = cluster
|
2015-04-29 15:23:13 +03:00
|
|
|
else:
|
|
|
|
clusters[word] = '0'
|
2015-05-31 06:50:50 +03:00
|
|
|
# Expand clusters with re-casing
|
|
|
|
for word, cluster in clusters.items():
|
|
|
|
if word.lower() not in clusters:
|
|
|
|
clusters[word.lower()] = cluster
|
|
|
|
if word.title() not in clusters:
|
|
|
|
clusters[word.title()] = cluster
|
2015-05-31 16:21:28 +03:00
|
|
|
if word.upper() not in clusters:
|
2015-05-31 06:50:50 +03:00
|
|
|
clusters[word.upper()] = cluster
|
2015-04-08 08:46:53 +03:00
|
|
|
return clusters
|
|
|
|
|
|
|
|
|
|
|
|
def _read_probs(loc):
|
|
|
|
probs = {}
|
|
|
|
for i, line in enumerate(codecs.open(str(loc), 'r', 'utf8')):
|
|
|
|
prob, word = line.split()
|
|
|
|
prob = float(prob)
|
|
|
|
probs[word] = prob
|
|
|
|
return probs
|
|
|
|
|
|
|
|
|
2015-07-01 19:48:59 +03:00
|
|
|
def _read_senses(loc):
|
|
|
|
lexicon = defaultdict(lambda: defaultdict(list))
|
2015-07-05 11:50:02 +03:00
|
|
|
pos_tags = [None, NOUN, VERB, ADJ, None, None]
|
2015-07-01 19:48:59 +03:00
|
|
|
for line in codecs.open(str(loc), 'r', 'utf8'):
|
2015-07-03 14:28:39 +03:00
|
|
|
sense_key, synset_offset, sense_number, tag_cnt = line.split()
|
|
|
|
lemma, lex_sense = sense_key.split('%')
|
|
|
|
ss_type, lex_filenum, lex_id, head_word, head_id = lex_sense.split(':')
|
|
|
|
pos = pos_tags[int(ss_type)]
|
2015-07-05 11:50:02 +03:00
|
|
|
lexicon[lemma][pos].append(int(lex_filenum) + 1)
|
2015-07-01 19:48:59 +03:00
|
|
|
return lexicon
|
|
|
|
|
|
|
|
|
2015-04-08 08:46:53 +03:00
|
|
|
def setup_vocab(src_dir, dst_dir):
|
|
|
|
if not dst_dir.exists():
|
|
|
|
dst_dir.mkdir()
|
2015-04-08 09:20:15 +03:00
|
|
|
|
|
|
|
vectors_src = src_dir / 'vectors.tgz'
|
|
|
|
if vectors_src.exists():
|
|
|
|
write_binary_vectors(str(vectors_src), str(dst_dir / 'vec.bin'))
|
2015-04-08 08:46:53 +03:00
|
|
|
vocab = Vocab(data_dir=None, get_lex_props=get_lex_props)
|
|
|
|
clusters = _read_clusters(src_dir / 'clusters.txt')
|
2015-07-03 14:28:39 +03:00
|
|
|
senses = _read_senses(src_dir / 'wordnet' / 'index.sense')
|
2015-04-08 08:46:53 +03:00
|
|
|
probs = _read_probs(src_dir / 'words.sgt.prob')
|
2015-07-01 19:48:59 +03:00
|
|
|
for word in set(clusters).union(set(senses)):
|
2015-05-31 06:46:16 +03:00
|
|
|
if word not in probs:
|
|
|
|
probs[word] = -17.0
|
2015-07-01 19:48:59 +03:00
|
|
|
lemmatizer = Lemmatizer(str(src_dir / 'wordnet'), NOUN, VERB, ADJ)
|
2015-04-08 08:46:53 +03:00
|
|
|
lexicon = []
|
|
|
|
for word, prob in reversed(sorted(probs.items(), key=lambda item: item[1])):
|
|
|
|
entry = get_lex_props(word)
|
2015-07-05 12:31:07 +03:00
|
|
|
if word in clusters or word in senses or float(prob) >= -17:
|
2015-04-08 08:46:53 +03:00
|
|
|
entry['prob'] = float(prob)
|
|
|
|
cluster = clusters.get(word, '0')
|
|
|
|
# Decode as a little-endian string, so that we can do & 15 to get
|
|
|
|
# the first 4 bits. See _parse_features.pyx
|
|
|
|
entry['cluster'] = int(cluster[::-1], 2)
|
2015-07-01 19:48:59 +03:00
|
|
|
orth_senses = set()
|
2015-07-05 11:50:02 +03:00
|
|
|
orth_senses.update(senses[word.lower()][None])
|
2015-07-01 19:48:59 +03:00
|
|
|
for pos in [NOUN, VERB, ADJ]:
|
|
|
|
for lemma in lemmatizer(word.lower(), pos):
|
|
|
|
orth_senses.update(senses[lemma][pos])
|
|
|
|
entry['senses'] = list(sorted(orth_senses))
|
2015-04-08 08:46:53 +03:00
|
|
|
vocab[word] = entry
|
|
|
|
vocab.dump(str(dst_dir / 'lexemes.bin'))
|
|
|
|
vocab.strings.dump(str(dst_dir / 'strings.txt'))
|
|
|
|
|
|
|
|
|
2015-07-05 11:50:02 +03:00
|
|
|
|
2015-04-08 09:20:15 +03:00
|
|
|
def main(lang_data_dir, corpora_dir, model_dir):
|
2015-04-08 08:46:53 +03:00
|
|
|
model_dir = Path(model_dir)
|
|
|
|
lang_data_dir = Path(lang_data_dir)
|
2015-04-08 09:20:15 +03:00
|
|
|
corpora_dir = Path(corpora_dir)
|
|
|
|
|
|
|
|
assert corpora_dir.exists()
|
|
|
|
assert lang_data_dir.exists()
|
2015-04-08 08:46:53 +03:00
|
|
|
|
|
|
|
if not model_dir.exists():
|
|
|
|
model_dir.mkdir()
|
|
|
|
|
|
|
|
setup_tokenizer(lang_data_dir, model_dir / 'tokenizer')
|
2015-04-08 09:20:15 +03:00
|
|
|
setup_vocab(corpora_dir, model_dir / 'vocab')
|
2015-07-05 22:03:59 +03:00
|
|
|
|
2015-04-08 09:20:15 +03:00
|
|
|
if not (model_dir / 'wordnet').exists():
|
|
|
|
copytree(str(corpora_dir / 'wordnet'), str(model_dir / 'wordnet'))
|
2015-07-04 18:24:32 +03:00
|
|
|
ss_probs = read_wordnet.make_supersense_dict(str(corpora_dir / 'wordnet'))
|
2015-07-05 22:03:59 +03:00
|
|
|
with codecs.open(str(model_dir / 'wordnet' / 'supersenses.json'), 'w', 'utf8') as file_:
|
2015-07-04 18:24:32 +03:00
|
|
|
json.dump(ss_probs, file_)
|
2015-04-08 08:46:53 +03:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
plac.call(main)
|