-
-
Save velppa/ba8954ea07dcd4514789d7f3e2340a42 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"strings" | |
) | |
func CompareTwoStrings(stringOne, stringTwo string) float32 { | |
removeSpaces(&stringOne, &stringTwo) | |
if value := returnEarlyIfPossible(stringOne, stringTwo); value >= 0 { | |
return value | |
} | |
firstBigrams := make(map[string]int) | |
for i := 0; i < len(stringOne)-1; i++ { | |
a := fmt.Sprintf("%c", stringOne[i]) | |
b := fmt.Sprintf("%c", stringOne[i+1]) | |
bigram := a + b | |
var count int | |
if value, ok := firstBigrams[bigram]; ok { | |
count = value + 1 | |
} else { | |
count = 1 | |
} | |
firstBigrams[bigram] = count | |
} | |
var intersectionSize float32 | |
intersectionSize = 0 | |
for i := 0; i < len(stringTwo)-1; i++ { | |
a := fmt.Sprintf("%c", stringTwo[i]) | |
b := fmt.Sprintf("%c", stringTwo[i+1]) | |
bigram := a + b | |
var count int | |
if value, ok := firstBigrams[bigram]; ok { | |
count = value | |
} else { | |
count = 0 | |
} | |
if count > 0 { | |
firstBigrams[bigram] = count - 1 | |
intersectionSize = intersectionSize + 1 | |
} | |
} | |
return (2.0 * intersectionSize) / (float32(len(stringOne)) + float32(len(stringTwo)) - 2) | |
} | |
func main() { | |
fmt.Println(CompareTwoStrings("drinking coffee makes me happy", "drinking tea makes me happy")) | |
} | |
func removeSpaces(stringOne, stringTwo *string) { | |
*stringOne = strings.Replace(*stringOne, " ", "", -1) | |
*stringTwo = strings.Replace(*stringTwo, " ", "", -1) | |
} | |
func returnEarlyIfPossible(stringOne, stringTwo string) float32 { | |
// if both are empty strings | |
if len(stringOne) == 0 && len(stringTwo) == 0 { | |
return 1 | |
} | |
// if only one is empty string | |
if len(stringOne) == 0 || len(stringTwo) == 0 { | |
return 0 | |
} | |
// identical | |
if stringOne == stringTwo { | |
return 1 | |
} | |
// both are 1-letter strings | |
if len(stringOne) == 1 && len(stringTwo) == 1 { | |
return 0 | |
} | |
// if either is a 1-letter string | |
if len(stringOne) < 2 || len(stringTwo) < 2 { | |
return 0 | |
} | |
return -1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment