frequency works
This commit is contained in:
parent
d8ea359d48
commit
925129f76e
3 changed files with 28 additions and 18 deletions
|
@ -1,3 +1,5 @@
|
||||||
module frequency
|
module frequency
|
||||||
|
|
||||||
go 1.18
|
go 1.18
|
||||||
|
|
||||||
|
require github.com/dariubs/percent v1.0.0
|
||||||
|
|
2
Lab04/code/part01/go.sum
Normal file
2
Lab04/code/part01/go.sum
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
github.com/dariubs/percent v1.0.0 h1:fY8q40FRYaCiFZ0gTOa73Cmp21hS32w+tSSmqbGnUzc=
|
||||||
|
github.com/dariubs/percent v1.0.0/go.mod h1:NDZpkezJ8QqyIW/510MywB5T2KdC8v/0oTlEoPcMsRM=
|
|
@ -1,27 +1,33 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/dariubs/percent"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func wordCount(str string) map[string]int {
|
var alphabet = []rune{
|
||||||
wordList := strings.Fields(str)
|
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
|
||||||
counts := make(map[string]int)
|
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
|
||||||
for _, word := range wordList {
|
}
|
||||||
_, ok := counts[word]
|
|
||||||
if ok {
|
func letterCount(inputText string) {
|
||||||
counts[word] += 1
|
count := make([]int, len(alphabet))
|
||||||
} else {
|
frequency := make([]float64, len(alphabet))
|
||||||
counts[word] = 1
|
totalCount := 0
|
||||||
}
|
for index, letter := range alphabet {
|
||||||
}
|
count[index] = strings.Count(inputText, string(letter))
|
||||||
return counts
|
count[index] += strings.Count(inputText, string(letter+32))
|
||||||
|
totalCount += count[index]
|
||||||
|
}
|
||||||
|
for index := range alphabet {
|
||||||
|
frequency[index] = percent.PercentOf(count[index], totalCount)
|
||||||
|
}
|
||||||
|
for index, letter := range alphabet {
|
||||||
|
fmt.Printf("The letter %c (%c) occurs %d times in the text and the frequency in percent is %0.2f\n", letter, letter+32, count[index], frequency[index])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
strLine := "Australia Canada Germany Australia Japan Canada"
|
letterCount("This is a test to check for something.Zz")
|
||||||
for index,element := range wordCount(strLine){
|
}
|
||||||
fmt.Println(index,"=>",element)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Reference in a new issue