forked from opeyemibami/NLP-Tennis-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocessor.py
44 lines (35 loc) · 1.27 KB
/
preprocessor.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
41
42
43
44
import pandas as pd
import numpy as np
import string
import re
import json
import nltk
# nltk.download('wordnet')
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import load_model
lemmatizer = WordNetLemmatizer()
def tokenizer(entry):
tokens = entry.split()
re_punc = re.compile('[%s]' % re.escape(string.punctuation))
tokens = [re_punc.sub('', w) for w in tokens]
tokens = [word for word in tokens if word.isalpha()]
tokens = [lemmatizer.lemmatize(w.lower()) for w in tokens]
# stop_words = set(stopwords.words('english'))
# tokens = [w for w in tokens if not w in stop_words]
tokens = [word.lower() for word in tokens if len(word) > 1]
return tokens
def remove_stop_words_for_input(tokenizer,df,feature):
doc_without_stopwords = []
entry = df[feature][0]
tokens = tokenizer(entry)
doc_without_stopwords.append(' '.join(tokens))
df[feature] = doc_without_stopwords
return df
def encode_input_text(tokenizer_t,df,feature):
t = tokenizer_t
entry = entry = [df[feature][0]]
encoded = t.texts_to_sequences(entry)
padded = pad_sequences(encoded, maxlen=16, padding='post')
return padded