Loading the lesson...
In the print dialog, choose "Save as PDF" as the destination.
Advanced NLP - Lesson 2
Reducing text variation with stemming, lemmatization, and Arabic-specific normalization
lovely, luv, loveeeKey Concept
Stemming
ing from verbsfrom nltk.stem.porter import PorterStemmer
stemmer = PorterStemmer()
singles = [stemmer.stem(w) for w in plurals]
# 'caresses' -> 'caress', 'flies' -> 'fli', 'meeting' -> 'meet'flies to fli show stemming can produce non-wordsLemmatization
WordNet to find its dictionary rootfrom nltk.stem import WordNetLemmatizer
lemmatizer = WordNetLemmatizer()
lemmatizer.lemmatize("rocks") # "rock"
lemmatizer.lemmatize("better", pos="a") # "good"pos argument matters: default noun assumption vs. adjective formdef normalize(sentence):
sentence = re.sub("[إأآا]", "ا", sentence)
sentence = re.sub("ى", "ي", sentence)
sentence = re.sub("ؤ", "ء", sentence)
sentence = re.sub("ئ", "ء", sentence)
sentence = re.sub("ة", "ه", sentence)
return sentenceFunction
NLTK's Arabic stopword listdef removing_ar_stopwords(text):
words = word_tokenize(text)
return " ".join(w for w in words if w not in stopwords)Arabic Stemming
NLTK; strips prefixes and suffixes using Arabic-specific rulesfrom nltk import ISRIStemmer
st = ISRIStemmer()
stemmed = " ".join(st.stem(w) for w in word_tokenize(text))Arabic Stemming
from tashaphyne.stemming import ArabicLightStemmer
ArListem = ArabicLightStemmer()
ArListem.light_stem(word)
stem = ArListem.get_stem()Decision
| Approach | Speed | Word Validity | Arabic Tools |
|---|---|---|---|
| Stemming | Fast, rule-based | May destroy words | ISRIStemmer, Tashaphyne |
| Lemmatization | Slower, morphology-aware | Keeps real words | Farasa (external) |
pandasRaw hadith text
Remove stopwords
removing_ar_stopwords
Normalize characters
normalize
Stem
stemming_1, ISRIStemmer
Cleaned column
cleared = [removing_ar_stopwords(h) for h in hadiths]
cleared = [normalize(h) for h in cleared]
cleared = [stemming_1(h) for h in cleared] # ISRIStemmerPorterStemmer (English) and ISRIStemmer / Tashaphyne (Arabic) are common stemmersMahmoud Abas|Text Normalization