Golang : Example of how to detect which type of script a word belongs to
In one of my projects, I need to detect if a word that cannot be translated is of Arabic or Latin script type. In order to detect a word's script type, I use the unicode.In()
function to match every single character inside the word against a dictionary.
Golang's unicode
package provides the dictionary that I needed and it is fairly easy to do so with a for loop.
Here you go!
package main
import (
"fmt"
"github.com/chrisport/go-lang-detector/langdet/langdetdef"
"unicode"
)
func main() {
// "love" translated to different languages
input := []string{"爱", "好き", "ආදරය", "cinta", "حب", "مينه", "ฮัก", "माया"}
for k, v := range input {
if checkifLatin(v) {
fmt.Println(k, "["+v+"] is a Latin or a derivative of Latin ? ", checkifLatin(v))
} else if checkifArabic(v) {
fmt.Println(k, "["+v+"] is an Arabic or a derivative of Arabic ? ", checkifArabic(v))
} else if checkifThai(v) {
fmt.Println(k, "["+v+"] is a Thai or a derivative of Thai ? ", checkifThai(v))
} else if checkifHan(v) {
fmt.Println(k, "["+v+"] is a Han(Chinese) or a derivative of Han(Chinese) ? ", checkifHan(v))
} else if checkifDevanagari(v) {
fmt.Println(k, "["+v+"] is a Devanagari or a derivative of Devanagari ? ", checkifDevanagari(v))
} else {
// throw in an unknown script Japanese and Sinhala just to test out. To detect, use unicode.Sinhala and unicode.Hiragana
fmt.Println(k, "["+v+"] unable to detect script type. Need new function to detect this script type. ")
}
}
fmt.Println("----------------------------------------------------------------------------")
// alternative method, but....it is not what I want.
detector := langdetdef.NewWithDefaultLanguages()
result := detector.GetClosestLanguage(input[4])
fmt.Println("GetClosestLanguage returns : ", result)
}
// checkifLatin - check if input is Latin scripts or not.
func checkifLatin(input string) bool {
var isLatin = false
for _, v := range input {
if unicode.In(v, unicode.Latin) {
isLatin = true
} else {
isLatin = false
}
}
return isLatin
}
// checkifThai - check if input is Thai scripts or not.
func checkifThai(input string) bool {
var isThai = false
for _, v := range input {
if unicode.In(v, unicode.Thai) {
isThai = true
} else {
isThai = false
}
}
return isThai
}
// checkifArabic is Arabic characters or not.
func checkifArabic(input string) bool {
var isArabic = false
for _, v := range input {
if unicode.In(v, unicode.Arabic) {
isArabic = true
} else {
isArabic = false
}
}
return isArabic
}
// checkifHan is Han(Chinese) characters or not.
func checkifHan(input string) bool {
var isHan = false
for _, v := range input {
if unicode.In(v, unicode.Han) {
isHan = true
} else {
isHan = false
}
}
return isHan
}
// checkifDevanagari - check if input is Devanagari scripts or not.
func checkifDevanagari(input string) bool {
var isDevanagari = false
for _, v := range input {
if unicode.In(v, unicode.Devanagari) {
isDevanagari = true
} else {
isDevanagari = false
}
}
return isDevanagari
}
Output:
0 [爱] is a Han(Chinese) or a derivative of Han(Chinese) ? true
1 [好き] unable to detect script type. Need new function to detect this script type.
2 [ආදරය] unable to detect script type. Need new function to detect this script type.
3 [cinta] is a Latin or a derivative of Latin ? true
4 [حب] is an Arabic or a derivative of Arabic ? true
5 [مينه] is an Arabic or a derivative of Arabic ? true
6 [ฮัก] is a Thai or a derivative of Thai ? true
7 [माया] is a Devanagari or a derivative of Devanagari ? true
----------------------------------------------------------------------------
GetClosestLanguage returns : undefined
Reference :
See also : Golang : Converting individual Jawi alphabet to Rumi(Romanized) alphabet example
By Adam Ng(黃武俊)
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+8.1k Useful methods to access blocked websites
+11.1k Golang : Delay or limit HTTP requests example
+7k Golang : How to fix html/template : "somefile" is undefined error?
+9k Golang : Generate random Chinese, Japanese, Korean and other runes
+18.3k Golang : Generate thumbnails from images
+7.6k Javascript : How to check a browser's Do Not Track status?
+8.9k Android Studio : Indicate progression with ProgressBar example
+13.4k Golang : Tutorial on loading GOB and PEM files
+20.2k Golang : Secure(TLS) connection between server and client
+33.3k Golang : All update packages with go get command
+7.1k Golang : Check to see if *File is a file or directory