garabic

package module
v1.16.12 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2024 License: Apache-2.0 Imports: 6 Imported by: 1

README

Arabic tools for golang - حزمة أدوات للتعامل مع اللغة العربية في لغة go

GArabic

GoDoc codecov Build Status Go Report Card

A set of functions for Arabic text processing in golang

مكتبة برمجية توفر دوالاً للتعامل مع النصوص العربية في لغة برمجة go

⚠️ The library still under active development

⚠️ المكتبة لا تزال تحت التطوير

Features

  • Normalize Arabic text for processing.
  • Remove Harakat from Arabic text.
  • Arabic numbers to words.
  • Arabic Glyphs shaping to render Arabic text properly in images.
  • Convert english digits to Arabic digits, and vice versa
  • Add diacritics to Arabic text [in progress]
  • Hijri date support.
  • English-Arabic Transliteration.
  • Arabic Sentiment Analysis.

المزايا

  • تنميط الحروف
  • اختزال التشكيل
  • تحويل الأعداد إلى كلمات
  • اصلاح تشبيك النص العربي
  • تحويل الأرقام الانجليزية لأرقام عربية و العكس
  • تشكيل النص العربي
  • التاريخ الهجري
  • دعم قراءة و تحويل النص العربي لحروف انجليزية
  • تحليل المشاعر في النص العربي

Usage

Normalization / تنميط الحروف
package main

import (
	"fmt"

	arabic "github.com/abdullahdiaa/garabic"
)

func main() {
     normalized := arabic.RemoveHarakat("سَنواتٌ")
	fmt.Println(normalized)
	// Output:
	// سنوات
}
Arabic Glyphs shaping / اصلاح تشبيك النص العربي

Here's an example for printing Arabic text on an image:

مثال لطباعة نص عربي علي صورة:

without the library/ من غير استخدام المكتبة

using the library/ باستخدام المكتبة

package main

import (
	"image"
	"image/color"
	"image/png"
	"io/ioutil"
	"log"
	"os"

	"github.com/abdullahdiaa/garabic"
	"golang.org/x/image/font"
	"golang.org/x/image/font/opentype"
	"golang.org/x/image/math/fixed"
)

func addLabel(img *image.RGBA, x, y int, label string) {
	//Load font file
	//You can download amiri font from this link: https://fonts.google.com/specimen/Amiri?preview.text=%D8%A8%D9%90%D8%A7%D9%84%D8%B9%D9%8E%D8%B1%D9%8E%D8%A8%D9%90%D9%91%D9%8A&preview.text_type=custom#standard-styles
	b, err := ioutil.ReadFile("Amiri-Regular.ttf")
	if err != nil {
		log.Println(err)
		return
	}

	ttf, err := opentype.Parse(b)
	if err != nil {
		log.Println(err)
		return
	}
	//Create Font.Face from font
	face, err := opentype.NewFace(ttf, &opentype.FaceOptions{
		Size:    26,
		DPI:     72,
		Hinting: font.HintingNone,
	})

	d := &font.Drawer{
		Dst:  img,
		Src:  image.NewUniform(color.RGBA{120, 157, 243, 255}),
		Face: face,
		Dot:  fixed.P(x, y),
	}

	d.DrawString(label)
}

func main() {
	img := image.NewRGBA(image.Rect(0, 0, 700, 70))
	addLabel(img, 40, 40, garabic.Shape("قِفا نَبكِ مِن ذِكرى حَبيبٍ وَمَنزِلِ   ****   بِسِقطِ اللِوى بَينَ الدَخولِ فَحَومَلِ"))

	f, err := os.Create("printed_arabic_text.png")
	if err != nil {
		panic(err)
	}
	defer f.Close()
	if err := png.Encode(f, img); err != nil {
		panic(err)
	}
}

Speed

Here's a benchmark for normalizing ~78K words on MBP i5 takes about ~45ms:

BenchmarkNormalizeBigText-4           25          45546613 ns/op         9275097 B/op         31 allocs/op
~45 ms

Documentation

You can view detailed documentation here: GoDoc.

التوثيق

يمكنك مراجعة توثيق الكود و الاستخدام الخاص بكل دالة من خلال هذا الرابط: GoDoc.

Contributing

There are many ways to contribute:

المشاركة

يمكنك المشاركة في تطوير المكتبة بأحد هذه الطرق:

Changelog

View the changelog for the latest updates and changes by version.

License

Apache License 2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Documentation

Overview

Package garabic provides a set of functions for Arabic text processing in golang

Index

Examples

Constants

View Source
const (
	//Alef  => ا
	Alef = '\u0627'
	//AlefMad =>  آ
	AlefMad = '\u0622'
	//AlefHamzaAbove => أ
	AlefHamzaAbove = '\u0623'
	//AlefHamzaBelow إ
	AlefHamzaBelow = '\u0625'
	//Yae =>  ي
	Yae = '\u064A'
	//Yeh =>  ي
	Yeh = '\u06cc'
	//DotlessYae =>  ى
	DotlessYae = '\u0649'
	//TehMarbuta => ة
	TehMarbuta = '\u0629'
	//Hae => ه
	Hae = '\u0647'
	//AlefWaslah ٱ / Waslah is considered part of harakat/تَشْكِيل ?
	AlefWaslah = '\u0671'
)

Normalizable letters alef/Yae/Hae

Variables

This section is empty.

Functions

func IsArabic

func IsArabic(input string) bool

IsArabic checks if the input string contains arabic unicode only

func IsArabicLetter

func IsArabicLetter(ch rune) bool

IsArabicLetter checks if the letter is arabic

Example
fmt.Println(IsArabicLetter('ص'))
Output:

true

func Normalize

func Normalize(input string) string

Normalize will prepare an arabic text to search and index

Example
normalized := Normalize("أحمد")
fmt.Println(normalized)
Output:

احمد

func RemoveHarakat

func RemoveHarakat(input string) string

RemoveHarakat will remove harakat from arabic text

Example
normalized := RemoveHarakat("سَنواتٌ")
fmt.Println(normalized)
Output:

سنوات

func Shape

func Shape(input string) string

Shape will reconstruct arabic text to be connected correctly

func SpellNumber

func SpellNumber(input int) string

SpellNumber will transform a number into a readable arabic version

Example
numberInWords := SpellNumber(100)
fmt.Println(numberInWords)
Output:

مئة

func Tashkeel

func Tashkeel(input string) string

Tashkeel will add matching diacritics to arabic text

func ToArabicDigits

func ToArabicDigits(input string) string

ToArabicDigits will convert english numbers to arabic numbers in text

Example
fmt.Println(ToArabicDigits("عام 2021"))
Output:

عام ٢٠٢١

func ToEnglishDigits

func ToEnglishDigits(input string) string

ToEnglishDigits will convert arabic numbers to english numbers in text

Example
fmt.Println(ToEnglishDigits("عام ٢٠٢١"))
Output:

عام 2021

func ToPersianDigits added in v1.16.11

func ToPersianDigits(input string) string

ToPersianDigits will convert english numbers to persian numbers in text

Types

This section is empty.

Jump to

Keyboard shortcuts

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