Scala中是否存在实现difflib.SequenceMatcher
的内容?我需要将我的一些生产代码从Python转换为Scala,但不想使用会改变SequenceMatcher先前输出的东西。
非常感谢任何建议。
答案 0 :(得分:2)
经过一番搜索,我找不到任何东西,所以我研究了SequenceMatcher并编写了它。在Scala中它实际上非常简单。请随意使用或改进。
/**
* Class SequenceMatcher - A simplified implimetation of Python's SequenceMatcher
* Currently only supports strings and ratio()
* @version 0.1
* @param stringA - first string
* @param stringB - second string
*/
class SequenceMatcher(stringA: String, stringB: String) {
/**
* ratio()
* @return Return a measure of the sequences’ similarity as a float in the range [0, 1]
*/
def ratio(): Double = {
(2.0 * numOfMatches()) / (stringA.length() + stringB.length())
}
/**
* numOfMatches()
* @return number of characters that match in the two strings
*/
def numOfMatches(): Long = {
stringA.intersect(stringB).length()
}
}