├── VERSION.txt ├── requirements.txt ├── MANIFEST.in ├── .travis.yml ├── CONTRIBUTING.md ├── README.md ├── .gitignore ├── LICENSE ├── hebrew_numbers ├── tests.py └── __init__.py ├── setup.py └── hebrew-special-numbers-default.yml /VERSION.txt: -------------------------------------------------------------------------------- 1 | 0.2.3 2 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | PyYaml 2 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include hebrew-special-numbers-default.yml 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | python: 3 | - "2.6" 4 | - "2.7" 5 | - "3.3" 6 | - "3.4" 7 | - "3.5" 8 | - "3.6" 9 | script: python -m hebrew_numbers.tests 10 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Python hebrew numbers 2 | 3 | ## Contribution guidelines 4 | 5 | ### quickstart 6 | * clone the repo 7 | * create a python virtualenv 8 | * change into the repo directory 9 | * activate the virtualenv 10 | ```bash 11 | (venv) python-hebrew-numbers$ pip install -e . 12 | (venv) python-hebrew-numbers$ python -m hebrew_numbers.tests 13 | ``` 14 | 15 | ## Publishing to pypi 16 | 17 | increment the version in VERSION.txt 18 | 19 | ``` 20 | rm -rf dist build &&\ 21 | python3 setup.py sdist bdist_wheel --universal &&\ 22 | twine upload dist/* 23 | ``` 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # python-hebrew-numbers 2 | 3 | python library for conversion of hebrew numbers (Gematria) 4 | 5 | ## Usage 6 | 7 | ### installation 8 | 9 | ```bash 10 | pip install python-hebrew-numbers 11 | ``` 12 | 13 | ### gematria_to_int 14 | 15 | ```python 16 | from hebrew_numbers import gematria_to_int 17 | gematria_to_int(u'רח"צ') 18 | # 298 19 | ``` 20 | 21 | ### int_to_gematria 22 | 23 | ```python 24 | from hebrew_numbers import int_to_gematria 25 | int_to_gematria(298) 26 | # u'רח״צ' 27 | int_to_gematria(298, gershayim=False) 28 | # u'רחצ' 29 | ``` 30 | 31 | ## Features 32 | 33 | * conversion from string of letters to an integer number 34 | * conversion of integer to string (using [hebrew-special-numbers](https://github.com/chaimleib/hebrew-special-numbers)) 35 | 36 | ## Contributing 37 | 38 | See the [contribution guide](CONTRIBUTING.md) 39 | 40 | ## Background 41 | 42 | * see https://github.com/hasadna/Open-Knesset/issues/169 43 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | 5 | # C extensions 6 | *.so 7 | 8 | # Distribution / packaging 9 | .Python 10 | env/ 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | *.egg-info/ 23 | .installed.cfg 24 | *.egg 25 | 26 | # PyInstaller 27 | # Usually these files are written by a python script from a template 28 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 29 | *.manifest 30 | *.spec 31 | 32 | # Installer logs 33 | pip-log.txt 34 | pip-delete-this-directory.txt 35 | 36 | # Unit test / coverage reports 37 | htmlcov/ 38 | .tox/ 39 | .coverage 40 | .coverage.* 41 | .cache 42 | nosetests.xml 43 | coverage.xml 44 | *,cover 45 | 46 | # Translations 47 | *.mo 48 | *.pot 49 | 50 | # Django stuff: 51 | *.log 52 | 53 | # Sphinx documentation 54 | docs/_build/ 55 | 56 | # PyBuilder 57 | target/ 58 | 59 | /.idea -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Ori Hoch 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /hebrew_numbers/tests.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import unittest 4 | from . import gematria_to_int, int_to_gematria 5 | 6 | 7 | class TestGematriaToInt(unittest.TestCase): 8 | 9 | def test(self): 10 | for heb, num in ( 11 | (u'א', 1), 12 | (u'ב', 2), 13 | (u'אב', 3), 14 | (u'קצ"ח', 198), 15 | (u'אאא', 3), 16 | ): 17 | self.assertEqual(gematria_to_int(heb), num) 18 | 19 | def test_thousands(self): 20 | self.assertEqual(gematria_to_int(u'ה\'תשע"ז'), 5777) 21 | 22 | 23 | class TestIntToGematria(unittest.TestCase): 24 | 25 | def test_with_gershayim(self): 26 | for heb, num in ( 27 | (u'ב׳', 2), 28 | (u'קצ״ח', 198), 29 | (u'רח״צ', 298), 30 | (u'ט״ז', 16), 31 | ): 32 | self.assertEqual(int_to_gematria(num), heb) 33 | 34 | def test_no_gershayim(self): 35 | for heb, num in ( 36 | (u'ב', 2), 37 | (u'קצח', 198), 38 | (u'רחצ', 298), 39 | (u'טז', 16), 40 | ): 41 | self.assertEqual(int_to_gematria(num, gershayim=False), heb) 42 | 43 | 44 | if __name__ == '__main__': 45 | unittest.main() 46 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import os 4 | import time 5 | import io 6 | 7 | 8 | def read(*paths): 9 | """Read a text file.""" 10 | basedir = os.path.dirname(__file__) 11 | fullpath = os.path.join(basedir, *paths) 12 | contents = io.open(fullpath, encoding='utf-8').read().strip() 13 | return contents 14 | 15 | 16 | try: 17 | from setuptools import setup 18 | except ImportError: 19 | from distutils.core import setup 20 | 21 | if os.path.exists("VERSION.txt"): 22 | # this file can be written by CI tools (e.g. Travis) 23 | with open("VERSION.txt") as version_file: 24 | version = version_file.read().strip().strip("v") 25 | else: 26 | version = str(time.time()) 27 | 28 | 29 | setup( 30 | name='python-hebrew-numbers', 31 | version=version, 32 | author='Ori Hoch', 33 | author_email='ori@uumpa.com', 34 | description='conversion of hebrew numbers (Gimatria)', 35 | long_description=read('README.md'), 36 | url='https://github.com/OriHoch/python-hebrew-numbers', 37 | license='MIT', 38 | packages=('hebrew_numbers',), 39 | install_requires=['PyYaml'], 40 | package_data={ 41 | 'hebrew_numbers': ['../hebrew-special-numbers-default.yml'] 42 | } 43 | ) 44 | -------------------------------------------------------------------------------- /hebrew_numbers/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | from os import path 3 | import io 4 | import yaml 5 | 6 | PROJ_PATH = path.sep.join(__file__.split(path.sep)[:-2]) 7 | DATA_PATH = path.join( 8 | PROJ_PATH, 'hebrew-special-numbers-default.yml') 9 | specialnumbers = yaml.safe_load(io.open(DATA_PATH, encoding='utf8')) 10 | 11 | 12 | MAP = ( 13 | (1, u'א'), 14 | (2, u'ב'), 15 | (3, u'ג'), 16 | (4, u'ד'), 17 | (5, u'ה'), 18 | (6, u'ו'), 19 | (7, u'ז'), 20 | (8, u'ח'), 21 | (9, u'ט'), 22 | (10, u'י'), 23 | (20, u'כ'), 24 | (30, u'ל'), 25 | (40, u'מ'), 26 | (50, u'נ'), 27 | (60, u'ס'), 28 | (70, u'ע'), 29 | (80, u'פ'), 30 | (90, u'צ'), 31 | (100, u'ק'), 32 | (200, u'ר'), 33 | (300, u'ש'), 34 | (400, u'ת'), 35 | (500, u'ך'), 36 | (600, u'ם'), 37 | (700, u'ן'), 38 | (800, u'ף'), 39 | (900, u'ץ') 40 | ) 41 | MAP_DICT = dict([(k, v) for v, k in MAP]) 42 | GERESH = set(("'", '׳')) 43 | 44 | 45 | def gematria_to_int(string): 46 | res = 0 47 | for i, char in enumerate(string): 48 | if char in GERESH and i < len(string)-1: 49 | res *= 1000 50 | if char in MAP_DICT: 51 | res += MAP_DICT[char] 52 | return res 53 | 54 | 55 | # adapted from hebrew-special-numbers documentation 56 | def int_to_gematria(num, gershayim=True): 57 | """convert integers between 1 an 999 to Hebrew numerals. 58 | 59 | - set gershayim flag to False to ommit gershayim 60 | """ 61 | # 1. Lookup in specials 62 | if num in specialnumbers['specials']: 63 | retval = specialnumbers['specials'][num] 64 | return _add_gershayim(retval) if gershayim else retval 65 | 66 | # 2. Generate numeral normally 67 | parts = [] 68 | rest = str(num) 69 | while rest: 70 | digit = int(rest[0]) 71 | rest = rest[1:] 72 | if digit == 0: 73 | continue 74 | power = 10 ** len(rest) 75 | parts.append(specialnumbers['numerals'][power * digit]) 76 | retval = ''.join(parts) 77 | # 3. Add gershayim 78 | return _add_gershayim(retval) if gershayim else retval 79 | 80 | 81 | def _add_gershayim(s): 82 | if len(s) == 1: 83 | s += specialnumbers['separators']['geresh'] 84 | else: 85 | s = ''.join([ 86 | s[:-1], 87 | specialnumbers['separators']['gershayim'], 88 | s[-1:] 89 | ]) 90 | return s 91 | -------------------------------------------------------------------------------- /hebrew-special-numbers-default.yml: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | # 3 | # Copyright (c) 2015 Chaim-Leib Halbert 4 | # 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy 6 | # of this software and associated documentation files (the "Software"), to deal 7 | # in the Software without restriction, including without limitation the rights 8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | # copies of the Software, and to permit persons to whom the Software is 10 | # furnished to do so, subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be included in all 13 | # copies or substantial portions of the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | # SOFTWARE. 22 | # 23 | # To read documentation or to make contributions, visit 24 | # https://github.com/chaimleib/hebrew-special-numbers 25 | 26 | # About This File 27 | # 28 | # This style should contain data sufficient for the majority of cases where 29 | # Hebrew numerals must be generated. 30 | # 31 | # For most cases, the typical algorithm using only the 'numerals' and possibly 32 | # the 'separators' keys is sufficient. However, some special cases not following 33 | # the typical algorithm exist. To cover these, the members of the 'specials' key 34 | # are provided. 35 | # 36 | # If a different style is desired, modifications can be stored in a separate 37 | # YAML file and deep-merged with the contents of this file. See 38 | # 39 | # https://github.com/chaimleib/hebrew-special-numbers 40 | # 41 | # for example code. 42 | 43 | version: 44 | styles: 45 | default: '2.0.0' 46 | order: [] # What order styles were merged. For composite styles 47 | separators: 48 | geresh: ׳ 49 | gershayim: ״ 50 | numerals: 51 | 1: א 52 | 2: ב 53 | 3: ג 54 | 4: ד 55 | 5: ה 56 | 6: ו 57 | 7: ז 58 | 8: ח 59 | 9: ט 60 | 10: י 61 | 20: כ 62 | 30: ל 63 | 40: מ 64 | 50: נ 65 | 60: ס 66 | 70: ע 67 | 80: פ 68 | 90: צ 69 | 100: ק 70 | 200: ר 71 | 300: ש 72 | 400: ת 73 | 500: תק 74 | 600: תר 75 | 700: תש 76 | 800: תת 77 | 900: תתק 78 | specials: 79 | 0: '0' 80 | 15: טו 81 | 16: טז 82 | 115: קטו 83 | 116: קטז 84 | 215: רטו 85 | 216: רטז 86 | 270: ער 87 | 272: ערב 88 | 274: עדר 89 | 275: ערה 90 | 298: רחצ 91 | 304: דש 92 | 315: שטו 93 | 316: שטז 94 | 344: שדמ 95 | 415: תטו 96 | 416: תטז 97 | 515: תקטו 98 | 516: תקטז 99 | 615: תרטו 100 | 616: תרטז 101 | 670: עתר 102 | 672: תערב 103 | 674: עדרת 104 | 698: תרחצ 105 | 715: תשטו 106 | 716: תשטז 107 | 744: תשדמ 108 | 815: תתטו 109 | 816: תתטז 110 | 915: תתקטו 111 | 916: תתקטז 112 | --------------------------------------------------------------------------------