From 657b0a67cb573d8a487cf366e5523feae2ced194 Mon Sep 17 00:00:00 2001 From: JasterV Date: Sun, 7 Jun 2020 05:46:34 +0200 Subject: [PATCH] first commit --- .gitignore | 142 ++++++++++++++++++++++++++++++++++++++++++++ README.md | 1 + src/colors2vec.py | 26 ++++++++ src/vector_utils.py | 19 ++++++ test/test_vector.py | 39 ++++++++++++ 5 files changed, 227 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 src/colors2vec.py create mode 100644 src/vector_utils.py create mode 100644 test/test_vector.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c0080ff --- /dev/null +++ b/.gitignore @@ -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 don’t 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 \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..2551122 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# Word-Vectors-Basics diff --git a/src/colors2vec.py b/src/colors2vec.py new file mode 100644 index 0000000..8fabde3 --- /dev/null +++ b/src/colors2vec.py @@ -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) + diff --git a/src/vector_utils.py b/src/vector_utils.py new file mode 100644 index 0000000..1384ad7 --- /dev/null +++ b/src/vector_utils.py @@ -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)] diff --git a/test/test_vector.py b/test/test_vector.py new file mode 100644 index 0000000..a90a8db --- /dev/null +++ b/test/test_vector.py @@ -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()