from pathlib import Path
import numpy as np
import matplotlib.pyplot as plt
plt.style.use(str(Path("../figure_style.mplstyle")))
words = np.array(["dog", "cat", "car", "truck", "apple", "pear", "violin", "piano"])
contexts = ["fur", "pet", "paws", "engine", "road", "wheels",
"fruit", "sweet", "tree", "music", "strings", "notes"]
counts = np.array([
[18, 15, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[17, 14, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 16, 12, 18, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 17, 15, 19, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 20, 12, 15, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 18, 14, 13, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 17, 15],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 14, 18],
], dtype=float)
mean = counts.mean(axis=0)
X = counts - mean
U, singular, Vt = np.linalg.svd(X, full_matrices=False)
rank = np.linalg.matrix_rank(X)
print(f"{len(words)} words, {len(contexts)} context features, centered rank {rank}.")