-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathpreprocessing.jl
More file actions
159 lines (133 loc) · 5.37 KB
/
preprocessing.jl
File metadata and controls
159 lines (133 loc) · 5.37 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
@testset "Preprocessing" begin
sample_text1 = "This is 1 MESSED 1 string!"
sample_text1_wo_punctuation = "This is 1 MESSED string"
sample_text1_wo_punctuation_numbers = "This is MESSED string"
sample_text1_wo_punctuation_numbers_case = "this is messed string"
sample_text1_wo_punctuation_numbers_case_az = "this is messed 1 string"
sample_texts = [
sample_text1,
sample_text1_wo_punctuation,
sample_text1_wo_punctuation_numbers,
sample_text1_wo_punctuation_numbers_case,
sample_text1_wo_punctuation_numbers_case_az
]
# This idiom is _really_ ugly since "OR" means "AND" here.
for str in sample_texts
sd = StringDocument(str)
prepare!(
sd,
strip_punctuation | strip_numbers | strip_case | strip_whitespace | strip_non_letters
)
@test isequal(strip(sd.text), "this is messed string")
end
# Need to only remove words at word boundaries
doc = Document("this is sample text")
remove_words!(doc, ["sample"])
@test isequal(doc.text, "this is text")
doc = Document("this is sample text")
prepare!(doc, strip_articles)
@test isequal(doc.text, "this is sample text")
doc = Document("this is sample text")
prepare!(doc, strip_definite_articles)
@test isequal(doc.text, "this is sample text")
doc = Document("this is sample text")
prepare!(doc, strip_indefinite_articles)
@test isequal(doc.text, "this is sample text")
doc = Document("this is sample text")
prepare!(doc, strip_prepositions)
@test isequal(doc.text, "this is sample text")
doc = Document("this is sample text")
prepare!(doc, strip_pronouns)
@test isequal(doc.text, "this is sample text")
doc = Document("this is sample text")
prepare!(doc, strip_stopwords)
@test isequal(strip(doc.text), "sample text")
doc = Document("this is sample text")
prepare!(doc, strip_whitespace)
@test isequal(doc.text, "this is sample text")
# stem!(sd)
# Do preprocessing on TokenDocument, NGramDocument, Corpus
d = NGramDocument("this is sample text")
@test haskey(d.ngrams, "sample")
remove_words!(d, ["sample"])
@test !haskey(d.ngrams, "sample")
d = StringDocument(
"""
<html>
<head>
<script language=\"javascript\"> x = 20; </script>
</head>
<body>
<h1>Hello</h1><a href=\"world\"> world</a>
</body>
</html>
"""
)
remove_html_tags!(d)
@test "Hello world" == strip(d.text)
style_html_doc = StringDocument(
"""
<html>
<head>
<script language=\"javascript\"> x = 20; </script>
</head>
<body>
<style>
.fake-style {
color: #00ff00;
}
</style>
<h1>Hello</h1><a href=\"world\"> world</a>
</body>
</html>
"""
)
remove_html_tags!(style_html_doc)
@test "Hello world" == strip(style_html_doc.text)
#Test #62
remove_corrupt_utf8("abc") == "abc"
remove_corrupt_utf8(String([0x43, 0xf0])) == "C "
#Test frequent_terms sparse_terms
crps = Corpus(StringDocument.(sample_texts))
@test isempty(setdiff(frequent_terms(crps), ["string", "is"]))
@test isempty(setdiff(sparse_terms(crps, 0.3), ["!"]))
#Tests strip_punctuation regex conditions
str = Document("These punctuations should be removed [-.,:;,!?'\"[](){}|\`#\$%@^&*_+<>")
answer = Document("These punctuations should be removed ")
prepare!(str, strip_punctuation)
@test isequal(str.text, answer.text)
str = Document("Intel(tm) Core i5-3300k, is a geat CPU! ")
answer = Document("Inteltm Core i53300k is a geat CPU ") #tests old implementation
prepare!(str, strip_punctuation)
@test isequal(str.text, answer.text)
#Tests no whitespace at end or begining
doc = Document(" this is sample text ")
prepare!(doc, strip_whitespace)
@test isequal(doc.text, "this is sample text")
doc = Document(" this is sample text ")
prepare!(doc, strip_whitespace)
@test isequal(doc.text, "this is sample text")
crps = Corpus(
[StringDocument(" Document 1"),
StringDocument(" Document 2 ")]
)
prepare!(crps, strip_whitespace)
@test isequal(crps[1].text, "Document 1")
@test isequal(crps[2].text, "Document 2")
crps = Corpus([StringDocument(" Hi there ! ")])
prepare!(crps, strip_html_tags | strip_whitespace | strip_non_letters)
@test isequal(crps[1].text, "Hi there")
end
@testset "strip_non_letters with Unicode" begin
samples = [
(" Wörterbuch für Ärzte! ", ["Wörterbuch", "für", "Ärzte"])
(" Проверим прочие алфавиты: αλφάβητο 字母 ! ", ["Проверим", "прочие", "алфавиты", "αλφάβητο", "字母"])
("123 الأبجدية 456", ["الأبجدية"])
# (" वर्णमाला ! ", "वर्णमाला")
]
for (sample, expected) in samples
crps = Corpus([StringDocument(sample)])
prepare!(crps, strip_html_tags | strip_whitespace | strip_non_letters)
@test isequal(crps[1].text, join(expected, ' '))
end
end