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

small refactor

This commit is contained in:
Wang Bin
2015-02-26 14:42:16 +08:00
parent 60b2c9f763
commit 35094877da
3 changed files with 34 additions and 22 deletions

16
dict.go
View File

@@ -26,7 +26,13 @@ func DictPath(dictFileName string) (string, error) {
return dictFilePath, nil
}
func ParseDictFile(dictFile *os.File) (wtfs []*WordTagFreq, err error) {
func ParseDictFile(dictFilePath string) (wtfs []*WordTagFreq, err error) {
var dictFile *os.File
dictFile, err = os.Open(dictFilePath)
if err != nil {
return
}
scanner := bufio.NewScanner(dictFile)
for scanner.Scan() {
line := scanner.Text()
@@ -38,7 +44,7 @@ func ParseDictFile(dictFile *os.File) (wtfs []*WordTagFreq, err error) {
if length > 1 {
wtf.Freq, err = strconv.ParseFloat(fields[1], 64)
if err != nil {
return nil, err
return
}
}
if length > 2 {
@@ -46,8 +52,6 @@ func ParseDictFile(dictFile *os.File) (wtfs []*WordTagFreq, err error) {
}
wtfs = append(wtfs, wtf)
}
if err = scanner.Err(); err != nil {
return nil, err
}
return wtfs, nil
err = scanner.Err()
return
}

View File

@@ -41,6 +41,26 @@ func init() {
}
}
/*
func SetDictionary(dictFileName string) error {
err := jiebago.SetDictionary(dictFileName)
if err != nil {
return err
}
dictFilePath, err := jiebago.DictPath(dictFileName)
if err != nil {
return err
}
dictFile, err := os.Open(dictFilePath)
if err != nil {
return err
}
defer dictFile.Close()
wtfs, err := ParseDictFile(dictFile)
}
*/
func load_model(f_name string) error {
file, openError := os.Open(f_name)
if openError != nil {

20
trie.go
View File

@@ -96,13 +96,7 @@ func newTrie(dictFileName string) (*Trie, error) {
if !isDictCached {
trie = &Trie{Total: 0.0, Freq: make(map[string]float64)}
dictFile, err := os.Open(dictFilePath)
if err != nil {
return nil, err
}
defer dictFile.Close()
wtfs, err := ParseDictFile(dictFile)
wtfs, err := ParseDictFile(dictFilePath)
if err != nil {
return nil, err
}
@@ -147,13 +141,7 @@ func addWord(wtf *WordTagFreq) {
}
func LoadUserDict(dictFilePath string) error {
dictFile, err := os.Open(dictFilePath)
if err != nil {
return err
}
defer dictFile.Close()
wtfs, err := ParseDictFile(dictFile)
wtfs, err := ParseDictFile(dictFilePath)
if err != nil {
return err
}
@@ -163,7 +151,7 @@ func LoadUserDict(dictFilePath string) error {
return nil
}
func SetDictionary(dict_path string) (err error) {
T, err = newTrie(dict_path)
func SetDictionary(dictFileName string) (err error) {
T, err = newTrie(dictFileName)
return
}