Skip to content

Instantly share code, notes, and snippets.

@vered1986
Last active August 16, 2019 23:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vered1986/e40f15d69f74715fba62e51ab9b76133 to your computer and use it in GitHub Desktop.
Save vered1986/e40f15d69f74715fba62e51ab9b76133 to your computer and use it in GitHub Desktop.
import numpy as np
# Create a very simple unigram LM with a small vocabulary
vocab = ['<s>', 'This', 'LM', 'is', 'stupid', 'cool', '</s>']
word2index = {w: i for i, w in enumerate(vocab)}
probabilities = np.array([[0.01, 0.94, 0.01, 0.01, 0.01, 0.01, 0.01],
[0.01, 0.01, 0.94, 0.01, 0.01, 0.01, 0.01],
[0.01, 0.01, 0.01, 0.94, 0.01, 0.01, 0.01],
[0.01, 0.01, 0.01, 0.01, 0.47, 0.48, 0.01],
[0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.94],
[0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.94],
[0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.94]])
stupid_lm = lambda s: probabilities[word2index.get(s.split()[-1], -1), :]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment