goarabic

package module
v0.0.2-0...-aaa4bd9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 17, 2018 License: MIT Imports: 3 Imported by: 0

README

GoArabic

Build Status GoDoc

A Go Lang package for dealing with Arabic text.

This is an initial work on a set of Go functions developed to enhance Arabic web applications. It all started when I wanted to develop a self-hosted no-dependency and standalone Arabic Captcha in Go (for fun, as I'm discovering Go), and as expected, the Arabic text wasn't rendered as it should:

Before processing

So I started playing with Glyphs, unicode, and the special Arabic rules for joining letters. After a bit of work, I got this:

Before processing

shouldn't this be a separated package, along with other functionalities? I guess it should!

Current functionalities

  • Glyph representation of the given Arabic text for images/pdf .. etc generation.
  • Strip Tashkeel (Arabic Vowels).
  • SmartLengh: return the length of the given Arabic String without considering Tashkeel (Arabic Vowels).
  • Strip Tatweel
  • rune-wise (UTF-8) reverse of the Arabic text, leaving out the Latin one.

Todo

  • Arabic text normalization (Unshaping)
  • Add support for some special Quran text characters in Glyphs
  • Richer SmartLengh, ignoring some special characters.
  • Slugify: a simple Arabic slug generation. Striping Tashkeel and other special chars.
  • Spell numbers in Arabic idiom.
  • Present dates in Arabic or Hijri and convert Hijri date into Unix timestamp And maybe more...

Usage

Importing
got get github.com/01walid/goarabic
Example Usage
package main

import (
    "fmt"
    "github.com/01walid/goarabic"
)

func main() {
    fmt.Println(goarabic.RemoveTashkeel("نًصٌ عَربيُّ"))
    fmt.Println(goarabic.ToGlyph("تجربة النص العربي"))
}

Documentation

Package docs available on gopkgdoc.

Contributing

Contributions are greatly appreciated. Please fork this repository, make your changes, and open a pull request. More test cases and considerations might be needed, you can run tests using go test for the existing functionalities.

This a SemVersioned package.

License

MIT License.

Documentation

Overview

Package goarabic contains utility functions for working with Arabic strings.

Index

Constants

This section is empty.

Variables

View Source
var (
	FATHA    rune = '\u064e'
	FATHATAN rune = '\u064b'
	DAMMA    rune = '\u064f'
	DAMMATAN rune = '\u064c'
	KASRA    rune = '\u0650'
	KASRATAN rune = '\u064d'
	SHADDA   rune = '\u0651'
	SUKUN    rune = '\u0652'
)

Vowels (Tashkeel) characters.

View Source
var (
	ALEF_HAMZA_ABOVE = Harf{
		Unicode:   '\u0623',
		Isolated:  '\ufe83',
		Beggining: '\u0623',
		Medium:    '\ufe84',
		Final:     '\ufe84'}

	ALEF = Harf{
		Unicode:   '\u0627',
		Isolated:  '\ufe8d',
		Beggining: '\u0627',
		Medium:    '\ufe8e',
		Final:     '\ufe8e'}

	ALEF_MADDA_ABOVE = Harf{
		Unicode:   '\u0622',
		Isolated:  '\ufe81',
		Beggining: '\u0622',
		Medium:    '\ufe82',
		Final:     '\ufe82'}

	HAMZA = Harf{
		Unicode:   '\u0621',
		Isolated:  '\ufe80',
		Beggining: '\u0621',
		Medium:    '\u0621',
		Final:     '\u0621'}

	WAW_HAMZA_ABOVE = Harf{
		Unicode:   '\u0624',
		Isolated:  '\ufe85',
		Beggining: '\u0624',
		Medium:    '\ufe86',
		Final:     '\ufe86'}

	ALEF_HAMZA_BELOW = Harf{
		Unicode:   '\u0625',
		Isolated:  '\ufe87',
		Beggining: '\u0625',
		Medium:    '\ufe88',
		Final:     '\ufe88'}

	YEH_HAMZA_ABOVE = Harf{
		Unicode:   '\u0626',
		Isolated:  '\ufe89',
		Beggining: '\ufe8b',
		Medium:    '\ufe8c',
		Final:     '\ufe8a'}

	BEH = Harf{
		Unicode:   '\u0628',
		Isolated:  '\ufe8f',
		Beggining: '\ufe91',
		Medium:    '\ufe92',
		Final:     '\ufe90'}

	PEH = Harf{
		Unicode:   '\u067e',
		Isolated:  '\ufb56',
		Beggining: '\ufb58',
		Medium:    '\ufb59',
		Final:     '\ufb57'}

	TEH = Harf{
		Unicode:   '\u062A',
		Isolated:  '\ufe95',
		Beggining: '\ufe97',
		Medium:    '\ufe98',
		Final:     '\ufe96'}

	TEH_MARBUTA = Harf{
		Unicode:   '\u0629',
		Isolated:  '\ufe93',
		Beggining: '\u0629',
		Medium:    '\u0629',
		Final:     '\ufe94'}

	THEH = Harf{
		Unicode:   '\u062b',
		Isolated:  '\ufe99',
		Beggining: '\ufe9b',
		Medium:    '\ufe9c',
		Final:     '\ufe9a'}

	JEEM = Harf{
		Unicode:   '\u062c',
		Isolated:  '\ufe9d',
		Beggining: '\ufe9f',
		Medium:    '\ufea0',
		Final:     '\ufe9e'}

	TCHEH = Harf{
		Unicode:   '\u0686',
		Isolated:  '\ufb7a',
		Beggining: '\ufb7c',
		Medium:    '\ufb7d',
		Final:     '\ufb7b'}

	HAH = Harf{
		Unicode:   '\u062d',
		Isolated:  '\ufea1',
		Beggining: '\ufea3',
		Medium:    '\ufea4',
		Final:     '\ufea2'}

	KHAH = Harf{
		Unicode:   '\u062e',
		Isolated:  '\ufea5',
		Beggining: '\ufea7',
		Medium:    '\ufea8',
		Final:     '\ufea6'}

	DAL = Harf{
		Unicode:   '\u062f',
		Isolated:  '\ufea9',
		Beggining: '\u062f',
		Medium:    '\ufeaa',
		Final:     '\ufeaa'}

	THAL = Harf{
		Unicode:   '\u0630',
		Isolated:  '\ufeab',
		Beggining: '\u0630',
		Medium:    '\ufeac',
		Final:     '\ufeac'}

	REH = Harf{
		Unicode:   '\u0631',
		Isolated:  '\ufead',
		Beggining: '\u0631',
		Medium:    '\ufeae',
		Final:     '\ufeae'}

	JEH = Harf{
		Unicode:   '\u0698',
		Isolated:  '\ufb8a',
		Beggining: '\u0698',
		Medium:    '\ufb8b',
		Final:     '\ufb8b',
	}

	ZAIN = Harf{
		Unicode:   '\u0632',
		Isolated:  '\ufeaf',
		Beggining: '\u0632',
		Medium:    '\ufeb0',
		Final:     '\ufeb0'}

	SEEN = Harf{
		Unicode:   '\u0633',
		Isolated:  '\ufeb1',
		Beggining: '\ufeb3',
		Medium:    '\ufeb4',
		Final:     '\ufeb2'}

	SHEEN = Harf{
		Unicode:   '\u0634',
		Isolated:  '\ufeb5',
		Beggining: '\ufeb7',
		Medium:    '\ufeb8',
		Final:     '\ufeb6'}

	SAD = Harf{
		Unicode:   '\u0635',
		Isolated:  '\ufeb9',
		Beggining: '\ufebb',
		Medium:    '\ufebc',
		Final:     '\ufeba'}

	DAD = Harf{
		Unicode:   '\u0636',
		Isolated:  '\ufebd',
		Beggining: '\ufebf',
		Medium:    '\ufec0',
		Final:     '\ufebe'}

	TAH = Harf{
		Unicode:   '\u0637',
		Isolated:  '\ufec1',
		Beggining: '\ufec3',
		Medium:    '\ufec4',
		Final:     '\ufec2'}

	ZAH = Harf{
		Unicode:   '\u0638',
		Isolated:  '\ufec5',
		Beggining: '\ufec7',
		Medium:    '\ufec8',
		Final:     '\ufec6'}

	AIN = Harf{
		Unicode:   '\u0639',
		Isolated:  '\ufec9',
		Beggining: '\ufecb',
		Medium:    '\ufecc',
		Final:     '\ufeca'}

	GHAIN = Harf{
		Unicode:   '\u063a',
		Isolated:  '\ufecd',
		Beggining: '\ufecf',
		Medium:    '\ufed0',
		Final:     '\ufece'}

	FEH = Harf{
		Unicode:   '\u0641',
		Isolated:  '\ufed1',
		Beggining: '\ufed3',
		Medium:    '\ufed4',
		Final:     '\ufed2'}

	QAF = Harf{
		Unicode:   '\u0642',
		Isolated:  '\ufed5',
		Beggining: '\ufed7',
		Medium:    '\ufed8',
		Final:     '\ufed6'}

	KAF = Harf{
		Unicode:   '\u0643',
		Isolated:  '\ufed9',
		Beggining: '\ufedb',
		Medium:    '\ufedc',
		Final:     '\ufeda'}

	KEHEH = Harf{
		Unicode:   '\u06a9',
		Isolated:  '\ufb8e',
		Beggining: '\ufb90',
		Medium:    '\ufb91',
		Final:     '\ufb8f',
	}

	GAF = Harf{
		Unicode:   '\u06af',
		Isolated:  '\ufb92',
		Beggining: '\ufb94',
		Medium:    '\ufb95',
		Final:     '\ufb93'}

	LAM = Harf{
		Unicode:   '\u0644',
		Isolated:  '\ufedd',
		Beggining: '\ufedf',
		Medium:    '\ufee0',
		Final:     '\ufede'}

	MEEM = Harf{
		Unicode:   '\u0645',
		Isolated:  '\ufee1',
		Beggining: '\ufee3',
		Medium:    '\ufee4',
		Final:     '\ufee2'}

	NOON = Harf{
		Unicode:   '\u0646',
		Isolated:  '\ufee5',
		Beggining: '\ufee7',
		Medium:    '\ufee8',
		Final:     '\ufee6'}

	HEH = Harf{
		Unicode:   '\u0647',
		Isolated:  '\ufee9',
		Beggining: '\ufeeb',
		Medium:    '\ufeec',
		Final:     '\ufeea'}

	WAW = Harf{
		Unicode:   '\u0648',
		Isolated:  '\ufeed',
		Beggining: '\u0648',
		Medium:    '\ufeee',
		Final:     '\ufeee'}

	YEH = Harf{
		Unicode:   '\u06cc',
		Isolated:  '\ufbfc',
		Beggining: '\ufbfe',
		Medium:    '\ufbff',
		Final:     '\ufbfd'}

	ARABICYEH = Harf{
		Unicode:   '\u064a',
		Isolated:  '\ufef1',
		Beggining: '\ufef3',
		Medium:    '\ufef4',
		Final:     '\ufef2'}

	ALEF_MAKSURA = Harf{
		Unicode:   '\u0649',
		Isolated:  '\ufeef',
		Beggining: '\u0649',
		Medium:    '\ufef0',
		Final:     '\ufef0'}

	TATWEEL = Harf{
		Unicode:   '\u0640',
		Isolated:  '\u0640',
		Beggining: '\u0640',
		Medium:    '\u0640',
		Final:     '\u0640'}

	LAM_ALEF = Harf{
		Unicode:   '\ufefb',
		Isolated:  '\ufefb',
		Beggining: '\ufefb',
		Medium:    '\ufefc',
		Final:     '\ufefc'}

	LAM_ALEF_HAMZA_ABOVE = Harf{
		Unicode:   '\ufef7',
		Isolated:  '\ufef7',
		Beggining: '\ufef7',
		Medium:    '\ufef8',
		Final:     '\ufef8'}
)

Arabic Alphabet using the new Harf type.

Functions

func AbbreviateNumber

func AbbreviateNumber(lint int) string

Convert large number to its abbreviation in Arabic. Abbreviate to billons, millions, and thousands. Otherwise number will be returned unedited.

func ContainsArabic

func ContainsArabic(text string) bool

Using regex, check if the given text contains at least 1 Arabic character or Arabic numerals.(Taskheel not included)

func IsArabic

func IsArabic(text string) bool

Using regex, check if the given text contains only Arabic characters,Arabic numerals, and White spaces. (Taskheel not included)

func Normalize

func Normalize(text string) string

General function to normalize Arabic text

  • switch english to arabic numerals.
  • remove tashkeel and tatweel.
  • trim all leading and trailing white spaces.
  • switch any "أإآ"" to "ا".
  • switch any "ة" to "ه".

func RemoveAllNonArabicChars

func RemoveAllNonArabicChars(text string) string

RemoveAllNonAlphabetChars deletes all characters which are not included in Arabic Alphabet

func RemoveTashkeel

func RemoveTashkeel(s string) string

RemoveTashkeel returns its argument as rune-wise string without Arabic vowels (Tashkeel).

func RemoveTatweel

func RemoveTatweel(s string) string

RemoveTatweel returns its argument as rune-wise string without Arabic Tatweel character.

func Reverse

func Reverse(s string) string

Reverse returns its argument string reversed rune-wise left to right.

func SmartLength

func SmartLength(s *string) int

SmartLength returns the length of the given string without considering the Arabic Vowels (Tashkeel).

func SwitchNumeral

func SwitchNumeral(text string, toArabic bool) string

Switch between Arabic and English Numeral systems. Pass true to switch to Arabic. If text contains other non numeral characters, it will be left intact.

func ToGlyph

func ToGlyph(text string) string

ToGlyph returns the glyph representation of the given text

Types

type Harf

type Harf struct {
	Unicode, Isolated, Beggining, Medium, Final rune
}

Harf holds the Arabic character with its different representation forms (glyphs).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL