from typing import Counter
import itertools


def build_vocab(texts):
	"""
	Builds a vocabulary mapping from word to index based on the sentences.
	Returns vocabulary mapping and inverse vocabulary mapping.
	"""
	# Build vocabulary
	word_counts = Counter(itertools.chain(*texts))
	# Mapping from index to word
	vocabulary_invariable = [x[0] for x in word_counts.most_common()]
	vocabulary_invariable = list(sorted(vocabulary_invariable))
	# Mapping from word to index
	vocabulary = {x: i for i, x in enumerate(vocabulary_invariable)}
	inverse_vocabulary = {v: k for k, v in vocabulary.items()}
	return [vocabulary, inverse_vocabulary]