-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
28 lines (24 loc) · 1.02 KB
/
utils.py
File metadata and controls
28 lines (24 loc) · 1.02 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
import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
from string import digits
from gensim.models.doc2vec import TaggedDocument
from nltk.tokenize import word_tokenize
# Function to preprocess the text, lowercasing, removing digits and stopwords, and lemmatizing
def text_preprocessing(raw_text):
# Tokenize and lowercase text
lower = CountVectorizer().build_tokenizer()(raw_text.lower())
# Remove digits
no_digits = [w for w in lower if not w.isdigit()]
# Remove stopwords
filtered = [w for w in no_digits if not w in stopwords.words('english')]
# Lemmatize the text
lemmatizer = WordNetLemmatizer()
lemma = [lemmatizer.lemmatize(w) for w in filtered]
return ' '.join(lemma)
# Function to tag the documents for training
def tag_documents(text):
# Tag documents with index
tagged_docs = [TaggedDocument(words=word_tokenize(doc), tags=[i]) for i, doc in enumerate(text)]
return tagged_docs