def _calc_log_prob_scores(self) -> List[Union[None, float]]:
"""Get log likelihood scores by calling RNNLM
"""
textfile = tempfile.NamedTemporaryFile(delete=True)
content = '\n'.join([''.join(ts) for ts in self.tss]) + '\n'
textfile.write(str.encode(content))
textfile.seek(0)
command = ['rnnlm',
'-rnnlm',
self.rnnlm_model_path,
'-test',
textfile.name]
process = Popen(command, stdout=PIPE, stderr=PIPE)
output, err = process.communicate()
lines = [line.strip() for line in output.decode('UTF-8').split('\n')
if line.strip() != '']
scores = []
for line in lines:
if line == const.OUT_OF_VOCABULARY:
scores.append(None)
else:
try:
score = float(line)
scores.append(score)
except ValueError:
pass
textfile.close()
return scores
评论列表
文章目录