# Pattern-matching function

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
}
}

• Zero-length words get assigned an Importance of 1. I'm not sure that is correct. – Bob Dalgleish May 28 '14 at 21:05
• 'token' is actually the output from a tokenizer which would not output zero-length words, so that case would never occur. – user1893354 May 29 '14 at 14:31
• Hmmm. I've heard that before. – Bob Dalgleish May 29 '14 at 19:23

I don't think that using pattern matching is necessary in this case, you only have a few simple conditions that can be better represented with a simple if. See what you think about this solution:
def getImportance(token: String, stopWords: Set[String]) =