Package: hebrew
Hebrew("בְּרֵאשִׁ֖ית")A python package with methods to handle the complexities of Hebrew text, calculate Gematria, and more.󠀠󠀠Documentation:https://hebrew.aviperl.me/Repository:https://github.com/avi-perl/hebrewInstallation$pipinstallhebrewExampleHebrewassists in working with Hebrew text by providing methods to handle the text according to user-perceived
characteristics. Additionally, methods for common Hebrew text processing are provided.fromhebrewimportHebrewfromhebrew.charsimportHebrewChar,ALEPHhs=Hebrew('בְּרֵאשִׁ֖ית')print(list(hs.graphemes))# ['בְּ', 'רֵ', 'א', 'שִׁ֖', 'י', 'ת']print(hs.text_only())# בראשיתprint(ALEPH)# HebrewChar(char='א', name='Aleph', hebrew_name='אָלֶף', name_alts=['Alef'], hebrew_name_alts=None, final_letter=False)print(HebrewChar.search('bet'))# HebrewChar(char='בּ', name='Bet', hebrew_name='בֵּית', name_alts=None, hebrew_name_alts=None, final_letter=False)GematriaTheHebrewclass includes agematriafunction that can return a value for23different variations of Gematria!fromhebrewimportHebrewfromhebrewimportGematriaTypeshs=Hebrew('בְּרֵאשִׁ֖ית בָּרָ֣א אֱלֹהִ֑ים אֵ֥ת הַשָּׁמַ֖יִם וְאֵ֥ת הָאָֽרֶץ׃')print(hs.gematria())# 2701print(hs.gematria(GematriaTypes.MISPAR_GADOL))# 4631Messy inputs, such as strings with english text mixed in, is supported. However, do be careful to work with sanitized
strings as much as possible.fromhebrewimportHebrewhs1=Hebrew('''Text:          "בְּרֵאשִׁ֖ית בָּרָ֣א אֱלֹהִ֑ים אֵ֥ת הַשָּׁמַ֖יִם וְאֵ֥ת הָאָֽרֶץ׃"Translation:   "When God began to create heaven and earth"''')hs2=Hebrew('בְּרֵאשִׁ֖ית בָּרָ֣א אֱלֹהִ֑ים אֵ֥ת הַשָּׁמַ֖יִם וְאֵ֥ת הָאָֽרֶץ׃')asserths1.gematria()==hs2.gematria()# 2701Major kudos goes toTorahCalcwhose calculator and explanations were critical to
the development of this feature.Numerical ConversionsYou can create a Hebrew object that represents a number using thefrom_numbermethod. This is handy for displaying numbers in Hebrew form.fromhebrewimportHebrewhs1=Hebrew.from_number(2)print(hs1)# ב׳# Add an apostrophe instead of a the unicode gereshhs2=Hebrew.from_number(2,geresh=False)print(hs2)# 'ב# Do not add punctuationhs2=Hebrew.from_number(2,geresh=False)print(hs2)# בCharacter NormalizationHidden among hebrew text can be special characters that are visually identical humans, but are made up of different
unicode characters. However, this can cause issues with presentation when there is no support for these characters.In this case, the first letter is made up of 2 unicode characters,sin with a dotandqamatz. The issue here is the sin. By normalizing the sin with a dot to 2 unicode
characters,שand thedot, the display
will look right!To normalize content, use theHebrew.normalizefunction:fromhebrewimportHebrewhs=Hebrew('שָׂחַקְתִּי כְּמוֹ')assertlen(hs.string)==14assertlen(hs.normalize().string)==18Normalizing YiddishBy default, special yiddish characters such asײ(double Yod) arenotnormalized.
However,ײַ(double Yod with a Patah) will be converted toײַ.To fully "normalize" yiddish characters, passTruetonormalize.Grapheme CharactersHebrew text comes in different forms, depending on the context. Hebrew text may appear with Niqqudot
"a system of diacritical signs used to represent vowels or distinguish between alternative pronunciations of letters
of the Hebrew alphabet".^1Additionally, Hebrew text may appear with extensive punctuation characters that connect
words, separate them, and cantillation marks "used as a guide for chanting the text, either from the printed text or,
in the case of the public reading of the Torah"^2.Because of the above, from the perspective of a hebrew reader, the following 3 words are the same:בְּרֵאשִׁ֖יתבְּרֵאשִׁיתבראשיתHowever, as a unicode string, they are entirely different because of the additional characters.assertlen("בְּרֵאשִׁ֖ית")==12assertlen("בְּרֵאשִׁית")==11assertlen("בראשית")==6This impacts the user is a number of other ways. For example, if I want to get the root of this hebrew word using a slice:Expected:רֵאשִׁ֖יתhe="בְּרֵאשִׁ֖ית"asserthe[-5:]=='ִׁ֖ית'The solution to this is to handle the unicode string as a list of grapheme^3characters, where each letter and its
accompanying characters are treated as a single unit.Working with Grapheme CharactersUsing thegraphemelibrary for python, we can work with the grapheme
characters as units. This allows us to get the right number of characters, slice the string correctly, and more.importgraphemeassertgrapheme.length("בְּרֵאשִׁ֖ית")==6assertgrapheme.slice("בְּרֵאשִׁ֖ית",start=1,end=6)=='רֵאשִׁ֖ית'This library includes 2 classes.GraphemeStringis a class that supports all the functions made available bygrapheme.
The 2nd classHebrewsubclassesGraphemeStringand adds methods for handling Hebrew text. This allows us to
interact with the text like so:fromhebrewimportHebrewv2=Hebrew("וְהָאָ֗רֶץ הָיְתָ֥ה תֹ֙הוּ֙ וָבֹ֔הוּ וְחֹ֖שֶׁךְ עַל־פְּנֵ֣י תְה֑וֹם וְר֣וּחַ אֱלֹהִ֔ים מְרַחֶ֖פֶת עַל־פְּנֵ֥י הַמָּֽיִם׃")print(v2.no_taamim())# "וְהָאָרֶץ הָיְתָה תֹהוּ וָבֹהוּ וְחֹשֶׁךְ עַל־פְּנֵי תְהוֹם וְרוּחַ אֱלֹהִים מְרַחֶפֶת עַל־פְּנֵי הַמָּיִם׃"print(v2.text_only())# והארץ היתה תהו ובהו וחשך על־פני תהום ורוח אלהים מרחפת על־פני המיםassertv2.length==66print(v2.words(split_maqaf=True))# [וְהָאָ֗רֶץ, הָיְתָ֥ה, תֹ֙הוּ֙, וָבֹ֔הוּ, וְחֹ֖שֶׁךְ, עַל, פְּנֵ֣י, תְה֑וֹם, וְר֣וּחַ, אֱלֹהִ֔ים, מְרַחֶ֖פֶת, עַל, פְּנֵ֥י, הַמָּֽיִם׃]The text in these examples and used in testing were sourced fromSefaria.hebrew.charsand Character Constantshebrew.Charscontains constants for every letter as well as lists by character category's.
Each value is an instance of a class that represents a character in the Hebrew character set with relevant properties.
Since this library seeks to support the use of the Hebrew language in the way it is used, characters such as "בּ" can be
located (BET) even though, strictly speaking, "בּ" is not part of the hebrew alphabet; it is a Hebrew letter plus a dot.fromhebrew.charsimportFINAL_LETTERS,YIDDISH_CHARS,TSADIprint(TSADI)# HebrewChar(char='צ', name='Tsadi', hebrew_name='צַדִי', name_alts=['Tzadik'], hebrew_name_alts=['צדיק'], final_letter=False)assert{c.name:c.charforcinFINAL_LETTERS}=={'Chaf Sofit':'ך','Mem Sofit':'ם','Nun Sofit':'ן','Fe Sofit':'ף','Tsadi Sofit':'ץ'}assert[c.charforcinYIDDISH_CHARS]==['ײ','װ','ױ']A letter can be retrieved using theCHARSdict; A dict of all instances of all supported Char types where the key is
the char and the value is an instance of BaseHebrewChar.fromhebrew.charsimportCHARSprint(CHARS.get('בּ'))# HebrewChar(char='בּ', name='Bet', hebrew_name='בֵּית', name_alts=None, hebrew_name_alts=None, final_letter=False)Search is also supported so that letters can be retrieved by their name.fromhebrew.charsimportHebrewCharprint(HebrewChar.search('bet'))# HebrewChar(char='בּ', name='Bet', hebrew_name='בֵּית', name_alts=None, hebrew_name_alts=None, final_letter=False)ContributingContributions in the form of pull requests are very welcome! I'm sure many more helpful methods related to hebrew text
could be helpful. More information and instructions for contributing can be foundhere.