-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLanguageProcessing.py
57 lines (46 loc) · 1.54 KB
/
LanguageProcessing.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
45
46
47
48
49
50
51
52
53
54
55
56
57
# Made by Kartik
flag = True
try:
import numpy as np
import nltk
from nltk.stem.porter import PorterStemmer
except ModuleNotFoundError:
print("The module named 'Numpy' or 'NLTK' not installed")
flag = False
if flag:
try:
Stemmer = PorterStemmer()
except:
Stemmer = False
def tokenize(sentence):
""" This function taken a sentence and return a tokenized sentence. """
return nltk.word_tokenize(sentence)
def stem(word):
""" This return list of word (stemmed) """
word = str(word)
if Stemmer == False:
print("Warning: Stemmer not working.")
return word.lower()
else:
return Stemmer.stem(word.lower())
def bag_of_words(tokenized_sentence, words):
""" This function creates a bagt of words for us. -> Document Vector """
sentence_words = [stem(word) for word in tokenized_sentence]
bag = np.zeros(len(words), dtype=np.float32)
for id_of_word, word in enumerate(words):
if word in sentence_words:
bag[id_of_word] = 1
return bag
if __name__ == '__main__':
# For Testing #
# sentence = "Kartik is a programmer. "
# new_sentence = tokenize(sentence)
# list1 = []
# for i in new_sentence:
# a = stem(i)
# list1.append(a)
# bag = bag_of_words(new_sentence, list1)
# print(new_sentence,"\n\n")
# print(list1,"\n\n")
# print(bag)
pass