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

refactor, removed duplicated codes

This commit is contained in:
Wang Bin
2015-02-26 17:59:34 +08:00
parent ed0bc44921
commit dd7e4ec802
2 changed files with 13 additions and 29 deletions

View File

@@ -1,12 +1,8 @@
package analyse package analyse
import ( import (
"bufio" "github.com/wangbin/jiebago"
"os"
"path/filepath"
"sort" "sort"
"strconv"
"strings"
) )
var ( var (
@@ -33,39 +29,27 @@ func (loader *IDFLoader) newPath(idfFilePath string) error {
if loader.Path == idfFilePath { if loader.Path == idfFilePath {
return nil return nil
} }
idfFile, err := os.Open(idfFilePath) wtfs, err := jiebago.ParseDictFile(idfFilePath)
if err != nil { if err != nil {
return err return err
} }
scanner := bufio.NewScanner(idfFile)
freqs := make([]float64, 0) freqs := make([]float64, 0)
for scanner.Scan() {
line := scanner.Text() for _, wtf := range wtfs {
words := strings.Split(line, " ") loader.Freq[wtf.Word] = wtf.Freq
word, freqStr := words[0], words[1] freqs = append(freqs, wtf.Freq)
freq, err := strconv.ParseFloat(freqStr, 64)
if err != nil {
continue
}
loader.Freq[word] = freq
freqs = append(freqs, freq)
}
if err := scanner.Err(); err != nil {
return err
} }
sort.Float64s(freqs) sort.Float64s(freqs)
loader.Median = freqs[len(freqs)/2] loader.Median = freqs[len(freqs)/2]
return nil return nil
} }
func SetIdf(idfFilePath string) error { func SetIdf(idfFileName string) error {
if !filepath.IsAbs(idfFilePath) { idfFilePath, err := jiebago.DictPath(idfFileName)
pwd, err := os.Getwd() if err != nil {
if err != nil { return err
return err
}
idfFilePath = filepath.Clean(filepath.Join(pwd, idfFilePath))
} }
return idfLoader.newPath(idfFilePath) return idfLoader.newPath(idfFilePath)
} }

View File

@@ -32,7 +32,7 @@ func ParseDictFile(dictFilePath string) (wtfs []*WordTagFreq, err error) {
if err != nil { if err != nil {
return return
} }
defer dictFile.Close()
scanner := bufio.NewScanner(dictFile) scanner := bufio.NewScanner(dictFile)
for scanner.Scan() { for scanner.Scan() {
line := scanner.Text() line := scanner.Text()