Last active
December 2, 2021 21:46
-
-
Save elsanussi-s-mneina/9673a0c9fba3a567bbba5c51b2fb76ad to your computer and use it in GitHub Desktop.
Hebrew to Arabic transliteration
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. | |
*/ | |
// You can run this in the Go Playground at go.dev/play | |
package main | |
import ( | |
"bufio" | |
"fmt" | |
"log" | |
"os" | |
"strings" | |
"testing" | |
) | |
func standardInToStandardOut() { | |
scanner := bufio.NewScanner(os.Stdin) | |
for scanner.Scan() { | |
var currentLine string = scanner.Text() | |
var transliteratedLine string = transliterate(currentLine) | |
fmt.Println(transliteratedLine) | |
} | |
if err := scanner.Err(); err != nil { | |
log.Println(err) | |
} | |
} | |
func main() { | |
standardInToStandardOut() | |
} | |
func demoOneWord() { | |
fmt.Println("Hebrew to Arabic transliterator") | |
// Example: | |
// Hebrew text: שלום | |
// Arabic script transliteration: شلوم | |
var hebrewText string | |
var transliteration string | |
hebrewText = "שלום" | |
fmt.Println("The Hebrew text is", hebrewText) | |
transliteration = transliterate(hebrewText) | |
fmt.Println("The transliteration is", transliteration) | |
} | |
func transliterate(text string) string { | |
var transliteration string | |
for len(text) > 0 { | |
if strings.HasPrefix(text, "א") { | |
transliteration += "ا" | |
text = strings.TrimPrefix(text, "א") | |
} else if strings.HasPrefix(text, "ב") { | |
transliteration += "ب" | |
text = strings.TrimPrefix(text, "ב") | |
} else if prefix := "ג"; strings.HasPrefix(text, prefix) { | |
transliteration += "ج" | |
text = strings.TrimPrefix(text, prefix) | |
} else if prefix := "ד"; strings.HasPrefix(text, prefix) { | |
transliteration += "د" | |
text = strings.TrimPrefix(text, prefix) | |
} else if prefix := "ה"; strings.HasPrefix(text, prefix) { | |
transliteration += "ه" | |
text = strings.TrimPrefix(text, prefix) | |
} else if prefix := "ו"; strings.HasPrefix(text, prefix) { | |
transliteration += "و" | |
text = strings.TrimPrefix(text, prefix) | |
} else if prefix := "ז"; strings.HasPrefix(text, prefix) { | |
transliteration += "ز" | |
text = strings.TrimPrefix(text, prefix) | |
} else if prefix := "ח"; strings.HasPrefix(text, prefix) { | |
transliteration += "ح" | |
text = strings.TrimPrefix(text, prefix) | |
} else if prefix := "ט"; strings.HasPrefix(text, prefix) { | |
transliteration += "ط" | |
text = strings.TrimPrefix(text, prefix) | |
} else if prefix := "י"; strings.HasPrefix(text, prefix) { | |
transliteration += "ي" | |
text = strings.TrimPrefix(text, prefix) | |
} else if prefix := "כ"; strings.HasPrefix(text, prefix) { | |
transliteration += "ك" | |
text = strings.TrimPrefix(text, prefix) | |
} else if prefix := "ל"; strings.HasPrefix(text, prefix) { | |
transliteration += "ل" | |
text = strings.TrimPrefix(text, prefix) | |
} else if prefix := "מ"; strings.HasPrefix(text, prefix) { | |
transliteration += "م" | |
text = strings.TrimPrefix(text, prefix) | |
} else if prefix := "נ"; strings.HasPrefix(text, prefix) { | |
transliteration += "ن" | |
text = strings.TrimPrefix(text, prefix) | |
} else if prefix := "ס"; strings.HasPrefix(text, prefix) { | |
transliteration += "س" | |
text = strings.TrimPrefix(text, prefix) | |
} else if prefix := "ע"; strings.HasPrefix(text, prefix) { | |
transliteration += "ع" | |
text = strings.TrimPrefix(text, prefix) | |
} else if prefix := "פ"; strings.HasPrefix(text, prefix) { | |
transliteration += "ف" | |
text = strings.TrimPrefix(text, prefix) | |
} else if prefix := "צ"; strings.HasPrefix(text, prefix) { | |
transliteration += "ص" | |
text = strings.TrimPrefix(text, prefix) | |
} else if prefix := "ק"; strings.HasPrefix(text, prefix) { | |
transliteration += "ق" | |
text = strings.TrimPrefix(text, prefix) | |
} else if prefix := "ר"; strings.HasPrefix(text, prefix) { | |
transliteration += "ر" | |
text = strings.TrimPrefix(text, prefix) | |
} else if prefix := "ש"; strings.HasPrefix(text, prefix) { | |
transliteration += "ش" | |
text = strings.TrimPrefix(text, prefix) | |
} else if prefix := "ת"; strings.HasPrefix(text, prefix) { | |
transliteration += "ت" | |
text = strings.TrimPrefix(text, prefix) | |
// Hebrew final letters: | |
} else if prefix := "ך"; strings.HasPrefix(text, prefix) { | |
transliteration += "ك" | |
text = strings.TrimPrefix(text, prefix) | |
} else if prefix := "ם"; strings.HasPrefix(text, prefix) { | |
transliteration += "م" | |
text = strings.TrimPrefix(text, prefix) | |
} else if prefix := "ן"; strings.HasPrefix(text, prefix) { | |
transliteration += "ن" | |
text = strings.TrimPrefix(text, prefix) | |
} else if prefix := "ף"; strings.HasPrefix(text, prefix) { | |
transliteration += "ف" | |
text = strings.TrimPrefix(text, prefix) | |
} else if prefix := "ץ"; strings.HasPrefix(text, prefix) { | |
transliteration += "ص" | |
text = strings.TrimPrefix(text, prefix) | |
// Punctuation: | |
} else if prefix := ","; strings.HasPrefix(text, prefix) { | |
transliteration += "،" | |
text = strings.TrimPrefix(text, prefix) | |
} else if prefix := "?"; strings.HasPrefix(text, prefix) { | |
transliteration += "؟" | |
text = strings.TrimPrefix(text, prefix) | |
} else { | |
// Make sure at least one character | |
// is deleted in each iteration of | |
// the for-loop. | |
first := string(([]rune(text))[0]) // get first character of string | |
transliteration += first | |
text = strings.TrimPrefix(text, first) | |
} | |
} | |
return transliteration | |
} | |
func TestTransliterate_א(t *testing.T) { | |
hebrew := "א" | |
got := transliterate(hebrew) | |
if got != "ا" { | |
t.Errorf("Did not transliterate %s correctly: %s", hebrew, got) | |
} | |
} | |
func TestTransliterate_ג(t *testing.T) { | |
hebrew := "ג" | |
got := transliterate(hebrew) | |
if got != "ج" { | |
t.Errorf("Did not transliterate %s correctly: %s", hebrew, got) | |
} | |
} | |
func TestTransliterate_אא(t *testing.T) { | |
hebrew := "אא" | |
got := transliterate(hebrew) | |
if got != "اا" { | |
t.Errorf("Did not transliterate %s correctly: %s", hebrew, got) | |
} | |
} | |
func TestTransliterate_שלום(t *testing.T) { | |
got := transliterate("שלום") | |
if got != "شلوم" { | |
t.Errorf("Did not transliterate שלום correctly: %s", got) | |
} | |
} | |
func TestTransliterate_שלוםשלום(t *testing.T) { | |
got := transliterate("שלום שלום") | |
if got != "شلوم شلوم" { | |
t.Errorf("Did not transliterate שלום שלום correctly: %s", got) | |
} | |
} | |
func TestTransliterate_comma(t *testing.T) { | |
got := transliterate("שלום, שלום") | |
if got != "شلوم، شلوم" { | |
t.Errorf("Did not transliterate שלום, שלום correctly: %s", got) | |
} | |
} | |
func TestTransliterate_questionMark(t *testing.T) { | |
got := transliterate("שלום? שלום") | |
if got != "شلوم؟ شلوم" { | |
t.Errorf("Did not transliterate שלום? שלום correctly: %s", got) | |
} | |
} | |
func TestTransliterate_numbers(t *testing.T) { | |
got := transliterate("שלום 0123456789 שלום") | |
if got != "شلوم 0123456789 شلوم" { | |
t.Errorf("Did not transliterate שלום 0123456789 שלום correctly: %s", got) | |
} | |
} | |
func TestTransliterate_HebrewLettersNonFinal(t *testing.T) { | |
hebrew := "אבגדהוזחטיכלמנסעפצקרשת" | |
got := transliterate(hebrew) | |
if got != "ابجدهوزحطيكلمنسعفصقرشت" { | |
t.Errorf("Did not transliterate Hebrew non final letters correctly: %s", got) | |
} | |
} | |
func TestTransliterate_HebrewLettersFinal(t *testing.T) { | |
hebrew := "ךםןףץ" | |
got := transliterate(hebrew) | |
if got != "كمنفص" { | |
t.Errorf("Did not transliterate Hebrew final letters correctly: %s", got) | |
} | |
} | |
func TestTransliterate_emptyString(t *testing.T) { | |
got := transliterate("") | |
if got != "" { | |
t.Errorf("Did not transliterate empty string correctly: %s", got) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment