LoginSignup
18
18

More than 5 years have passed since last update.

MeCabで文章を単語ごとに分けて単語listを作るメモ

Posted at

Mecabで単語のlistを作成したい

単語を要素として、listを作りたいと思ったので
そのやり方をメモ。

以下のブログを参考にさせていただきました。より詳しく書いていらっしゃいます。
MeCab-Pythonで分かち書きと形態素解析

環境

Python : 3.6.2(Anaconda)
mecab-python3 : 0.7

Code

Pythonのコード

import MeCab

text = 'Pythonを使って、機械学習を勉強する。'

tagger = MeCab.Tagger("-Owakati")

#str型で、単語が空白で別れる
str_output = tagger.parse(text)
print(str_output)
print(type(str_output))

#listに変換する
list_output = str_output.split(' ')
print(list_output)
print(type(list_output))

出力

Python を 使っ て 、 機械 学習 を 勉強 する 。 
<class 'str'>

['Python', 'を', '使っ', 'て', '、', '機械', '学習', 'を', '勉強', 'する', '。', '\n']
<class 'list'>
18
18
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
18
18