1. TOP
  2. プログラミング
  3. 教師ありの機械学習(AI)プログラムを使用して、与えられた文章が何の言語なのか判定する

教師ありの機械学習(AI)プログラムを使用して、与えられた文章が何の言語なのか判定する

 2019/02/26 プログラミング
この記事は約 2 分で読めます。 4,731 Views

Warning: Undefined array key 3 in /home/octo8/dainamo.jp/public_html/wp/wp-content/themes/surfing/lib/table-of-contents-plus/surfing_toc.php on line 1434

Warning: Undefined array key 3 in /home/octo8/dainamo.jp/public_html/wp/wp-content/themes/surfing/lib/table-of-contents-plus/surfing_toc.php on line 1434

Warning: Undefined array key 3 in /home/octo8/dainamo.jp/public_html/wp/wp-content/themes/surfing/lib/table-of-contents-plus/surfing_toc.php on line 1434

Warning: Undefined array key 3 in /home/octo8/dainamo.jp/public_html/wp/wp-content/themes/surfing/lib/table-of-contents-plus/surfing_toc.php on line 1434

Warning: Undefined array key 3 in /home/octo8/dainamo.jp/public_html/wp/wp-content/themes/surfing/lib/table-of-contents-plus/surfing_toc.php on line 1434

Warning: Undefined array key 3 in /home/octo8/dainamo.jp/public_html/wp/wp-content/themes/surfing/lib/table-of-contents-plus/surfing_toc.php on line 1434

Warning: Undefined array key 3 in /home/octo8/dainamo.jp/public_html/wp/wp-content/themes/surfing/lib/table-of-contents-plus/surfing_toc.php on line 1451

Warning: Undefined array key 3 in /home/octo8/dainamo.jp/public_html/wp/wp-content/themes/surfing/lib/table-of-contents-plus/surfing_toc.php on line 1451

Warning: Undefined array key 3 in /home/octo8/dainamo.jp/public_html/wp/wp-content/themes/surfing/lib/table-of-contents-plus/surfing_toc.php on line 1451

Warning: Undefined array key 3 in /home/octo8/dainamo.jp/public_html/wp/wp-content/themes/surfing/lib/table-of-contents-plus/surfing_toc.php on line 1451

Warning: Undefined array key 3 in /home/octo8/dainamo.jp/public_html/wp/wp-content/themes/surfing/lib/table-of-contents-plus/surfing_toc.php on line 1451

Warning: Undefined array key 3 in /home/octo8/dainamo.jp/public_html/wp/wp-content/themes/surfing/lib/table-of-contents-plus/surfing_toc.php on line 1451

Warning: Undefined array key 3 in /home/octo8/dainamo.jp/public_html/wp/wp-content/themes/surfing/lib/table-of-contents-plus/surfing_toc.php on line 1216

Warning: Undefined array key 3 in /home/octo8/dainamo.jp/public_html/wp/wp-content/themes/surfing/lib/table-of-contents-plus/surfing_toc.php on line 1216

Warning: Undefined array key 3 in /home/octo8/dainamo.jp/public_html/wp/wp-content/themes/surfing/lib/table-of-contents-plus/surfing_toc.php on line 1216

Warning: Undefined array key 3 in /home/octo8/dainamo.jp/public_html/wp/wp-content/themes/surfing/lib/table-of-contents-plus/surfing_toc.php on line 1216

Warning: Undefined array key 3 in /home/octo8/dainamo.jp/public_html/wp/wp-content/themes/surfing/lib/table-of-contents-plus/surfing_toc.php on line 1216

Warning: Undefined array key 3 in /home/octo8/dainamo.jp/public_html/wp/wp-content/themes/surfing/lib/table-of-contents-plus/surfing_toc.php on line 1216

Warning: Undefined array key 3 in /home/octo8/dainamo.jp/public_html/wp/wp-content/themes/surfing/lib/table-of-contents-plus/surfing_toc.php on line 1226

Warning: Undefined array key 3 in /home/octo8/dainamo.jp/public_html/wp/wp-content/themes/surfing/lib/table-of-contents-plus/surfing_toc.php on line 1226

Warning: Undefined array key 3 in /home/octo8/dainamo.jp/public_html/wp/wp-content/themes/surfing/lib/table-of-contents-plus/surfing_toc.php on line 1226

Warning: Undefined array key 3 in /home/octo8/dainamo.jp/public_html/wp/wp-content/themes/surfing/lib/table-of-contents-plus/surfing_toc.php on line 1226

Warning: Undefined array key 3 in /home/octo8/dainamo.jp/public_html/wp/wp-content/themes/surfing/lib/table-of-contents-plus/surfing_toc.php on line 1226

Warning: Undefined array key 3 in /home/octo8/dainamo.jp/public_html/wp/wp-content/themes/surfing/lib/table-of-contents-plus/surfing_toc.php on line 1226

Warning: Undefined array key 3 in /home/octo8/dainamo.jp/public_html/wp/wp-content/themes/surfing/lib/table-of-contents-plus/surfing_toc.php on line 1239

Warning: Undefined array key 3 in /home/octo8/dainamo.jp/public_html/wp/wp-content/themes/surfing/lib/table-of-contents-plus/surfing_toc.php on line 1239

Warning: Undefined array key 3 in /home/octo8/dainamo.jp/public_html/wp/wp-content/themes/surfing/lib/table-of-contents-plus/surfing_toc.php on line 1239

Warning: Undefined array key 3 in /home/octo8/dainamo.jp/public_html/wp/wp-content/themes/surfing/lib/table-of-contents-plus/surfing_toc.php on line 1239

Warning: Undefined array key 3 in /home/octo8/dainamo.jp/public_html/wp/wp-content/themes/surfing/lib/table-of-contents-plus/surfing_toc.php on line 1239

Warning: Undefined array key 3 in /home/octo8/dainamo.jp/public_html/wp/wp-content/themes/surfing/lib/table-of-contents-plus/surfing_toc.php on line 1239

今回は最近話題のAI、機械学習について書いていきます。

今回のテーマ

今回のテーマは、与えられた文章が何語か判定する、教師ありの機械学習プログラムです
英語でもスペイン語でも同じアルファベットを使用するが、言語によってよく使用される文字が決まっています。

今回はその傾向に注目して、頻出頻度をもとに言語判定を行います。

今回は少ないデータでもわかりやすい、日本語、英語、アラビア語、中国語でテストを行います。

教師ありとは

問題と解答が揃っているデータを使って学習を行うものをいいます。
株価とか天気予報にも使用されたりします。

補足として、
教師なしとは、データだけあって解答がないものをいいます。
データから特徴を抽出してグループ分けし、特徴、特異性、傾向等を解析します。

その他に強化学習というものもあります。
動物に餌を与えて芸を仕込むような、機械に報酬を与えて、試行錯誤させて失敗と成功から学習していく方法です。

Unicodeのコードポイントを使用する

コードポイントとは、個々の文字に対して、文字集合内での符号位置が決められていますが、これをコードポイントといいます。
(例)

U+307B U+3052
開発環境
OS : mac
言語 : Python3.5
IDE : anaconda spider
ライブラリ :サイキット・ラーン(scikit-learn)

こちらの環境で開発を行っていきます。
テキストデータの場合ナイーブベイズを使用することが推奨されているため、
今回はナイーブベイズという分類器を使用します。

実際にコードを書いていく

#分類器とライブラリのインポート
import numpy as np
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score

#Unicodeのコードポイント頻度を測定するメソッド
def countUnicodeCodePoint(str):
    cnt = np.zeros(65535)
    
    for i in range(len(str)):
	//各文字をUicodeのコードポイントに変換
        codePoint = ord(str[i])
        if codePoint > 65535:
            continue
	// 対応するアドレスの出現頻度回数をインクリメント
        cnt[codePoint] += 1
    // 要素を文字数で割って正規化
    cnt = cnt/len(str)
    return cnt


// 学習用データ
ja_str = 'これは日本語です'
en_str = 'This is Enlish'
th_str = 'هذه جملة عربية'
ch_str = '这是一个中文句子'

x_train = [countUnicodeCodePoint(ja_str), countUnicodeCodePoint(en_str),countUnicodeCodePoint(th_str),countUnicodeCodePoint(ch_str)]
y_train = ['日本語','英語','アラビア語','中国語']

//学習させる
clf = GaussianNB()
clf.fit(x_train,y_train)


// テスト用データ
test_str = '一直走'

// 実際に分類できているか確認
pred = [countUnicodeCodePoint(test_str)]
result = clf.predict(pred)
print(result)

結果

私の環境では、コンソールには【中国語】と表示されました。
判定する文章を変えると日本語と判定されたり、アラビア語と判定されたりするかもしれません。

データをもっと用意すれば、精度が高くなり、同じアルファベットを使用する言語でも判別が可能になります。

メリットを挙げるとすれば、

  • 言語判定を行うことでサイトで自動言語切り替えなどが実装できる
  • 言語判定で自動翻訳すれば掲示板など世界各国で利用しやすくなる

機械学習は使えるようになると仕事の幅が広がります
既存のサービスに機械学習をプラスすれば、便利なサービスになることは確実です。

制作のご依頼・ご相談はこちら

dainamoでは魅力的なWEBサイトや、アプリ、ツール、ポスター、パンフレット、チラシなど幅広く承っております。ご相談、見積もり、お問い合わせなど、お気軽にご連絡ください。

今すぐご相談・問い合わせをする

\ SNSでシェアしよう! /

web制作・デザイン・システム開発・管理なら dainamo(ダイナモ)の注目記事を受け取ろう

この記事が気に入ったら
いいね!しよう

web制作・デザイン・システム開発・管理なら dainamo(ダイナモ)の人気記事をお届けします。

  • 気に入ったらブックマーク! このエントリーをはてなブックマークに追加
  • フォローしよう!

ライター紹介 ライター一覧

kz

kz

税金にも詳しいエンジニア

サーバーの構築からフロントエンド、iOSアプリの作成も行います。
WEBアプリや会員サービスの構築を得意としています。

制作物の一部を紹介
・海外旅行者向け事業者サービス
https://vacation-japan.com

・記憶術を使ったWEBアプリ会員サイト

その他、教育アプリetc..

この人が書いた記事  記事一覧

  • 【VPN】自宅のMacから会社のMacにリモートアクセスする場合のVPNの設定と画面共有方法【リモートデスクトップ】

  • Apacheのバージョン情報を隠してセキュリティを向上させる

  • 文字コードとは一体何なのか?MacでCSVの文字コードをUTF-8に変換する

  • 教師ありの機械学習(AI)プログラムを使用して、与えられた文章が何の言語なのか判定する

関連記事

  • phpMyAdminの簡単にできるセキュリティ対策【Apache】

  • VPSサーバーの構築 第3回 CentOS7 apache MariaDB PHP7 2018

  • 会員サイトで、特定のディレクトリ内にアクセス制限をかける

  • VPSサーバーの構築 第4回 CentOS7 apache MariaDB PHP7 2018

  • 【簡単入門】初心者が一から始める機械学習入門

  • フリーランスエンジニアが実際に使っている便利なフリーソフトツール2020