LoginSignup
4
4

More than 5 years have passed since last update.

文字ベクトルをテキストファイルに偽装

Posted at

RMeCabの文章行列作成関数は,インプットとして
フォルダないしファイルを想定している.

ただ文字ベクトルを指定して,文書行列を作成
したい場合があったりする.Rには
textConnection()
という関数があって,文字ベクトルをファイルに見せかける
方法があるのだけど,RMeCabの関数内部ではfile.exists()を
使ってチェックを行なっているので,偽装が効かない.

それで文字列ベクトルの要素数が少なければ,以下のように
一時フォルダに一時ファイルを作成して,処理できるかな.

dummy <- c ("私は真面目な学生です。", "彼女は数学専攻の学生です。", "彼らは物理学を専攻している。")
tmpdir <- tempdir ()

for (i in seq(dummy) ){
  td <- tempfile("tmp", tmpdir = tmpdir)
  write( dummy [i] ,  file = td)
}

library (RMeCab)

x <- docMatrix (tmpdir)
unlink (tmpdir, recursive = TRUE)  # tmpファイルを削除
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4