トップ
新規
一覧
単語検索
最終更新
ヘルプ
ログイン
アールメカブ
hidden_Rと和布蕪
をテンプレートにして作成
開始行:
[[Rの備忘録]]
/*
R CMD SHLIB mecab3.c -L/usr/local/lib/ -lmecab -I/usr/lo...
*/
#include <R.h>
#include <Rdefines.h>
#include <Rinternals.h>
#include <mecab.h>
#include <stdio.h>
#define CHECK(eval) if (! eval) { \
fprintf (stderr, "Exception:%s\n", mecab_strerror (me...
mecab_destroy(mecab); \
return -1; }
SEXP mecab3(SEXP aa){
SEXP parsed;
const char* input = CHAR(STRING_ELT(aa,0));
mecab_t *mecab;
mecab_node_t *node;
const char *result;
int i;
mecab = mecab_new2 (input);
CHECK(mecab);
// node ごとに表示などする場合
node = mecab_sparse_tonode(mecab, input);
CHECK(node);
for (; node; node = node->next) {
printf("id = %d \n", node->id);
if (node->stat == MECAB_BOS_NODE)
printf("BOS=\n");
else if (node->stat == MECAB_EOS_NODE)
printf("EOS=\n");
else{// NULL terminateされていないので fwrite を使う
//変数に代入したければ
//strncpy(buf, node->feature, node...
printf("fwrite = ");
fwrite (node->surface, sizeof(char),
node->lengt...
// node->length はノード全体ではなく,形態素の長さ
}
// R コンソールに表示を始める
Rprintf(" feature = %s %d %d rcAttr = %d
lcAttr = %d posid = %d chartype = %d st...
isbest = %d alph = %f beta = %f
prob = %f cost = %d\n",
node->feature,// CSV で表記された素性情報
(int)(node->surface - input),
(int)(node->surface - input + node->length),
node->rcAttr,
node->lcAttr,
node->posid,
(int)node->char_type,
(int)node->stat,
(int)node->isbest,
node->alpha,
node->beta,
node->prob,
node->cost);
}//for文終了
// 返却値用
result = mecab_sparse_tostr(mecab, input);
CHECK(result);
PROTECT(parsed = mkString(result));
UNPROTECT(1);
mecab_destroy(mecab);
return(parsed);
}
出力例(適宜改行をいれた)
kekka <- .Call("mecab3", "太郎は次郎が持っている本を花子...
id = 0
BOS=
feature = BOS/EOS,*,*,*,*,*,*,*,*
0 0
rcAttr = 0
lcAttr = 0
posid = 0
chartype = 0
stat = 2
isbest = 1
alph = 0.000000
beta = 0.000000
prob = 0.000000
cost = 0
id =5
fwrite = 太郎
feature = 名詞,固有名詞,人名,名,*,*,太郎,タロウ,タロー
0 6
rcAttr = 1291
lcAttr = 1291
posid = 44
chartype = 2
stat = 0
isbest = 1
alph = 0.000000
beta = 0.000000
prob = 0.000000
cost = 8614
# RjpWiki の 「Rの文字列処理関数」を参照
#
dyn.load("mecab3.so")
kekka <- .Call("mecab3",
"太郎は次郎が持っている本を花子に渡した。")
kekka2 <- unlist(strsplit(kekka, "\n"))
reg<- NULL
kekka3<- NULL
for(i in 1 :length(kekka2)){
reg<- regexpr("^(\\w+)\t(\\w+)", kekka2[i])
kekka3<- c(kekka3, substring(kekka2[i], reg[1],
attributes(reg)[[1]]))
}
kekka3
さて,上のコードだと和布蕪の解析結果をそのままRに返してい...
というわけで,もう少し手を入れた[[雛形>hidden_RへCから名...
終了行:
[[Rの備忘録]]
/*
R CMD SHLIB mecab3.c -L/usr/local/lib/ -lmecab -I/usr/lo...
*/
#include <R.h>
#include <Rdefines.h>
#include <Rinternals.h>
#include <mecab.h>
#include <stdio.h>
#define CHECK(eval) if (! eval) { \
fprintf (stderr, "Exception:%s\n", mecab_strerror (me...
mecab_destroy(mecab); \
return -1; }
SEXP mecab3(SEXP aa){
SEXP parsed;
const char* input = CHAR(STRING_ELT(aa,0));
mecab_t *mecab;
mecab_node_t *node;
const char *result;
int i;
mecab = mecab_new2 (input);
CHECK(mecab);
// node ごとに表示などする場合
node = mecab_sparse_tonode(mecab, input);
CHECK(node);
for (; node; node = node->next) {
printf("id = %d \n", node->id);
if (node->stat == MECAB_BOS_NODE)
printf("BOS=\n");
else if (node->stat == MECAB_EOS_NODE)
printf("EOS=\n");
else{// NULL terminateされていないので fwrite を使う
//変数に代入したければ
//strncpy(buf, node->feature, node...
printf("fwrite = ");
fwrite (node->surface, sizeof(char),
node->lengt...
// node->length はノード全体ではなく,形態素の長さ
}
// R コンソールに表示を始める
Rprintf(" feature = %s %d %d rcAttr = %d
lcAttr = %d posid = %d chartype = %d st...
isbest = %d alph = %f beta = %f
prob = %f cost = %d\n",
node->feature,// CSV で表記された素性情報
(int)(node->surface - input),
(int)(node->surface - input + node->length),
node->rcAttr,
node->lcAttr,
node->posid,
(int)node->char_type,
(int)node->stat,
(int)node->isbest,
node->alpha,
node->beta,
node->prob,
node->cost);
}//for文終了
// 返却値用
result = mecab_sparse_tostr(mecab, input);
CHECK(result);
PROTECT(parsed = mkString(result));
UNPROTECT(1);
mecab_destroy(mecab);
return(parsed);
}
出力例(適宜改行をいれた)
kekka <- .Call("mecab3", "太郎は次郎が持っている本を花子...
id = 0
BOS=
feature = BOS/EOS,*,*,*,*,*,*,*,*
0 0
rcAttr = 0
lcAttr = 0
posid = 0
chartype = 0
stat = 2
isbest = 1
alph = 0.000000
beta = 0.000000
prob = 0.000000
cost = 0
id =5
fwrite = 太郎
feature = 名詞,固有名詞,人名,名,*,*,太郎,タロウ,タロー
0 6
rcAttr = 1291
lcAttr = 1291
posid = 44
chartype = 2
stat = 0
isbest = 1
alph = 0.000000
beta = 0.000000
prob = 0.000000
cost = 8614
# RjpWiki の 「Rの文字列処理関数」を参照
#
dyn.load("mecab3.so")
kekka <- .Call("mecab3",
"太郎は次郎が持っている本を花子に渡した。")
kekka2 <- unlist(strsplit(kekka, "\n"))
reg<- NULL
kekka3<- NULL
for(i in 1 :length(kekka2)){
reg<- regexpr("^(\\w+)\t(\\w+)", kekka2[i])
kekka3<- c(kekka3, substring(kekka2[i], reg[1],
attributes(reg)[[1]]))
}
kekka3
さて,上のコードだと和布蕪の解析結果をそのままRに返してい...
というわけで,もう少し手を入れた[[雛形>hidden_RへCから名...
ページ名: