TreeTagger を Python で動かして、原形を得ます。
下のコードでは、例として "He has lots of books, and I have as many books." という文を処理しています。アウトプットとして、['he', 'have', 'lot', 'of', 'book', ',', 'and', 'I', 'have', 'as', 'many', 'book', '.'] と返ってきます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ttw として treetaggerwrapper をインポート | |
import treetaggerwrapper as ttw | |
# 初期設定 | |
taglang = "en" #英語 | |
tagdir = "/Users/************/TreeTagger" #TreeTagger関連ファイルのあるディレクトリ | |
tagger = ttw.TreeTagger(TAGLANG=taglang, TAGDIR=tagdir) | |
# 処理をする文を指定 | |
line = "He has lots of books, and I have as many books." | |
# treetaggerによる処理 | |
tags = tagger.TagText(line) | |
# 原形のみをリスト("ori_words")に格納 | |
ori_words = [] | |
for tag in tags: | |
ori_words.append(tag.split("\t")[2]) | |
# 必要に応じて、abc順に並べ替える | |
# ("key=str.lower" をつけると、小文字での並べ替え) | |
# ori_words.sort(key=str.lower) | |
# 表示してチェックしてみる | |
print(ori_words) |
なお、10行目の "line" に処理をする文を格納していますが、input()を使ったり、コマンドライン引数を使ったりすると、使い勝手も向上しそうです。
関連
0 件のコメント:
コメントを投稿