-
Notifications
You must be signed in to change notification settings - Fork 2
/
Preprocessing_joke_dataset.py
62 lines (54 loc) · 1.73 KB
/
Preprocessing_joke_dataset.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
58
59
60
61
62
import json
import numpy as np
import pandas as pd
import nltk
from nltk.stem import PorterStemmer
from nltk.tokenize import RegexpTokenizer
from nltk.tokenize import sent_tokenize, word_tokenize
from nltk.corpus import stopwords
import re
import itertools
from collections import Counter
stop_words=set(nltk.corpus.stopwords.words('english'))
stop_words = stopwords.words('english')
tokenizer = RegexpTokenizer(r'\w+')
porter=PorterStemmer()
def stem_sentence(sentence):
token = tokenizer.tokenize(sentence)
token = [word for word in token if word not in stop_words]
stemmed = []
for word in token:
#stemmed.append(porter.stem(word))
stemmed.append(word)
stemmed.append(" ")
return "".join(stemmed)
data = pd.read_csv('shortjokes.csv')
data = data['Joke']
jokes = []
for i in range(800):
jokes.append(data[i])
for i in range(len(jokes)):
jokes[i] = stem_sentence(jokes[i])
## word to vec
embeddings_index = {}
f = open('glove.6B.200d.txt', encoding="utf8")
for line in f:
values = line.split()
word = values[0]
coefs = np.asarray(values[1:], dtype='float32')
embeddings_index[word] = coefs
f.close()
vectorized = []
sequence_length = 263
temp = 0
for sentence in jokes:
vectorized.append([])
for word in sentence:
vectorized[-1].append(np.array(embeddings_index.get(word)).tolist())
if vectorized[-1][-1] is None or len(vectorized[-1][-1]) < 200:
vectorized[-1][-1] = np.zeros(200).tolist()
temp += 1
print(temp)
for i in range(sequence_length - len(sentence)):
vectorized[-1].append(np.zeros(200).tolist())
np.save('shortjokes_for_quotes_vec', np.array(vectorized))