<commit_before>import collections
import difflib

from diff_align import word_diff

def alignment_score(hypothesis, reference):
	'''Generates a metric comparing the quality of a generated alignment
	to a reference alignment.

	The algorithm aligns the generated alignment to the reference alignment,
	ignoring tokens that don't contain a time. Then it runs the diff algorithm
	and returns the number of words correct, inserted, deleted, and substituted
	divided by the number of reference words.
	'''
	if len(reference) == 0:
		raise ValueError('missing reference text')

	def aligned_words(tran):
		for t in tran:
			if t['case'] == 'not-found-in-audio':
				continue
			yield t['alignedWord']
	ref_words = list(aligned_words(reference))
	hyp_words = list(aligned_words(hypothesis))

	counts = collections.defaultdict(float)
	for op, _, _ in word_diff(ref_words, hyp_words):
		counts[op] += 1

	score =  {
		'inserted': counts['insert'],
		'correct': counts['equal'],
		'deleted': counts['delete'],
		'substituted': counts['replace'],
	}
	for k in score.keys():
		score[k] /= len(ref_words)
	score['error'] = score['inserted'] + score['deleted'] + score['substituted']

	return score
<commit_msg>allow using alignment_score from cli<commit_after>import collections
import difflib

from diff_align import word_diff

def alignment_score(hypothesis, reference):
	'''Generates a metric comparing the quality of a generated alignment
	to a reference alignment.

	The algorithm aligns the generated alignment to the reference alignment,
	ignoring tokens that don't contain a time. Then it runs the diff algorithm
	and returns the number of words correct, inserted, deleted, and substituted
	divided by the number of reference words.
	'''
	if len(reference) == 0:
		raise ValueError('missing reference text')

	def aligned_words(tran):
		for t in tran:
			if t['case'] == 'not-found-in-audio':
				continue
			yield t['alignedWord']
	ref_words = list(aligned_words(reference))
	hyp_words = list(aligned_words(hypothesis))

	counts = collections.defaultdict(float)
	for op, _, _ in word_diff(ref_words, hyp_words):
		counts[op] += 1

	score =  {
		'inserted': counts['insert'],
		'correct': counts['equal'],
		'deleted': counts['delete'],
		'substituted': counts['replace'],
	}
	for k in score.keys():
		score[k] /= len(ref_words)
	score['error'] = score['inserted'] + score['deleted'] + score['substituted']

	return score

if __name__=='__main__':
	import argparse
	import json

	parser = argparse.ArgumentParser(
		description='Generate statistics about the quality of an alignment.')
	parser.add_argument('hypothesis', type=argparse.FileType('r'),
		help='generated transcript for testing')
	parser.add_argument('reference', type=argparse.FileType('r'),
		help='ground truth transcript to compare with')

	args = parser.parse_args()

	hypothesis = json.load(args.hypothesis)['words']
	reference = json.load(args.reference)['words']

	score = alignment_score(hypothesis, reference)
	for label, s in score.iteritems():
		print '%15s:  %.2f' % (label, s)
{'fraction_non_alphanumeric': 0.11876640419947507, 'fraction_numerical': 0.0022965879265091863, 'mean_word_length': 5.247950819672131, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5686625', 'n_tokens_mistral': 987, 'n_tokens_neox': 910, 'n_words': 291}