goarabic

package module
v0.0.0-...-21eaac6 Latest Latest
Warning

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

Go to latest
Published: May 28, 2023 License: MIT Imports: 2 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 was discovering Go ~8 yrs ago), 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
go 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    = '\u064e'
	FATHATAN = '\u064b'
	DAMMA    = '\u064f'
	DAMMATAN = '\u064c'
	KASRA    = '\u0650'
	KASRATAN = '\u064d'
	SHADDA   = '\u0651'
	SUKUN    = '\u0652'
)

Vowels (Tashkeel) characters.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Arabic Alphabet using the new Harf type.

Functions

func RemoveAllNonArabicChars

func RemoveAllNonArabicChars(text string) string

RemoveAllNonArabicChars 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 ToGlyph

func ToGlyph(text string) string

ToGlyph returns the glyph representation of the given text

Types

type Harf

type Harf struct {
	Unicode, Isolated, Beginning, 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