Skip to content

Commit 8ec2f72

Browse files
committed
Initial commit
0 parents  commit 8ec2f72

File tree

3 files changed

+1136
-0
lines changed

3 files changed

+1136
-0
lines changed

.gitignore

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
env/
12+
build/
13+
develop-eggs/
14+
dist/
15+
downloads/
16+
eggs/
17+
.eggs/
18+
lib/
19+
lib64/
20+
parts/
21+
sdist/
22+
var/
23+
wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
28+
# PyInstaller
29+
# Usually these files are written by a python script from a template
30+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
31+
*.manifest
32+
*.spec
33+
34+
# Installer logs
35+
pip-log.txt
36+
pip-delete-this-directory.txt
37+
38+
# Unit test / coverage reports
39+
htmlcov/
40+
.tox/
41+
.coverage
42+
.coverage.*
43+
.cache
44+
nosetests.xml
45+
coverage.xml
46+
*.cover
47+
.hypothesis/
48+
49+
# Translations
50+
*.mo
51+
*.pot
52+
53+
# Django stuff:
54+
*.log
55+
local_settings.py
56+
57+
# Flask stuff:
58+
instance/
59+
.webassets-cache
60+
61+
# Scrapy stuff:
62+
.scrapy
63+
64+
# Sphinx documentation
65+
docs/_build/
66+
67+
# PyBuilder
68+
target/
69+
70+
# Jupyter Notebook
71+
.ipynb_checkpoints
72+
73+
# pyenv
74+
.python-version
75+
76+
# celery beat schedule file
77+
celerybeat-schedule
78+
79+
# SageMath parsed files
80+
*.sage.py
81+
82+
# dotenv
83+
.env
84+
85+
# virtualenv
86+
.venv
87+
venv/
88+
ENV/
89+
90+
# Spyder project settings
91+
.spyderproject
92+
.spyproject
93+
94+
# Rope project settings
95+
.ropeproject
96+
97+
# mkdocs documentation
98+
/site
99+
100+
# mypy
101+
.mypy_cache/

vectors.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import math
2+
from typing import Iterator, List
3+
4+
Vector = List[float]
5+
6+
class Word:
7+
def __init__(self, word: str, vector: Vector) -> None:
8+
self.word = word
9+
self.vector = vector
10+
11+
def __repr__(self) -> str:
12+
vector_preview = ', '.join(map(str, self.vector[:2]))
13+
return f"{self.word} [{vector_preview}, ...]"
14+
15+
def vector_len(v: Vector) -> float:
16+
return math.sqrt(sum([x*x for x in v]))
17+
18+
def dot_product(v1: Vector, v2: Vector) -> float:
19+
assert len(v1) == len(v2)
20+
return sum([x*y for (x,y) in zip(v1, v2)])
21+
22+
def cosine_similarity(v1: Vector, v2: Vector) -> float:
23+
"""Returns the cosine of the angle between the two vectors."""
24+
return dot_product(v1, v2) / (vector_len(v1) * vector_len(v2))
25+
26+
def load_words(file_name: str) -> List[Word]:
27+
return [
28+
Word('cat', [-0.3, 1.12, 3.1]),
29+
Word('dog', [0.3, -0.6, -0.4])
30+
]
31+
32+
print(vector_len([3, 4]))
33+
34+
print(dot_product([2, 3], [-1, 7]))
35+
36+
print(load_words('words-short.vec'))

0 commit comments

Comments
 (0)