-
Notifications
You must be signed in to change notification settings - Fork 1
/
preprocessing.py
71 lines (46 loc) · 1.94 KB
/
preprocessing.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
63
64
65
66
67
68
69
70
71
import re
import nltk
import pickle
import pandas as pd
from nltk.stem import WordNetLemmatizer
from nltk.stem.lancaster import LancasterStemmer
from nltk.stem.porter import PorterStemmer
wordnet_lemmatizer = WordNetLemmatizer()
lancaster_stemmer = LancasterStemmer()
porter_stemmer = PorterStemmer()
#df_all_word_counts = pd.read_pickle('EN-DE_word_occurrence_sample_2281331.pkl')
#df_all_word_counts = pd.read_pickle('twitter_word_occurrence_sample_377265.pkl')
#df_all_word_counts = pd.read_pickle('twitter_word_occurrence_sample_377265_lem.pkl')
df_all_word_counts = pd.read_pickle('twitter_word_occurrence_sample_2601244_lem.pkl')
#df_all_word_counts = pd.read_pickle('twitter_word_occurrence_sample_333769_lem_questions.pkl')
def checkWordOccurrence(x, occurence, word_set):
return set(x) & word_set
def checkWordOccurrenceLength(x, occurence, word_set):
return len(set(x) & word_set)
def checkChars(x):
return re.sub(r'([^\s\w.?!]|_)+', r' ', x).lower() #What does this do again?
def remove_non_ascii(text):
return re.sub(r'[^\x00-\x7F]+','', text)
def appendEOS(x):
return x + ['<EOS>']
def countTokens(x):
return len(nltk.word_tokenize(str(x)))
def nltkStem(words):
stem_sent = [porter_stemmer.stem(word) for word in words]
return stem_sent, len(stem_sent)
def nltkLem(words):
lem_sent = [wordnet_lemmatizer.lemmatize(word) for word in words]
return lem_sent, len(lem_sent)
def checkAlphaLower(words):
words = nltk.word_tokenize(words)
checked_text = \
' '.join([''.join([char for char in word if char.isalpha() or char=='?']) for word in words])
checked_text = nltk.word_tokenize(checked_text)
tokens = appendEOS([i.lower() for i in checked_text])
return tokens, len(tokens)
def reverseOrdering(x):
return [x[len(x)-i-1] for i in range(len(x))]
def makeSet(x):
return set(x)
def nltkNGram(text, n_gram):
return list(nltk.ngrams(text, n_gram))