forked from geosakel77/auth_bigdata_ex_sbt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextCleaner.scala
281 lines (218 loc) · 8.57 KB
/
TextCleaner.scala
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/**
* Created by Alex on 15/1/2017.
*/
import java.io.{File, FileInputStream}
import org.apache.poi.hssf.usermodel.HSSFWorkbook
import scala.collection.immutable.Set
class TextCleaner(){
private var select = 0;
private val stopwords =scala.io.Source.fromFile(System.getProperty("user.dir")+"/preprocess/stopWords.txt").getLines.toSet
//------------------------------
//positive count : 2006
//negative words : 4783
private val negativeWords1 = scala.io.Source.fromFile(System.getProperty("user.dir")+"/preprocess/sentimentLexicon/negative-words.txt").getLines.toSet
private val positiveWords1 = scala.io.Source.fromFile(System.getProperty("user.dir")+"/preprocess/sentimentLexicon/positive-words.txt").getLines.toSet
//------------------------------
//pos: 1638
//neg: 2006
private var positiveHarvard = Set.empty[String]
private var negativeHarvard = Set.empty[String]
//3040
private var harvardCompleteStemmed= Set.empty[String]
//-------------------------------
private val stemmer = new Stemmer
def setup(select: Int): Unit ={
this.select = select
if(select==2){
readFromXls(System.getProperty("user.dir")+"/preprocess/inquirerbasic.xls")
println("words in stemmed positive: "+ positiveHarvard.size)
println("words in stemmed negative: "+ negativeHarvard.size)
harvardCompleteStemmed ++= positiveHarvard
harvardCompleteStemmed ++= negativeHarvard
println("words in stemmed complete: "+ harvardCompleteStemmed.size)
}else if(select ==3){
readFromXls(System.getProperty("user.dir")+"/preprocess/inquirerbasic.xls")
}else if(select ==4){
readFromXls(System.getProperty("user.dir")+"/preprocess/inquirerbasic.xls")
}
}
/**
* prints the words that are in the first sentiment lexicon
*/
def printsentimentwords (): Unit ={
println("Positive words:")
positiveWords1.foreach(w=> {
if(!w.forall(c => c.isLetter)){
println(w)
}
})
println("Negative words:")
negativeWords1.foreach(w => {
if(!w.forall(c => c.isLetter)){
println(w)
}
})
println("positive words : " + positiveWords1.size)
println("negative words : "+ negativeWords1.size)
}
def clearText(line: String): String= {
if(select ==0){
return clean0Simple(line)
}else if(select ==1){
return clean1Sentiment(line)
}else if(select ==2){
return clean2HarvardLexiconComplete(line)
}else if(select ==3){
return clean3HarvardLexiconPositive(line)
}else if(select==4){
return clean4HarvardLexiconNegative(line)
}
println("default selection == 0")
return clean0Simple(line)
}
/**
* Vanilla cleaning
*
* To lower case.
* Removes punctuation except for apostrophe and space.
* Removes stopwords.
* Keeps the stem of the remaining words (and remove again the stopwords, (e.g. throwing -(stem)-> throw \in stopwords)).
*
*
* @param line, the document to get cleaned
* @return
*/
private def clean0Simple(line:String): String ={
var cleanline = line.toLowerCase()
cleanline = removePunctuation(cleanline, List('\'', ' '))
cleanline = cleanline.split(" ").filter(word => !stopwords.contains(word)).mkString(" ")
return stemmer.stemLine(cleanline).filter(w => !stopwords.contains(w)).mkString(" ")
}
/**
* Keeps only the words that have positive/negative meaning.
* From the first lexicon which isn't too great.
*
* To lower case.
* Remove punctuation except for -, +, *
* Remove all the words that aren't in the sentiment lexicon.
* Stem them ? mallon oxi giati exei polles lexeis to lexico pou exoun katalikseis ing klp
*
* @param line
* @return
*/
private def clean1Sentiment(line:String): String = {
var cleanline = line.toLowerCase
cleanline = removePunctuation(cleanline, List('-','+','*'))
cleanline = cleanline.split(" ").filter(word => negativeWords1.contains(word)||positiveWords1.contains(word)).mkString(" ")
//cleanline = stemmer.stemLine(cleanline).filter(w => !stopwords.contains(w)).mkString(" ")
return cleanline
}
/**
* The line to lower case.
* Remove special chars except for apostrophe and spaces.
* Remove stopwords.
* Stem the words.
* If a stemmed word isn't a stopword, AND the word is in the stemmed harvard lexicon, keep it.
*
* @param line
* @return
*/
private def clean2HarvardLexiconComplete(line:String): String ={
var cleanline = line.toLowerCase()
cleanline = removePunctuation(cleanline, List('\'', ' '))
cleanline = cleanline.split(" ").filter(word => !stopwords.contains(word)).mkString(" ")
return stemmer.stemLine(cleanline).filter(w => !stopwords.contains(w) && harvardCompleteStemmed.contains(w)).mkString(" ")
}
/**
* Keeps only the positive words from the Harvard lexicon
*
* @param line
* @return
*/
private def clean3HarvardLexiconPositive(line:String): String ={
var cleanline = line.toLowerCase()
cleanline = removePunctuation(cleanline, List('\'', ' '))
cleanline = cleanline.split(" ").filter(word => !stopwords.contains(word)).mkString(" ")
return stemmer.stemLine(cleanline).filter(w => !stopwords.contains(w) && positiveHarvard.contains(w)).mkString(" ")
}
/**
* Only the negative words from Harvard lexicon
*
* @param line
* @return
*/
private def clean4HarvardLexiconNegative(line:String): String ={
var cleanline = line.toLowerCase()
cleanline = removePunctuation(cleanline, List('\'', ' '))
cleanline = cleanline.split(" ").filter(word => !stopwords.contains(word)).mkString(" ")
return stemmer.stemLine(cleanline).filter(w => !stopwords.contains(w) && negativeHarvard.contains(w)).mkString(" ")
}
/**
* Removes all the characters except for letters, spaces, and the characters that are given in the allowedSpecialChars list
* and returns the resulted string.
*
* @param line
* @param allowedSpecialChars
* @return
*/
private def removePunctuation(line: String, allowedSpecialChars : List[Char]): String ={
return line.toCharArray.filter(c => {allowedSpecialChars.contains(c) || c.isLetter|| c==' '}).mkString
}
/**
* Finds and prints the total number of words in the data.csv, as well as the testdata.csv.
* Also prints the number of unique words in both files.
*
* @param csvfilename
*/
def uniqueWordsCSV(csvfilename:String): Unit ={
val wordlist= scala.io.Source.fromFile(csvfilename).getLines.toList.flatMap(line =>{
line.split(",")(0).toCharArray.filter(c => c!='\"').mkString("").split(" ")//removing the quotes from the csv file
})
println("Total word on document "+ csvfilename+ " are :"+ wordlist.size+" , and distinct words: "+ wordlist.toSet.size)
val wordlistTest= scala.io.Source.fromFile("test"+csvfilename).getLines.toList.flatMap(line =>{
line.split(",")(0).toCharArray.filter(c => c!='\"').mkString("").split(" ")//removing the quotes from the csv file
})
println("Total word on document test"+ csvfilename+ " are :"+ wordlistTest.size+" , and distinct words: "+ wordlistTest.toSet.size)
}
/**
* Read words from sentiment lexicon "Inquirer", and puts the words on the 4 "Global" sets
*
* @param filename
*/
private def readFromXls(filename:String): Unit ={
val file = new FileInputStream(new File(filename))
val workbook = new HSSFWorkbook(file)
val sheet = workbook.getSheetAt(0)
val rowIterator = sheet.iterator()
while(rowIterator.hasNext){
val cellIterator = rowIterator.next().cellIterator()
var counter =1
var word = ""
var tag = ""
while(cellIterator.hasNext){
if (counter== 1){
word= stemmer.stemWord(cellIterator.next().toString.split('#')(0).toLowerCase)
}
tag = cellIterator.next().toString
if(tag.compareToIgnoreCase("positiv")==0 || tag.compareToIgnoreCase("pstv")==0){
positiveHarvard+= word
}else if(tag.compareToIgnoreCase("negativ")==0 || tag.compareToIgnoreCase("ngtv")==0){
negativeHarvard+=word
}
counter+=1
}
}
}
/**
* print the words that have special chars
*
* THere was none!
*
*/
def printWordsWithSpecialChars(): Unit ={
println("WORDS POSITIVE H4 WITH SPECIAL CHARS : ")
positiveHarvard.filter( w => !w.toCharArray.forall(c => c.isLetter)).foreach(w => println(w))
println("WORDS NEGATIVE H4 WITH SPECIAL CHARS : ")
negativeHarvard.filter( w => !w.toCharArray.forall(c => c.isLetter)).foreach(w => println(w))
}
}