Example #1
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
	}
}
Example #2
0
// Parse a lattice with this tagger
func (t *Tagger) ParseLattice(l *Lattice) error {
	C.mecab_parse_lattice(t.tagger, l.lattice)
	return t.getLastError()
}
Example #3
0
func (t *Tagger) parse(l *Lattice) int {
	return int(C.mecab_parse_lattice(t.toMecabT(), l.toMecabLatticeT()))
}
Example #4
0
// ParseLattice is a method to parse lattice object.
// Return true if lattice is parsed successfully. A sentence must be set to the lattice with Lattice:set_sentence object before calling this method. Parsed node object can be obtained with Lattice:bos_node. This method is thread safe.
func (t *Tagger) ParseLattice(lattice *Lattice) int {
	l := lattice.toMecabLatticeT()
	return (int)(C.mecab_parse_lattice(t.toMecabT(), l))
}