Пример #1
0
func (sentiment *Sentiment) Train(negFile string, posFile string) {
	negDocs := []string{}
	posDocs := []string{}
	file, reader := util.GetReader(negFile)
	defer file.Close()
	if reader != nil {
		err := util.ReadFile(reader, func(line string) {
			line = strings.Trim(line, " ")
			negDocs = append(negDocs, line)
		})
		if err != nil {
			log.Println("读取" + negFile + "出错")
		}
	}
	file, reader = util.GetReader(posFile)
	defer file.Close()
	if reader != nil {
		err := util.ReadFile(reader, func(line string) {
			line = strings.Trim(line, " ")
			posDocs = append(posDocs, line)
		})
		if err != nil {
			log.Println("读取"+posFile+"出错", err)
		}
	}
	data := []([]interface{}){}
	for _, sent := range negDocs {
		words := sentiment.Handle(sent)
		arr := make([]interface{}, 2)
		arr[0] = words
		arr[1] = "neg"
		data = append(data, arr)
	}
	log.Println("pos")
	for _, sent := range posDocs {
		words := sentiment.Handle(sent)
		log.Println(words)
		arr := make([]interface{}, 2)
		arr[0] = words
		arr[1] = "pos"
		data = append(data, arr)
	}
	sentiment.Bayes.Train(data)
}
Пример #2
0
func initPinyin(pyFile string) map[string]string {
	result := make(map[string]string)
	file, reader := util.GetReader(pyFile)
	defer file.Close()
	if reader != nil {
		err := util.ReadFile(reader, func(line string) {
			words := strings.SplitN(strings.Trim(line, " "), " ", 2)
			result[words[0]] = words[1]
		})
		if err != nil {
			log.Println("normal load pinyinFile err", err)
		}
	}
	return result
}
Пример #3
0
func initStop(stopFile string) []string {
	result := []string{}
	file, reader := util.GetReader(stopFile)
	defer file.Close()
	if reader != nil {
		err := util.ReadFile(reader, func(line string) {
			line = strings.Trim(line, " ")
			result = append(result, line)
		})
		if err != nil {
			log.Println("normal load stopFile err", err)
		}
	}
	return result
}
Пример #4
0
func (tag *Tag) Train(fileName string) {
	wordTags := list.New()
	file, reader := util.GetReader(fileName)
	defer file.Close()
	if reader != nil {
		err := util.ReadFile(reader, func(line string) {
			line = strings.Trim(line, " ")
			wts := list.New()
			wordTags.PushBack(wts)
			for _, str := range regexp.MustCompile("[^\\s]+").FindAllString(line, -1) {
				if strings.Trim(str, " ") != "" {
					wts.PushBack(pojo.WordTag{strings.Split(str, "/")[0], strings.Split(str, "/")[1]})
				}
			}
		})
		if err != nil {
			log.Println("seg train err", err)
		}
		tag.Tnt.Train(wordTags)

	}
}