GoFrame gstr-相似計算

2022-04-11 17:51 更新

Levenshtein

  • 說明:?Levenshtein?計算兩個字符串之間的?Levenshtein?距離。
  • 格式:

Levenshtein(str1, str2 string, costIns, costRep, costDel int) int

  • 示例:

func ExampleLevenshtein() {
	var (
		str1    = "Hello World"
		str2    = "hallo World"
		costIns = 1
		costRep = 1
		costDel = 1
		result  = gstr.Levenshtein(str1, str2, costIns, costRep, costDel)
	)
	fmt.Println(result)

	// Output:
	// 2
}

SimilarText

  • 說明:?SimilarText?計算兩個字符串之間的相似度。  
  • 格式:

SimilarText(first, second string, percent *float64) int

  • 示例:

func ExampleSimilarText() {
	var (
		first   = `AaBbCcDd`
		second  = `ad`
		percent = 0.80
		result  = gstr.SimilarText(first, second, &percent)
	)
	fmt.Println(result)

	// Output:
	// 2
}

Soundex

  • 說明:?Soundex?用于計算字符串的?Soundex?鍵。  
  • 格式:

Soundex(str string) string

  • 示例:

func ExampleSoundex() {
	var (
		str1    = `Hello`
		str2    = `Hallo`
		result1 = gstr.Soundex(str1)
		result2 = gstr.Soundex(str2)
	)
	fmt.Println(result1, result2)

	// Output:
	// H400 H400
}


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號