1
0
mirror of https://github.com/fumiama/jieba.git synced 2026-06-05 00:32:51 +08:00

using bufio.Scanner to reading files, this the recommended way since Go v1.1

This commit is contained in:
Wang Bin
2014-08-11 16:43:38 +08:00
parent b46a8dd73d
commit bd12e7682d

View File

@@ -45,18 +45,19 @@ func newTopTrie(filename string) (*TopTrie, error) {
}
defer file.Close()
reader := bufio.NewReader(file)
for {
line, readError := reader.ReadString('\n')
if readError != nil && len(line) == 0 {
break
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
words := strings.Split(line, " ")
word, freqStr := words[0], words[1]
freq, _ := strconv.ParseFloat(freqStr, 64)
topTrie.Total += freq
topTrie.addWord(word, freq)
}
if scanErr := scanner.Err(); scanErr != nil {
return nil, scanErr
}
var val float64
for key := range topTrie.Freq {
val = math.Log(topTrie.Freq[key] / topTrie.Total)