Skip to content

Instantly share code, notes, and snippets.

@shubham0204
Created May 19, 2019 12:32
Show Gist options
  • Save shubham0204/0c01da15c5d440fc21e1bdc2f431c4d3 to your computer and use it in GitHub Desktop.
Save shubham0204/0c01da15c5d440fc21e1bdc2f431c4d3 to your computer and use it in GitHub Desktop.
companion object {
val CLASS_POSITIVE = 0 // Spam
val CLASS_NEGATIVE = 1 // Ham
private val englishStopWords = arrayOf(
"i", "me", "my", "myself", "we", "our", "ours", "ourselves", "you", "your", "yours", "yourself", "yourselves",
"he", "him", "his", "himself", "she", "her", "hers", "herself", "it", "its", "itself", "they", "them", "their",
"theirs", "themselves", "what", "which", "who", "whom", "this", "that", "these", "those", "am", "is", "are", "was",
"were", "be", "been", "being", "have", "has", "had", "having", "do", "does", "did", "doing", "a", "an", "the",
"and", "but", "if", "or", "because", "as", "until", "while", "of", "at", "by", "for", "with", "about", "against",
"between", "into", "through", "during", "before", "after", "above", "below", "to", "from", "up", "down", "in",
"out", "on", "off", "over", "under", "again", "further", "then", "once", "here", "there", "when", "where", "why",
"how", "all", "any", "both", "each", "few", "more", "most", "other", "some", "such", "no",
"nor", "not", "only", "own", "same", "so", "than", "too", "very", "s", "t", "can", "will", "just", "don", "should",
"now" , "hello" , "hi" , "dear" , "respected" , "thank"
)
fun getTokens( text : String ) : Array<String> {
val tokens = text.toLowerCase().split( " " )
val filteredTokens = ArrayList<String>()
for ( i in 0 until tokens.count() ) {
if ( !tokens[i].trim().isBlank() ) {
filteredTokens.add( tokens[ i ] )
}
}
val stopWordRemovedTokens = tokens.map { Regex("[^a-zA-Z]").replace( it , "") }
as ArrayList<String>
stopWordRemovedTokens.removeAll {
englishStopWords.contains( it ) or it.trim().isBlank()
}
return stopWordRemovedTokens.distinct().toTypedArray()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment