我对Scala非常陌生,尤其是令人敬畏的模式匹配。但是,我发现这个代码不起作用。我创建了一个包含匹配词的“词典”,然后我使用了理解,因此每一行都会与词典中的词匹配。Scala模式匹配无法匹配特定字
这是用来创建正则表达式的地图。
val dictionary = Map(
"""will""" -> 1,
"""going to""" -> 2,
"""future""" -> 3
)
这是主要的for循环:
for (
ln <- file.getLines();
(word, loc) <- dictionary
){
val regex = word.r
ln match {
case regex(ln) => {totalLine += 1
println("Match detected: " + word)
val originalElem = doc.BOWVector(dictionary.get(ln).get)
doc.BOWVector.updated(dictionary.get(ln).get, originalElem+1) //vector is updated
}
case _ => {totalLine += 1}
}
}
当我使用ln.contains("will")
和它的作品!然而,正则表达式不起作用。为什么?
谢谢!其实'.unanchored'做了很大的改变,并使这个程序的工作! –