Last active
April 27, 2020 02:49
-
-
Save ShigekiKarita/3b81a34b4530cf7cebb6754e30876985 to your computer and use it in GitHub Desktop.
This file contains 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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0 <s>\n", | |
"5.98510933 ABOUT\n", | |
"5.46656704 HE\n", | |
"4.97864962 SAID\n", | |
"3.17434382 </s>\n", | |
"40.5448189\n" | |
] | |
} | |
], | |
"source": [ | |
"# You need Kaldi and WSJ corpus to create this FST-based LM\n", | |
"# $ gunzip -c 13-32.1/wsj1/doc/lng_modl/base_lm/tcb20onp.z | arpa2fst - lm.fst\n", | |
"# $ pip install openfst-python\n", | |
"import openfst_python as fst\n", | |
"\n", | |
"\n", | |
"class FSTScorer:\n", | |
" def __init__(self, input, space=\" \", unk_label=\"<UNK>\"):\n", | |
" if isinstance(input, str):\n", | |
" self.fst = fst.Fst.read(input)\n", | |
" else:\n", | |
" self.fst = input\n", | |
" self.space = space\n", | |
" self.word2id = dict()\n", | |
" self.id2word = dict()\n", | |
" for k, v in self.fst.input_symbols():\n", | |
" assert v not in self.word2id\n", | |
" self.id2word[k] = v\n", | |
" self.word2id[v] = k\n", | |
" self.unk_ilabel = self.word2id[unk_label]\n", | |
" self.unk_score = fst.Weight(self.fst.weight_type(), self.word2id[unk_label])\n", | |
"\n", | |
" def arc_weight(self, score, state, dst):\n", | |
" for a in self.fst.arcs(state):\n", | |
" if a.ilabel == dst:\n", | |
" print(a.weight, self.id2word[a.ilabel])\n", | |
" return fst.times(score, a.weight), a.nextstate\n", | |
" if a.ilabel == 0:\n", | |
" return self.arc_weight(fst.times(score, a.weight), a.nextstate, dst)\n", | |
" # assert False\n", | |
" return self.arc_weight(score, state, self.unk_ilabel)\n", | |
"\n", | |
" def score_isym(self, isyms):\n", | |
" state = self.fst.start()\n", | |
" ret = fst.Weight.One(self.fst.weight_type())\n", | |
" for i in isyms:\n", | |
" ret, state = self.arc_weight(ret, state, i)\n", | |
" return float(ret)\n", | |
"\n", | |
" def score_str(self, s):\n", | |
" return self.score_isym((self.word2id.get(i, self.unk_ilabel) for i in s.split(self.space)))\n", | |
"\n", | |
"\n", | |
"scorer = FSTScorer(\"./lm_tgpr_5k.fst\")\n", | |
"print(scorer.score_str(\"<s> ABOUT HE SAID </s>\"))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment