NLTK

ピンインの変換 その2

前回書いたのは、内部的に保存されている(と仮定した)声調番号を後ろに付けたフォーマット(以下、保存用フォーマット)のピンインから、いわゆる表示用フォーマットに変換するロジックの話でした。一応、それとは逆のパターンも考えてみたいと思います。 どん…

ピンインの変換

日本人(だけじゃないかな?)が中国語学習をする上で一番大変なのは声調を覚えることというのは前にも書いたとおりです。そこで一生懸命覚えることになるのですが、例えば自分で電子的に単語帳を作る、それが本格的なDBだろうがExcelだろうが、ピンインの処理…

Filemaker to Python (5) - Updateを試す

Select, InsertときたらUpdateも試しておこうということで、コードを書いてみましたというお話です。 上書きのルール 既存のレコードをUpdateするにあたって、何かしらのルールを決めておく事は重要だと思いました。例えば単語のDBを例にすると、単語そのも…

Filemaker to Python (4) - Insertを試す

中国語学習のDBのプラットフォームとして、Filemakerを使ったのはいくつか理由があるのですが、大きかったのは当時WinとMac両方の環境を使っていた事と、途中からFilemaker Goが出てきたので、そのままiPadとかでも勉強できるかも(実際にはやらなかったので…

Filemaker to Python (4) - パターン検索

とりあえず文字化けというか文字が途切れる問題は回避でき、Filemakerのデータベースにアクセスする手順は整ったと言えそうです。Filemaker ODBCドライバ経由で使用できるSQLについては、この資料の中に書いてあります。ただ、今回は中国語の単語のデータベ…

Filemaker to Python (3) - 途切れた文字列

前回、PythonからFilemakerのテーブルにアクセスする事に成功はしたのですが、検索した文字列の2文字目以降が途切れてしまうという問題が発生しています。ちなみにODBC側でログを取るオプションを使っても何も吐き出されていませんでした。 ちょっと実験 Sel…

Filemaker to Python (2) - Python側の準備

前の記事の続きです。 pyODBCのインストール これは、ホントにそのままesay_installであっさり行きました。 $ easy_install pyodbc Searching for pyodbc Reading http://pypi.python.org/simple/pyodbc/ Reading http://code.google.com/p/pyodbc Reading h…

Filemaker to Python (1) - ODBCドライバを入れてみる

これまでとちょっと毛色の違う話と感じるかも知れませんが、これも技術ネタなのでここに書いておきたいと思います。 なぜいきなりこの話? 先日、中国語の単語のデータベースを持っているという話がありましたが、Filemaker Pro 12のデータベースに入ってい…

Chunking (7.2)

Now I jump to Chapter 7.2 Noun Phrase Chunking (7.2.1) >>> sentence = [("the", "DT"), ("little", "JJ"), ("yellow", "JJ"), ... ("dog", "NN"), ("barked", "VBD"), ("at", "IN"), ("the", "DT"), ("cat", "NN")] >>> grammar = "NP: {<DT>?<JJ>*<NN>}" >>> cp = n</nn></jj></dt>…

あ、確率の話か (中国語の声調を推測してみる) その3

懲りもせず第3弾の記事です。前回、xin, cheng, gongという音の声調には偏りがあるというデータを出したのですが、もっと顕著な例もあるよというお話しです。 もう一回前提の話 誤解を招きやすい表現なので、きちんと補足しておいた方が良いと思います。今回…

Decision Trees (6.4)

Entropy and Information Gain (6.4.1) Try to execute entropy calculation sample. >>> import nltk >>> from nltk_init import * >>> import math >>> def entoropy(labels): ... freqdist = nltk.FreqDist(labels) ... probs = [freqdist.freq(l) for l …

あ、確率の話か (中国語の声調を推測してみる) その2

引っ張った昨日の話の続きです。 どんなデータを作ったのか。 チラ見せするとこんな感じ。元となったデータは、ピンインを純粋な発音の部分(子音+母音)と4つある声調(あと軽声も)に分けてマトリックス状にしました。発音の種類は395種類あり、それぞれについ…

あ、確率の話か (中国語の声調を推測してみる) その1

今から一ヶ月ぐらい前に、自分の持っている中国語単語DB(5800語ぐらい入っているはず)のデータをいじった話を記事にしました。Accessing Chinese word database Analysing Chinese words 2 Pick up combination of Pingyin and Chinese charactersこの単語DB…

Evaluation (6.3)

The Test Set (6.3.1) >>> import random >>> from nltk.corpus import brown >>> tagged_sents = list(brown.tagged_sents(categories='news')) >>> random.shuffle(tagged_sents) >>> size = int(len(tagged_sents) * 0.1) >>> train_set, test_set = tagg…

Recoginzing Textual Entailment (6.2.3)

Recognizing Textual Entailment (6.2.3) Save as rte_features.py with following source code. import nltk def rte_features(rtepair): extractor = nltk.RTEFeatureExtactor(rtpair) features = {} features['word_overlap'] = len(extractor.overlap('w…

Identifying Dialogue Act Types (6.2.2)

Identifying Dialogue Act Types (6.2.2) >>> from nltk_init import * >>> posts = nltk.corpus.nps_chat.xml_posts()[:10000] >>> def dialogue_act_features(post): ... features = {} ... for word in nltk.word_tokenize(post): ... features['contains…

結局NLTKで何をやりたいのか

この本の写経も気がつけば第6章に突入して、この本のボリュームからすると約半分というところまで消化してきています。入門 自然言語処理作者: Steven Bird,Ewan Klein,Edward Loper,萩原正人,中山敬広,水野貴明出版社/メーカー: オライリージャパン発売日: …

Further Examples of Supervised Classification (6.2)

Sentence Segmentation (6.2.1) >>> sents = nltk.corpus.treebank_raw.sents() >>> tokens = [] >>> boundaries = set() >>> offset = 0 >>> for sent in nltk.corpus.treebank_raw.sents(): ... tokens.extend(sent) ... offset += len(sent) ... boundari…

Sequence Classification (6.1.6)

この本の写経シリーズは英語で始めてしまったので、とりあえず英語のまま行きます。特に深い意味はありません。As I have already started this series (learning NLTK) in English, continue to write in English. Sequence Classification (6.1.6) This sa…

Using context (6.1.5)

This example to get previous word as well as suffix. >>> train_set, test_set = featuresets[size:], featuresets[:size] >>> classifier = nltk.DecisionTreeClassifier.train(train_set) >>> >>> def pos_features(sentence, i): ... features = {"suf…

Tagging POS (6.1.4)

FreqDist.inc() is new for me. According to help, "Increment this FreqDist's count for the given sample." Okay, then start from empty and extract last 1, 2, 3 chars from the words. >>> from nltk.corpus import brown >>> suffix_fdist = nltk.F…

Document classification (6.1.3)

Construct correctly labeled document. >>> from nltk.corpus import movie_reviews >>> decoments = [(list(movie_reviews.words(fileid)), category) ... for category in movie_reviews.categories() ... for fileid in movie_reviews.fileids(category)…

Choosing the Right Features (6.1.2)

In my understanding, the example is try to explain "overfit" situation. >>> def gender_features2(name): ... features = {} ... features["firstletter"] = name[0].lower() ... features["lastletter"] = name[-1].lower() ... for letter in 'abcdef…

Supervised classifying (6.1-6.1.1)

Go into the new chapter, Chapter 6 of the whale book.We learned there are some relationship between the last character of first name and gender at Chapter 2.4. Going to use same sample here. This function is to get the last character of th…

Brill tagging (5.6)

Brill tagging concept is shown in the textbook. This is an example in NLTK. >>> nltk.tag.brill.demo() Loading tagged data... Done loading. Training unigram tagger: [accuracy: 0.832151] Training bigram tagger: [accuracy: 0.837930] Training …

Performance limitation (5.5.7-5.5.8)

>>> cfd = nltk.ConditionalFreqDist( ... ((x[1], y[1], z[0]), z[1]) ... for sent in brown_tagged_sents ... for x, y, z in nltk.trigrams(sent)) >>> ambiguous_contexts = [c for c in cfd.conditions() if len(cfd[c]) > 1] >>> sum(cfd[c].N() for …

Combine Taggers (5.5.4-5.5.5)

In case tags cannot be assigned, it is possible to switch to more general tagger by using backoff option. >>> t0 = nltk.DefaultTagger('NN') >>> t1 = nltk.UnigramTagger(train_sents, backoff=t0) >>> t2 = nltk.BigramTagger(train_sents, backof…

Generic N-gram tagger (5.3.3)

Unigram tagger is to assign tags which are "probably" used. This is the restriction as each single word is focused in Unigram tagger. N-gramTagger is to check tags of neighbor words. >>> size = int(len(brown_tagged_sents) * 0.9) >>> train_…

Separating Training and Test data (5.5.2)

What a busy week! Today's topic is also short version. >>> size = int(len(brown_tagged_sents) * 0.9) >>> size 4160 >>> train_sents = brown_tagged_sents[:size] >>> test_sents = brown_tagged_sents[size:] >>> unigram_tagger = nltk.UnigramTagg…

Unigram tagger (5.5.1)

Today's article is short as too busy today!Unigram tagger: >>> from nltk.corpus import brown >>> brown_tagged_sents = brown.tagged_sents(categories='news') >>> brown_sents = brown.sents(categories='news') >>> unigram_tagger = nltk.UnigramT…