1
0
mirror of https://github.com/fumiama/jieba.git synced 2026-06-21 03:10:55 +08:00

removed dict.go, functions move to util.go, also use interface to simplify code

This commit is contained in:
Wang Bin
2015-03-25 18:28:37 +08:00
parent 7fe5e7d4c4
commit 59da5b5e3a
7 changed files with 57 additions and 115 deletions

63
util.go
View File

@@ -1,15 +1,15 @@
package jiebago
import (
// "bufio"
"bufio"
// "crypto/md5"
// "encoding/gob"
// "fmt"
"os"
"path/filepath"
"regexp"
// "strconv"
// "strings"
"strconv"
"strings"
)
func DictPath(dictFileName string) (string, error) {
@@ -25,6 +25,35 @@ func DictPath(dictFileName string) (string, error) {
return dictFilePath, nil
}
func LoadDict(l DictLoader, dictFilePath string, usingFlag bool) error {
dictFile, err := os.Open(dictFilePath)
if err != nil {
return err
}
defer dictFile.Close()
scanner := bufio.NewScanner(dictFile)
var entry *Entry
var line string
var fields []string
for scanner.Scan() {
line = scanner.Text()
fields = strings.Split(line, " ")
entry = NewEntry()
entry.Word = strings.Replace(fields[0], "\ufeff", "", 1)
if length := len(fields); length > 1 {
entry.Freq, err = strconv.ParseFloat(fields[1], 64)
if err != nil {
return err
}
if usingFlag && length > 2 {
entry.Flag = fields[2]
}
}
l.AddEntry(entry)
}
return scanner.Err()
}
/*
func cachePath(dictPath string) string {
return filepath.Join(os.TempDir(),
@@ -56,34 +85,6 @@ func load(cachePath string, d DictLoader) error {
return dec.Decode(&d)
}
func read(dictPath, d DictLoader, pos bool) error {
dictFile, err := os.Open(dictFilePath)
if err != nil {
return err
}
defer dictFile.Close()
scanner := bufio.NewScanner(dictFile)
var token *Token
var line string
var fields []string
for scanner.Scan() {
line = scanner.Text()
fields = strings.Split(line, " ")
token = &Token{Term: strings.Replace(fields[0], "\ufeff", "", 1)}
if length := len(fields); length > 1 {
token.Freq, err = strconv.ParseFloat(fields[1], 64)
if err != nil {
return err
}
if pos && length > 2 {
token.Pos = fields[2]
}
}
d.Add(token)
}
return scanner.Err()
}
func dump(cachePath string, d DictLoader) error {
cacheFile, err = os.OpenFile(cachePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {