1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-08 06:32:28 +08:00

feat: add WordCount

This commit is contained in:
dudaodong
2023-02-21 14:27:00 +08:00
parent ed98ad53ec
commit ec740e442c
3 changed files with 73 additions and 0 deletions

View File

@@ -329,3 +329,34 @@ func SplitWords(s string) []string {
return words
}
// WordCount return the number of meaningful word, word only contains alphabetic characters.
// Play: todo
func WordCount(s string) int {
var r rune
var size, count int
isWord := false
for len(s) > 0 {
r, size = utf8.DecodeRuneInString(s)
switch {
case isLetter(r):
if !isWord {
isWord = true
count++
}
case isWord && (r == '\'' || r == '-'):
// is word
default:
isWord = false
}
s = s[size:]
}
return count
}