Created
February 26, 2018 17:53
-
-
Save jbaiter/3d781a311e536b471b24fb4a46c952a4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import logging | |
import sys | |
import time | |
from pathlib import Path | |
from gensim.models.fasttext import FastText | |
from gensim.models.word2vec import Text8Corpus | |
def train(target_dir): | |
corpus = Text8Corpus('./text8') | |
model = FastText(corpus) | |
model.save(str(target_dir/'model')) | |
def evaluate(model_dir): | |
model = FastText.load(str(model_dir/'model')) | |
accuracy = model.accuracy('./questions-words.txt') | |
with (model_dir/'accuracy.json').open('wt') as fp: | |
json.dump(accuracy, fp) | |
if __name__ == '__main__': | |
if len(sys.argv) != 3: | |
print('bench.py [train|evaluate] target_dir', file=sys.stderr) | |
sys.exit(1) | |
target_dir = Path(sys.argv[2]) | |
if not target_dir.exists(): | |
target_dir.mkdir() | |
logging.basicConfig(level=logging.INFO) | |
time.sleep(15) | |
if sys.argv[1] == 'train': | |
train(Path(target_dir)) | |
elif sys.argv[1] == 'evaluate': | |
evaluate(Path(target_dir)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment