first commit

This commit is contained in:
JasterV 2020-06-07 05:46:34 +02:00
commit 657b0a67cb
5 changed files with 227 additions and 0 deletions

142
.gitignore vendored Normal file
View file

@ -0,0 +1,142 @@
# Created by https://www.toptal.com/developers/gitignore/api/visualstudiocode
# Edit at https://www.toptal.com/developers/gitignore?templates=visualstudiocode
### VisualStudioCode ###
.vscode/*
### VisualStudioCode Patch ###
# Ignore all local history of files
.history
# End of https://www.toptal.com/developers/gitignore/api/visualstudiocode
# Created by https://www.toptal.com/developers/gitignore/api/python
# Edit at https://www.toptal.com/developers/gitignore?templates=python
### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
.python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that dont work, or not
# install all needed dependencies.
#Pipfile.lock
# celery beat schedule file
celerybeat-schedule
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# End of https://www.toptal.com/developers/gitignore/api/python

1
README.md Normal file
View file

@ -0,0 +1 @@
# Word-Vectors-Basics

26
src/colors2vec.py Normal file
View file

@ -0,0 +1,26 @@
import requests
import json
from vector_utils import Vector
def hex_to_int(s):
s = s.lstrip('#')
return [int(s[:2], 16), int(s[2:4], 16), int(s[4:6], 16)]
def get_json(url):
response = requests.get(url)
content = response.content.decode()
data = json.loads(content)
return data
def parse_colors(data_set):
return dict((item['color'], hex_to_int(item['hex'])) for item in data_set['colors'])
if __name__ == '__main__':
data_set_url = "https://raw.githubusercontent.com/dariusk/corpora/master/data/colors/xkcd.json"
data_set = get_json(data_set_url)
colors = parse_colors(data_set)

19
src/vector_utils.py Normal file
View file

@ -0,0 +1,19 @@
import math
from functools import reduce
class Vector:
@staticmethod
def distance(v1, v2):
return math.sqrt(sum((i - j)**2 for i, j in zip(v1, v2)))
@staticmethod
def add(*vecs):
return [i + j for i, j in zip(*vecs)]
@staticmethod
def subs(*vecs):
return [i - j for i, j in zip(*vecs)]
@staticmethod
def mean(*vecs):
return [float(coord) / len(vecs) for coord in reduce(Vector.add, vecs)]

39
test/test_vector.py Normal file
View file

@ -0,0 +1,39 @@
import unittest
from src.vector_utils import Vector
class TestStringMethods(unittest.TestCase):
def test_add(self):
v1 = [2, 3]
v2 = [3, 4]
self.assertEqual([5, 7], Vector.add(v1, v2))
v1 = [5, 12, 32, 12]
v2 = [34, 3, 4, 2]
self.assertEqual([39, 15, 36, 14], Vector.add(v1, v2))
def test_substract(self):
v1 = [2, 3]
v2 = [3, 4]
self.assertEqual([-1, -1], Vector.subs(v1, v2))
v1 = [5, 12, 32, 12]
v2 = [34, 3, 4, 2]
self.assertEqual([-29, 9, 28, 10], Vector.subs(v1, v2))
def test_distance(self):
v1 = [7, 4, 3]
v2 = [17, 6, 2]
self.assertAlmostEqual(10.24695, Vector.distance(v1, v2), places=4)
v1 = [23, 43, 12]
v2 = [45, 21, 10]
self.assertAlmostEqual(31.176915, Vector.distance(v1, v2), places=4)
def test_mean(self):
self.assertEqual([2.0, 2.0], Vector.mean([0, 1], [2, 2], [4, 3]))
if __name__ == '__main__':
unittest.main()