1
0
mirror of https://github.com/fumiama/jieba.git synced 2026-06-23 04:30:44 +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:44:40 +08:00
parent b51ae37771
commit c8d195143d

View File

@@ -27,7 +27,10 @@ func init() {
_, filename, _, _ := runtime.Caller(1) _, filename, _, _ := runtime.Caller(1)
dict_dir := filepath.Dir(filepath.Dir(filename)) dict_dir := filepath.Dir(filepath.Dir(filename))
dict_path := filepath.Join(dict_dir, jiebago.Dictionary) dict_path := filepath.Join(dict_dir, jiebago.Dictionary)
load_model(dict_path) err := load_model(dict_path)
if err != nil {
panic(err)
}
} }
func load_model(f_name string) error { func load_model(f_name string) error {
@@ -37,16 +40,16 @@ func load_model(f_name string) error {
} }
defer file.Close() defer file.Close()
reader := bufio.NewReader(file) scanner := bufio.NewScanner(file)
for { for scanner.Scan() {
line, readError := reader.ReadString('\n') line := scanner.Text()
if readError != nil && len(line) == 0 {
break
}
words := strings.Split(strings.TrimSpace(line), " ") words := strings.Split(strings.TrimSpace(line), " ")
word, tag := words[0], words[2] word, tag := words[0], words[2]
WordTagTab[word] = tag WordTagTab[word] = tag
} }
if err := scanner.Err(); err != nil {
return err
}
return nil return nil
} }