mirror of
https://github.com/fumiama/jieba.git
synced 2026-06-09 19:20:26 +08:00
refactor Cut function, make CutAll a seperate function, to simplify the logic of Cut function
This commit is contained in:
52
util.go
52
util.go
@@ -1,61 +1,9 @@
|
||||
package jiebago
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func dictPath(dictFileName string) (string, error) {
|
||||
if filepath.IsAbs(dictFileName) {
|
||||
return dictFileName, nil
|
||||
}
|
||||
var dictFilePath string
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return dictFilePath, err
|
||||
}
|
||||
dictFilePath = filepath.Clean(filepath.Join(cwd, dictFileName))
|
||||
return dictFilePath, nil
|
||||
}
|
||||
|
||||
func LoadDict(l DictLoader, dictFileName string, usingFlag bool) error {
|
||||
dictFilePath, err := dictPath(dictFileName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
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.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()
|
||||
}
|
||||
|
||||
// Split sentence using regular expression.
|
||||
func RegexpSplit(r *regexp.Regexp, sentence string) chan string {
|
||||
result := make(chan string)
|
||||
|
||||
Reference in New Issue
Block a user