-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cleaning.py
54 lines (50 loc) · 1.41 KB
/
Cleaning.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
import nltk
from nltk.stem import *
import re
def tokenize(text):
"""
Apply the nltk word_tokenize on the text
This function call a TreeBank tokenizer
https://www.nltk.org/_modules/nltk/tokenize/treebank.html#TreebankWordTokenizer
"""
list_token=nltk.word_tokenize(text)
return list_token
def get_Article(link):
"""
Transform an filename into a text
"""
fArticle = open(link,'r')
rawText = fArticle.read()
return rawText
def stem(tokens):
"""
Use the PorterStemmer of nltk on the list of token
https://www.nltk.org/_modules/nltk/stem/porter.html#PorterStemmer
Return:
List of token stemmed
"""
list_stem = []
stemmer = PorterStemmer()
for token in tokens:
list_stem.append(stemmer.stem(token))
return list_stem
def particle_removal(tokens):
"""
Remove the weird characters of the token list.
"""
cleaned_list = []
regex = re.compile('[-=@_!#$%^&*()<>?,"/\|}{~:]')
for token in tokens:
if (regex.search(token) == None):
if (len(token)>2):
cleaned_list.append(token)
return cleaned_list
def clean(link):
"""
Apply every operations on a filename
"""
text = get_Article(link)
tokens=tokenize(text)
stemmed_token = stem(tokens)
big_words = particle_removal(stemmed_token)
print(big_words)