-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstemming_lemmatization.py
More file actions
45 lines (35 loc) · 1.18 KB
/
stemming_lemmatization.py
File metadata and controls
45 lines (35 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#%%
import nltk
nltk.download('wordnet', quiet=True)
from nltk.stem import PorterStemmer
# create a PorterStemmer object
stemmer = PorterStemmer()
words = ["running", "ran", "easily", "fairly","go","went", "happily", "happiness"]
def stem_words(words):
"""
Stem the list of words using PorterStemmer.
"""
return [stemmer.stem(wo) for wo in words]
# finding stems of the words using PorterStemmer
print("Original words:", words)
print("------------------------")
print("Stems of the words:")
stemmed_words = stem_words(words)
print("Stemmed words:", stemmed_words)
# %%
# Lemmatisation using WordNetLemmatizer
from nltk.stem import WordNetLemmatizer
lemmatizer = WordNetLemmatizer()
words = ["running", "runner","ran", "easily","go","went", "fairly", "happily", "happiness"]
def lemmatize_words(words):
"""
Lemmatize the list of words using WordNetLemmatizer.
"""
return [lemmatizer.lemmatize(wo ,pos="v") for wo in words]
# finding lemmas of the words using WordNetLemmatizer
print("Original words:", words)
print("------------------------")
print("Lemmas of the words:")
lemmatized_words = lemmatize_words(words)
print("Lemmatized words:", lemmatized_words)
# %%