Created
December 7, 2021 07:47
-
-
Save elsanussi-s-mneina/c430dcd67d4b5e6e77e9755b1aa8e8a3 to your computer and use it in GitHub Desktop.
Here we transliterate Hebrew text from Hebrew script to Arabic script, all letters of the Hebrew alphabet, plus 3 vowels are transliterated.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright 2021 Elsanussi Mneina (youtube.com/GrassDewMorning) | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
package main | |
import ( | |
"fmt" | |
"strings" | |
) | |
const HEBREW_COL = 0 | |
const ARABIC_COL = 1 | |
// turn demo mode on, if you cannot give user input | |
// For example at the Go playground at go.dev/play | |
const DEMO_MODE bool = true | |
// Transliteration table was made using | |
// a spreadsheet program, | |
// and the spreadsheet is saved at https://ethercalc.net/hwezfx1qtmmd | |
var transliterationTable = [][2]string{ | |
{"\u05D0", "\u0627"}, {"\u05D1", "\u0628"}, {"\u05D2", "\u062C"}, {"\u05D3", "\u062F"}, {"\u05D4", "\u0647"}, {"\u05D5", "\u0648"}, {"\u05D6", "\u0632"}, {"\u05D7", "\u062D"}, {"\u05D8", "\u0637"}, {"\u05D9", "\u064A"}, {"\u05DB", "\u0643"}, {"\u05DC", "\u0644"}, | |
{"\u05DE", "\u0645"}, {"\u05DD", "\u0645"}, {"\u05E0", "\u0646"}, {"\u05DF", "\u0646"}, {"\u05E1", "\u0633"}, | |
{"\u05E2", "\u0639"}, {"\u05E4", "\u0641"}, {"\u05E3", "\u0641"}, {"\u05E7", "\u0642"}, {"\u05E8", "\u0631"}, {"\u05E9", "\u0634"}, {"\u05EA", "\u062A"}, | |
{"?", "\u061F"}, // Arabic question mark | |
{",", "\u060C"}, // Arabic comma | |
{"\u05B4", "\u0650"}, | |
{"\u05B7", "\u064E"}, {"\u05BB", "\u064F"}, | |
{"\u05DA", "\u0643"}, // Hebrew letter final kaf | |
{"\u05E5", "\u0635"}, // Hebrew letter final tsadi | |
} | |
func main() { | |
fmt.Println("Arabic to Hebrew transliteration") | |
fmt.Println("The transliteration table is:", transliterationTable) | |
var hebrewTextInput string | |
if DEMO_MODE { | |
hebrewTextInput = "אבגדהוזחטיכלמנסעפקרשת ךםןץף ,? אִאַאֻ א" | |
} else { | |
fmt.Scanf("%s", &hebrewTextInput) | |
} | |
fmt.Println("Hebrew input:", hebrewTextInput) | |
var transliteration = transliterate(hebrewTextInput) | |
fmt.Println("Arabic:", transliteration) | |
fmt.Println("Subscribe to youtube.com/GrassDewMorning") | |
} | |
func transliterate(hebrewText string) string { | |
var arabicText string | |
for len(hebrewText) > 0 { | |
var haveMatch bool | |
for _, row := range transliterationTable { | |
if strings.HasPrefix(hebrewText, row[HEBREW_COL]) { | |
haveMatch = true | |
arabicText += row[ARABIC_COL] | |
hebrewText = strings.TrimPrefix(hebrewText, row[HEBREW_COL]) | |
} | |
} | |
if !haveMatch { | |
var firstCharacter string | |
var charactersInHebrewText = []rune(hebrewText) | |
firstCharacter = string(charactersInHebrewText[0]) | |
// DO THE ABOVE, | |
// DO NOT DO THIS: hebrewText[0] BAD!!!! | |
hebrewText = strings.TrimPrefix(hebrewText, firstCharacter) | |
// Preserve untransliteratable characters as-is: | |
arabicText += firstCharacter | |
} | |
} | |
return arabicText | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment