HSPでMecab(形態素解析エンジン)を使ってみるサンプル

libmecab.dllが必要です。 https://code.google.com/p/mecab/

libmecab.dllが必要です。 https://code.google.com/p/mecab/

  • hk1v
  • 2013/5/28 0:51
#uselib "libmecab.dll"
#cfunc global mecab_new "mecab_new" int, str
#cfunc global mecab_new2 "mecab_new2" str
#cfunc global mecab_sparse_tostr "mecab_sparse_tostr" int, str
#func global mecab_destroy "mecab_destroy" int
#cfunc global mecab_strerror "mecab_strerror" int
#module mecab
#defcfunc MecabParse str _in
	if (p_mecab_t != 0){
		mecab_destroy p_mecab_t
		p_mecab_t = 0
	}
	p_mecab_t = mecab_new2("");"--userdic=wikipedia.dic"
	if p_mecab_t == 0{
		dupptr e, mecab_strerror(0), 4096, 2
		dialog e : end
	}
	p_result = mecab_sparse_tostr(p_mecab_t, _in)
return p_result
#deffunc GetMecabResult int _ptr, array _res, array _pos, array _read
	if _ptr == 0 : return 0
	dupptr st_result, _ptr, 4096, 2
	LF = "" : poke LF, 0, 0x0A	// LF(Unix)
	sdim res : sdim _res, 256: sdim _pos, 64 : sdim _read, 256 : i = 0
	split st_result, LF, res
	repeat length(res)
		split res(cnt), "\t", res2
		if res2(0) == "EOS" : i = cnt : break
		_res.cnt = "" + res2(0)
		sdim res3, 256, 9
		split res2(1), ",", res3 
		_pos.cnt = "" + res3.0
		_read.cnt = "" + res3.7
	loop
return i
#deffunc MecabClose int _ptr
	mecab_destroy _ptr
return
#global

GetMecabResult MecabParse("花子さんがトイレで雑談してたよ!"), w, p, r

repeat stat
	mes w(cnt) + "\t" + p(cnt) + "\t" + r(cnt)
loop