Skip to content

Commit

Permalink
Generate trigrams with a consistent ordering
Browse files Browse the repository at this point in the history
This fixes abadojack/whatlanggo/abadojack#9
  • Loading branch information
rylans committed Feb 28, 2018
1 parent 9f160b1 commit e7b4e92
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion trigrams.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package whatlanggo

import (
"sort"
"strings"
"unicode"
)

Expand All @@ -28,7 +29,12 @@ func getTrigramsWithPositions(text string) map[string]int {
i++
}

sort.SliceStable(trigrams, func(i, j int) bool { return trigrams[i].count < trigrams[j].count })
sort.SliceStable(trigrams, func(i, j int) bool {
if trigrams[i].count == trigrams[j].count {
return strings.Compare(trigrams[i].trigram, trigrams[j].trigram) < 0
}
return trigrams[i].count < trigrams[j].count
})

trigramsWithPositions := map[string]int{}

Expand Down

0 comments on commit e7b4e92

Please sign in to comment.