LoginSignup
7
7

More than 5 years have passed since last update.

テキストマイニングの正規化

Last updated at Posted at 2015-03-06

MacOSはコンソールコマンドが揃っているので、テキストの前処理も出来るだけコマンドラインで済ませてしまった方が楽です。

全角文字、半角文字、大文字小文字等の吸収

phpコマンドを1Linerで使うのが手っ取り早い。


# 全角英数字を半角英数字に統一
$ cat /path/to/file | php -R 'echo mb_convert_kana($argn, 'a', "UTF-8" ), "\n";'

# 小文字を大文字に統一
$ cat /path/to/file | php -R 'echo mb_strtoupper( $argn, "UTF-8" ) , "\n";'

# 上記を一気に
$ cat /path/to/file | php -R 'echo mb_strtoupper( mb_convert_kana( $argn, "a", "UTF-8"), "UTF-8" ), "\n";'

単語の置換

sed の -f オプションを使うと辞書ファイルのように使い回しができて便利。

置換する単語のリストを適当な名前(sed.command)で作成する。

s/hogehoge/Foo/g
s/moo/woo/g

sedコマンドで上記を実行

$ sed -f sed.command /path/to/original 

空行の削除

grepを使う

$ grep -v '^\s*$' /path/to/file

sedを使う

$  sed -e 's/^$//g' /path/to/file

上記を1linerで実行

sed.command に s/^$//gを追加しておく

$ cat /path/to/file | php -R 'echo mb_strtoupper( mb_convert_kana( $argn, "a", "UTF-8"), "UTF-8" ), "\n";' | sed -f sed.command >/path/to/new_file
7
7
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
7
7