2021-07-26 19:39:24 +02:00
|
|
|
import nltk
|
|
|
|
import numpy as np
|
|
|
|
from nltk.stem.porter import PorterStemmer
|
|
|
|
|
2021-07-27 17:44:51 +02:00
|
|
|
from jarvis.utils import languages_utils
|
2021-07-26 21:49:03 +02:00
|
|
|
|
2021-07-26 19:39:24 +02:00
|
|
|
stemmer = PorterStemmer()
|
|
|
|
|
|
|
|
|
2021-07-26 21:49:03 +02:00
|
|
|
# TODO : have a look to replace nltk by spacy or the other way (use only one of them)
|
|
|
|
|
2021-07-26 19:39:24 +02:00
|
|
|
def tokenize(sentence):
|
|
|
|
"""
|
|
|
|
split sentence into array of words/tokens
|
|
|
|
a token can be a word or punctuation character, or number
|
|
|
|
"""
|
2021-07-26 21:49:03 +02:00
|
|
|
# English, Danish, Estonian, French, Greek, Norwegian, Portuguese, Spanish, Turkish,
|
|
|
|
# Czech, Dutch, Finnish, German, Italian, Polish, Slovene, and Swedish
|
|
|
|
|
|
|
|
return nltk.word_tokenize(sentence,
|
2021-07-27 16:27:55 +02:00
|
|
|
language=languages_utils.get_language_full_name())
|
2021-07-26 19:39:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
def stem(word):
|
|
|
|
"""
|
|
|
|
stemming = find the root form of the word
|
|
|
|
examples:
|
|
|
|
words = ["organize", "organizes", "organizing"]
|
|
|
|
words = [stem(w) for w in words]
|
|
|
|
-> ["organ", "organ", "organ"]
|
|
|
|
"""
|
|
|
|
return stemmer.stem(word.lower())
|
|
|
|
|
|
|
|
|
|
|
|
def bag_of_words(tokenized_sentence, words):
|
|
|
|
"""
|
|
|
|
return bag of words array:
|
|
|
|
1 for each known word that exists in the sentence, 0 otherwise
|
|
|
|
example:
|
|
|
|
sentence = ["hello", "how", "are", "you"]
|
|
|
|
words = ["hi", "hello", "I", "you", "bye", "thank", "cool"]
|
|
|
|
bog = [ 0 , 1 , 0 , 1 , 0 , 0 , 0]
|
|
|
|
"""
|
|
|
|
# stem each word
|
|
|
|
sentence_words = [stem(word) for word in tokenized_sentence]
|
|
|
|
# initialize bag with 0 for each word
|
|
|
|
bag = np.zeros(len(words), dtype='float32')
|
|
|
|
for idx, w in enumerate(words):
|
|
|
|
if w in sentence_words:
|
|
|
|
bag[idx] = 1
|
|
|
|
|
|
|
|
return bag
|