Loading the lesson...
In the print dialog, choose "Save as PDF" as the destination.
A tour of CAMeL Tools: normalization, morphology, disambiguation, and pretrained Arabic NLP models
camel-tools.readthedocs.io, source at github.com/CAMeL-Lab/camel_toolspip install camel-tools
camel_data -i all # install all pretrained databases/modelscamel_data -i all installs every pretrained database and model.from camel_tools.utils.normalize import normalize_unicode
sent_norm = normalize_unicode('ﷺ')
print(sent_norm)
# 'صلى الله عليه وسلم'from camel_tools.utils.normalize import (
normalize_alef_ar, normalize_alef_maksura_ar, normalize_teh_marbuta_ar)
sent_norm = normalize_alef_ar("هل ذهبت إلى المكتبة؟")
sent_norm = normalize_alef_maksura_ar(sent_norm)
sent_norm = normalize_teh_marbuta_ar(sent_norm)
# 'هل ذهبت الي المكتبه؟'from camel_tools.utils.dediac import dediac_ar
dediac_ar("هَلْ ذَهَبْتَ إِلَى المَكْتَبَةِ؟")
# 'هل ذهبت إلى المكتبة؟'simple_word_tokenize splits on whitespace and punctuation.from camel_tools.tokenizers.word import simple_word_tokenize
simple_word_tokenize("هَلْ ذَهَبْتَ")
# ['هَلْ', 'ذَهَبْتَ']CharMapperfrom camel_tools.utils.charmap import CharMapper
ar2bw = CharMapper.builtin_mapper('ar2bw')
ar2bw("ذهبت إلى المكتبة.")
# '*hbt <lY Almktbp.'from camel_tools.morphology.database import MorphologyDB
from camel_tools.morphology.analyzer import Analyzer
db = MorphologyDB.builtin_db()
analyzer = Analyzer(db)
analyses = analyzer.analyze('وسيكتبونها')generator = Generator(MorphologyDB.builtin_db(flags='g'))
generator.generate('مُوَظَّف', {'pos': 'noun', 'gen': 'm', 'num': 'p'})
# 'مُوَظَّفُونَ', 'مُوَظَّفِينَ', ...reinflector = Reinflector(MorphologyDB.builtin_db(flags='r'))
reinflector.reinflect('شوارع', {'num': 'd', 'prc1': 'bi_prep'})
# 'بِشارِعَيْنِ', 'بِشارِعَيْ'MLEDisambiguator picks the most likely analysis for each word in context (MLE = maximum likelihood estimate)mle = MLEDisambiguator.pretrained()
disambig = mle.disambiguate('سوف نقرأ الكتب'.split())
# ('سَوْفَ', 'part_fut', 'سَوْفَ')
# ('نَقْرَأ', 'verb', 'قَرَأ')
# ('الكُتُبِ', 'noun', 'كِتاب')| Scheme | What it produces |
|---|---|
d3tok | Diacritized morpheme segments |
atbtok | ATB-style segments, undiacritized |
bwtok | Buckwalter-style segments |
mle_msa = MLEDisambiguator.pretrained('calima-msa-r13')
tokenizer = MorphologicalTokenizer(disambiguator=mle_msa, scheme='atbtok')
tokenizer.tokenize(['فتنفست', 'الصعداء'])
# ['ف+_تنفست', 'الصعداء']from camel_tools.sentiment import SentimentAnalyzer
sa = SentimentAnalyzer.pretrained()
sa.predict_sentence('أنا بخير') # 'positive'
sa.predict(['أنا بخير', 'أنا لست بخير']) # ['positive', 'negative']B-LOC, I-LOC, O)from camel_tools.ner import NERecognizer
ner = NERecognizer.pretrained()
sentence = 'إمارة أبوظبي هي إحدى إمارات دولة الإمارات العربية المتحدة السبع .'.split()
labels = ner.predict_sentence(sentence)
# ('أبوظبي', 'B-LOC'), ('الإمارات', 'B-LOC'), ('العربية', 'I-LOC'), ('المتحدة', 'I-LOC')Normalization
Tokenization
Morphology
Disambiguation
Tagging
MLEDisambiguator resolves ambiguous word analyses using sentence contextMahmoud Abas|CAMeL Tools Documentation