一行づつ、をstringで

自分用メモ。

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

#include <R.h> 
#include <Rdefines.h> 
#include <Rinternals.h> 

extern "C" {
  SEXP my_scan(SEXP fn, SEXP rho) {
	SEXP result;
	std::vector<std::string> v;
	std::ifstream fis("/Users/syou6162/dbcls/umls/umls.txt");

	std::string line;
	while(getline(fis, line)) {
	  SEXP R_fcall, tmp, value;

	  PROTECT(tmp = allocVector(STRSXP, 1));
	  SET_STRING_ELT(tmp, 0, mkChar(line.c_str()));

	  PROTECT(R_fcall = lang2(fn, tmp));
	  PROTECT(value = eval(R_fcall, rho));

	  v.push_back(std::string(CHAR(STRING_ELT(value, 0))));

	  UNPROTECT(3);
	}

	PROTECT(result = allocVector(STRSXP, v.size()));

	for(int i = 0; i < v.size(); i++) {
	  SET_STRING_ELT(result, i, mkChar(v.at(i).c_str()));
	}

 	UNPROTECT(1);
	return(result);
  };
}
setwd("/Users/syou6162/")
dyn.load(paste("my_scan", .Platform$dynlib.ext, sep = "")) 

my_scan <- function(f){
  .Call("my_scan", f, new.env())
}

library(RMeCab)

table(my_scan(function(x){
  names(RMeCabC(x)[[1]])
}))