arabic

package module
v0.0.0-...-177ba95 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2021 License: Apache-2.0 Imports: 6 Imported by: 0

README

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

ar-golang

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.
  • 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/ar-golang"
)

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"

	arabic "github.com/abdullahdiaa/ar-golang"
	"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:    80,
		DPI:     72,
		Hinting: font.HintingFull,
	})

	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, 300, 150))
	addLabel(img, 55, 95, arabic.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 arabic 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'
	//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 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 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

Types

This section is empty.

Jump to

Keyboard shortcuts

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