1
0
mirror of https://github.com/fumiama/jieba.git synced 2026-06-07 09:40:38 +08:00
Files
jieba/dict.go
2015-03-25 17:53:25 +08:00

39 lines
755 B
Go

package jiebago
import (
"bufio"
"os"
"strconv"
"strings"
)
func ParseDictFile(dictFilePath string) ([]*Entry, error) {
dictFile, err := os.Open(dictFilePath)
if err != nil {
return nil, err
}
defer dictFile.Close()
entries := make([]*Entry, 0)
scanner := bufio.NewScanner(dictFile)
for scanner.Scan() {
line := scanner.Text()
fields := strings.Split(line, " ")
length := len(fields)
word := fields[0]
word = strings.Replace(word, "\ufeff", "", 1)
entry := NewEntry()
entry.Word = word
if length > 1 {
entry.Freq, err = strconv.ParseFloat(fields[1], 64)
if err != nil {
return nil, err
}
}
if length > 2 {
entry.Flag = fields[2]
}
entries = append(entries, entry)
}
return entries, scanner.Err()
}