I am new to Scala and I have written a function which uses pattern matching to filter words based on some conditions. It seems to work correctly but I am suspect that I haven't used the Scala pattern matching in the best way. I think this because each of my cases contain an if
statement. I have not seen any examples where people use if
statements in pattern matching functions so I'm thinking there might be a better way of doing this. Or maybe this particular usage does not fit well with pattern matching.
def getImportance(token:String,stopWords:Set[String])={
token match{
case t if t.length()==1 => 0
case t if t.length()>15 => 0
case t if t.matches("\\p{Punct}+") => 0
case t if stopWords.contains(t.toLowerCase()) => 0
case _ => 1
}
}