-
Notifications
You must be signed in to change notification settings - Fork 0
/
naturalLP.py
40 lines (33 loc) · 1.19 KB
/
naturalLP.py
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
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
from nltk import pos_tag
def preprocess_text(text):
# Tokenization
tokens = word_tokenize(text)
# Remove stopwords
stop_words = set(stopwords.words("english"))
filtered_tokens = [token for token in tokens if token.lower() not in stop_words]
# Part-of-speech (POS) tagging
pos_tags = pos_tag(filtered_tokens)
# Lemmatization
lemmatizer = WordNetLemmatizer()
lemmatized_tokens = [lemmatizer.lemmatize(token, get_wordnet_pos(pos)) for token, pos in pos_tags]
return lemmatized_tokens
def get_wordnet_pos(tag):
# Map POS tag to WordNet POS tag
if tag.startswith('J'):
return nltk.corpus.wordnet.ADJ
elif tag.startswith('V'):
return nltk.corpus.wordnet.VERB
elif tag.startswith('N'):
return nltk.corpus.wordnet.NOUN
elif tag.startswith('R'):
return nltk.corpus.wordnet.ADV
else:
return nltk.corpus.wordnet.NOUN
# Example usage
text = "I am running a marathon today and feeling really excited!"
preprocessed_tokens = preprocess_text(text)
print(preprocessed_tokens)