AraBERT is designed to be compatible with existing BERT codebases. The primary difference is in the tokenization.py file, where the _is_punctuation function is modified to support the + symbol and [ and ] characters.
Depending on the version you choose, your preprocessing workflow will differ:
- AraBERTv1 and AraBERTv2: These versions require pre-segmentation. You must use the
ArabertPreprocessor to split prefixes and suffixes (using the Farasa Segmenter logic) before passing the text to the tokenizer. - AraBERTv0.1 and AraBERTv0.2: These versions do not require pre-segmentation and can be used with raw text.
from transformers import AutoTokenizer, AutoModel
from arabert.preprocess import ArabertPreprocessor
model_name = "aubmindlab/bert-base-arabertv2"
arabert_tokenizer = AutoTokenizer.from_pretrained(model_name)
arabert_model = AutoModel.from_pretrained(model_name)
arabert_prep = ArabertPreprocessor(model_name=model_name)
text = "ولن نبالغ إذا قلنا إن هاتف أو كمبيوتر المكتب في زمننا هذا ضروري"
text_preprocessed = arabert_prep.preprocess(text)
# Output: "و+ لن نبالغ إذا قل +نا إن هاتف أو كمبيوتر ال+ مكتب في زمن +نا هذا ضروري"
tokens = arabert_tokenizer.tokenize(text_preprocessed)
# Output: ['و+', 'لن', 'نبال', '##غ', 'إذا', 'قل', '+نا', 'إن', 'هاتف', 'أو', 'كمبيوتر', 'ال+', 'مكتب', 'في', 'زمن', '+نا', 'هذا', 'ضروري']