Exemplo n.º 1
0
func (m Model) NewLattice() (Lattice, error) {
	lattice := C.mecab_model_new_lattice(m.model)
	if lattice == nil {
		return Lattice{}, errors.New("lattice is not created")
	}
	return Lattice{lattice: lattice}, nil
}
Exemplo n.º 2
0
// Parses a sentence using mecab
func mecabParser(q chan mecabRequest) {
	model := C.mecab_model_new2(C.CString(""))
	if model == nil {
		log.Panicln("Can't create a mecab model")
	}
	defer C.mecab_model_destroy(model)

	mecab := C.mecab_model_new_tagger(model)
	if mecab == nil {
		log.Panicln("Can't create a mecab tagger")
	}
	defer C.mecab_destroy(mecab)

	lattice := C.mecab_model_new_lattice(model)
	if lattice == nil {
		log.Panicln("Can't create a mecab lattice")
	}
	defer C.mecab_lattice_destroy(lattice)

	for {
		req := <-q

		res := make([]mecabResult, 0)
		C.mecab_lattice_set_sentence(lattice, C.CString(req.Sentence))
		C.mecab_parse_lattice(mecab, lattice)

		lines := strings.Split(C.GoString(C.mecab_lattice_tostr(lattice)), "\n")
		for _, l := range lines {
			if strings.Index(l, "EOS") != 0 {
				if len(l) > 1 {
					res = append(res, split(l))
				}
			}
		}

		req.Result <- res
	}
}