• Stars
    star
    202
  • Rank 189,254 (Top 4 %)
  • Language
    Python
  • License
    MIT License
  • Created over 7 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Tutorial to train fastText with Japanese corpus

fastText Japanese Tutorial

Facebookの発表したfastTextを日本語で学習させるためのチュートリアルです。

Setup

事前に、以下の環境のセットアップを行います。Windowsの場合、MeCabのインストールが鬼門のためWindows10ならbash on Windowsを利用してUbuntu環境で作業することを推奨します。

1. 学習に使用する文書を用意する

日本語Wikipediaのダンプデータをダウンロードし、 source フォルダに格納してください。

2. テキストを抽出する

WikiExtractorを使用し、sourceに格納したWikipediaのデータからテキストを抜き出し、corpusフォルダに格納します。

python wikiextractor/WikiExtractor.py -b 500M -o corpus source/jawiki-xxxxxxxx-pages-articles-multistream.xml.bz2
  • -bは、抽出したデータそれぞれのファイルサイズで、この単位でテキストファイルが作成されていきます。必要に応じ調整をしてください。

Wikipediaのabstractのファイルを使う場合は、parse.pyにabstract用の抽出処理を実装しているので、そちらを利用してください。

python parse.py jawiki-xxxxxxxxx-abstract4.xml  --extract

テキストを抽出したファイルは、最終的に一つにまとめます。これはコマンドで行ってもよいですが、parse.pyに結合用のスクリプトを用意しているのでコマンドがわからない場合は使ってください。

python parse.py (対象フォルダ) --concat (対象ファイルに共通する名称(wiki_など))

これで、学習用のテキストデータの作成が完了しました。

3. テキストを単語に分ける(分かち書きする)

テキストデータ内の単語を英語と同じようにスペースで分ける作業(=分かち書き)を行います。この作業には、MeCabを利用します。

mecab (対象テキストファイル) -O wakati -o (出力先ファイル)

これで、単語ごとに区切られたファイルができました。

4. fastTextで学習する

英語と同じように、単語ごとに区切られたファイルが手に入ったため、あとはfastTextを実行するだけです。fastTextのリポジトリをcloneしてきて、ドキュメントにある通りmakeによりビルドしてください。

設定パラメーターは各種ありますが、論文を参考にすると、扱うデータセットにより単語の数値表現のサイズ(ベクトルの次元)は以下のようになっています(※tokenが何の単位なのかは言及がなかったのですが、おそらく単語カウントと思われます)。

  • small(50M tokens): 100
  • mediam(200M tokens) :200
  • full:300

要は、小さいデータセットなら小さい次元、ということです。Wikipedia全件のような場合はfullの300次元に相当するため、以下のように処理します。

./fasttext skipgram -input (分かち書きしたファイル) -output model -dim 300

Word2Vecの学習と同等のパラメーターで行う場合は、以下のようになります(パラメーター設定などは、こちらに詳しいです)。

./fasttext skipgram -input (分かち書きしたファイル) -output model -dim 200 -neg 25 -ws 8

(Issueにも上がっていますが、パラメーターで結構変わるらしいです。epoch、mincountなど。。。)

学習が完了すると、-outputで指定したファイル名について、.bin.vecの二種類のファイルが作成されます。これらが学習された分散表現を収めたファイルになります。 ただ、Wikipeida全件のような場合はデータサイズが大きすぎてmodelのファイルを読み込もうとするとMemoryErrorで飛ぶこともままあるほか、エンコードの問題が発生するケースがあります(というか発生したのですが)。そのような場合は、一旦単語の辞書を作り(「朝」->11など、単語をIDに変換する辞書を作る)、テキストファイルを単語IDの列に変換するなどして対応します。 この作業のために、parser.pyにtokenizeの機能を実装しているので、必要に応じて活用してください。

python parser.py (対象テキストファイル)  --tokenize

これで、.vocabという辞書ファイルと、_tokenizedという単語ID化されたテキストファイルが手に入ります。

5.fastTextを活用する

eval.pyを利用し、似ている単語を検索することができます。

python eval.py (単語)

こちらは、デフォルトでfastText内のmodel.vecを参照します。別のファイル名、または別の場所に保管している場合は--pathオプションで位置を指定してください。

More Repositories

1

awesome-text-summarization

The guide to tackle with the Text Summarization
1,267
star
2

baby-steps-of-rl-ja

Pythonで学ぶ強化学習 -入門から実践まで- サンプルコード
Jupyter Notebook
420
star
3

awesome-financial-nlp

Researches for Natural Language Processing for Financial Domain
384
star
4

tensorflow_qrnn

QRNN implementation for TensorFlow
Python
238
star
5

mlnote-note

機械学習帳を学ぶノート
Jupyter Notebook
216
star
6

graph-convolution-nlp

Graph Convolution Network for NLP
Jupyter Notebook
214
star
7

kemono_puyo

🐱 Take kemono pictures and lines up 3, then tanoshii
JavaScript
94
star
8

magenta_session

🎹 Music Session with Google Magenta
JavaScript
81
star
9

techcircle_openai_handson

Tech-Circle OpenAI Handson Repository
Python
64
star
10

python_exercises

short exercises for Python
Python
54
star
11

machine_learning_in_application

Practical Machine Learning Implementation In the Application
Python
38
star
12

tying-wv-and-wc

Implementation for "Tying Word Vectors and Word Classifiers: A Loss Framework for Language Modeling"
Python
38
star
13

gensim_notebook

explain how to develop the topic model and how to evaluate it.
Python
38
star
14

pyfbi

pyFBI enables a necessary and sufficient profiling and visualization
JavaScript
31
star
15

baby_steps_of_machine_learning

basic tutorial for machine learning implementation
Jupyter Notebook
29
star
16

pykintone

Python library to access kintone
Python
27
star
17

mlimages

prepare the image data set for machine learning
Python
24
star
18

datascience-template

Data science project template
Jupyter Notebook
19
star
19

acl-anthology

Script to get ACL Anthology
Python
16
star
20

SAPExtractorDotNET

The data extractor for SAP Query , Table
Visual Basic
16
star
21

sms-tools-workspace

sms-tools workspace
Python
14
star
22

kintoneDotNET

サイボウズ社のkintoneを.NET Framework上で扱うためのライブラリです (※非公認)
Visual Basic
14
star
23

cv_tutorial

OpenCV Tutorial
Jupyter Notebook
14
star
24

yans-2019-annotation-hackathon

Yans2019 Annotation hackathon
Python
12
star
25

KanaMaker

make Kana(Katakana,Hiragana,Roman) text
JavaScript
11
star
26

tech_book_2

技術書典2で出店する書籍の紹介ページです
11
star
27

rnn_demo

Recurrent Neural Network Demo by PyBrain
Python
10
star
28

pyoxford

Python Client for Microsoft Project Oxford
Python
10
star
29

chainer_pong

learn pong by chainer
Python
9
star
30

kintoneJavaScripts

kintoneのJavaScriptカスタマイズテンプレート集
JavaScript
9
star
31

karura

Easy machine learning platform on the kintone
Python
9
star
32

scikit-learn-notebook

japanese scikit-learn tutorial
Jupyter Notebook
8
star
33

number_recognizer

handwritten number recognizer by scikit-learn
Python
8
star
34

localstack-lab

Invoke localstack on SageMaker Studio Lab
Jupyter Notebook
7
star
35

airflow-ml-pipeline

The repository to learn Machine Learning with Airflow
Python
6
star
36

git-sparse

git command extention for sparse-checkout
6
star
37

esg-evaluation-by-nlp

Automate ESG evaluation by nlp
Jupyter Notebook
5
star
38

JavaEE7App

simple JavaEE7 web application
Java
5
star
39

DataBaseExecutors

Simple And Functional Database Access Library for C# / VB.NET
Visual Basic
5
star
40

DialogueBreakdownDetection2016

2016年度の対話破綻検出チャレンジの提出モデル
Python
5
star
41

python_doc_sample

sample python project that documented by Sphinx
Python
4
star
42

allennlp-sagemaker-tuning

Hyper Parameter Tuning for AllenNLP Model by Amazon SageMaker
Jupyter Notebook
4
star
43

python_training

python研修用リポジトリ
Python
4
star
44

get_intangible_assets

Get Intangible Assets from XBRL
Jupyter Notebook
4
star
45

robinchorus

Robin Chorus is group chat service
Python
3
star
46

cv_tutorial_feature

Tutorial to detect and describe the feature of image by OpenCV
Jupyter Notebook
3
star
47

tensorflow-windows-build

tensorflow wheel file builded on Windows
3
star
48

cartpole-q-learning

Solve CartPole environment on OpenAI Gym by simple q-learning algorithm
Python
3
star
49

simple_decimal

This is javascript library for decimal type. Released under MIT licence
JavaScript
2
star
50

TravelPenguinBot

Travel Penguin Bot
Python
2
star
51

pollen_prediction_by_azure

predict pollen by Azure Machine Learning
Python
2
star
52

sayuri-server

Conference Consultant Sayuri
Python
2
star
53

YourHybridcast

Hybridcast on Youtube
JavaScript
2
star
54

decolo

Decorate Face and show message for your party guests.
JavaScript
2
star
55

tensorflow-arithmetic

tensorflow example by using arithmetic calculation
2
star
56

xbrl_read_tutorial

How to read XBRL file
Jupyter Notebook
2
star
57

jira-wiki-gadget

Atlassian JIRA's gadget for Wiki
CSS
2
star
58

enigma_abroad

let's travel by choosing the photo
Python
2
star
59

Gears

ASP.NET WebForm用 開発フレームワーク
Visual Basic
2
star
60

introduction_to_music_production

Introduction to Music Production on Coursera
1
star
61

rockin_on_data_science

Datascience for Rockin'on!
Jupyter Notebook
1
star
62

developers-stop-watch

stop watch for developer
CSS
1
star
63

enigma_travel

let's travel by one click
Python
1
star
64

NodeJournal

reads the site and summarizes it like newspaper
JavaScript
1
star
65

yans-2018-ttk

YANS 2018 TTKチームリポジトリ
Python
1
star
66

SignalRonMVC4

SignalR sample on ASP.NET MVC4
JavaScript
1
star
67

jina-demo

Demonstration of JINA, cross-modal and multi-modal data store
Jupyter Notebook
1
star
68

elastic_search_study

Elastic Search勉強用リポジトリ
1
star
69

go_exercises

short exercises for Go
Go
1
star
70

chika

Japanese Shiritori Master Chika
JavaScript
1
star
71

SlickGridNumericEditor

the numeric (decimal) editor for SlickGrid
JavaScript
1
star
72

heroku-buildpack-jubatus

heroku buildpack to run the jubatus
Shell
1
star
73

hacker-touches-the-walls

Tell your fortune by your key touches!
JavaScript
1
star
74

pyzenin

Template project to follow Zen of Python
Python
1
star
75

studiolab-cdk

Learn the cdk by SageMaker Studio Lab
Jupyter Notebook
1
star
76

music_hack_day_onpasha

music_hack_day_onpasha
Python
1
star