[英]Is there an equivalent of Python's difflib.SequenceMatcher for Scala
有什么東西可以在Scala中實現difflib.SequenceMatcher
嗎? 我需要將我的一些生產代碼從Python轉換為Scala,但又不想使用會更改SequenceMatcher先前輸出的內容。
任何建議都非常感謝。
經過一番搜索,我什么都找不到,所以我研究了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()
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.