かつてはアクセスする相手の使用言語別に、
そのページを作成しなければなりませんでしたが
そのページを作成しなければなりませんでしたが
作る側ではなく、サイトを訪れる側で自動的に言語を変換してもらえればその必要はなくなります
その言語自動変換に利用可能と思われる『言語判定』をテーマ扱います
深層学習などにより、自動翻訳が実際に利用可能になりつつあります
今回は、これを利用して言語を
言語判定における機械学習
・言語判定にUnicode のコードポイントを用いる
・アルゴリズムはNaiveBayesを利用する
コード
import numpy as np
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score
import glob
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score
import glob
# Unicodeのコードポイント頻度測定
def count_codePoint(str):
# Unicodeのコードポイントをアドレスとする配列を用意
# Unicodeは21ビット(2^16 x 2^5: Uを含む5文字に変換、2^16=65536)
counter = np.zeros(65535)
def count_codePoint(str):
# Unicodeのコードポイントをアドレスとする配列を用意
# Unicodeは21ビット(2^16 x 2^5: Uを含む5文字に変換、2^16=65536)
counter = np.zeros(65535)
for i in range(len(str)):
# 各文字をUnicodeのコードポイントに変換
code_point = ord(str[i])
if code_point > 65535 :
continue
# 対応するアドレスの出現回数をインクリメント
counter[code_point] += 1
# 各文字をUnicodeのコードポイントに変換
code_point = ord(str[i])
if code_point > 65535 :
continue
# 対応するアドレスの出現回数をインクリメント
counter[code_point] += 1
# 各要素を文字数で割って正規化
counter = counter/len(str)
return counter
counter = counter/len(str)
return counter
# 学習データの準備 --- (*1)
index = 0
x_train = []
y_train = []
for file in glob.glob('./train/*.txt'): # ファイル一覧の取得
# ファイル名(de_...txt、en_...text、es_...txt)からラベルを作成する
# 言語情報の取得し、ラベルに設定(9文字目から10文字目までを切り取る)
y_train.append(file[8:10])
# ファイルの中の文字列を読み込む ⇒ count_codePoint()メソッドを実行 ⇒ Unicodeのコードポイントの出現頻度によりベクトル化する
# (ファイル内の文字列を連結後、Unicodeのコードポイント頻度測定し、入力データに設定)
file_str = ''
for line in open(file, 'r', encoding='utf8'):
file_str = file_str + line
x_train.append(count_codePoint(file_str))
index = 0
x_train = []
y_train = []
for file in glob.glob('./train/*.txt'): # ファイル一覧の取得
# ファイル名(de_...txt、en_...text、es_...txt)からラベルを作成する
# 言語情報の取得し、ラベルに設定(9文字目から10文字目までを切り取る)
y_train.append(file[8:10])
# ファイルの中の文字列を読み込む ⇒ count_codePoint()メソッドを実行 ⇒ Unicodeのコードポイントの出現頻度によりベクトル化する
# (ファイル内の文字列を連結後、Unicodeのコードポイント頻度測定し、入力データに設定)
file_str = ''
for line in open(file, 'r', encoding='utf8'):
file_str = file_str + line
x_train.append(count_codePoint(file_str))
# 学習する
clf = GaussianNB()
clf.fit(x_train, y_train)
clf = GaussianNB()
clf.fit(x_train, y_train)
# 評価データの準備
index = 0
x_test = []
y_test = []
for file in glob.glob('./test/*.txt'): # ファイル一覧の取得
# 言語情報の取得し、ラベルに設定(8文字目から9文字目までを切り取る)
y_test.append(file[7:9])
# ファイルの中の文字列を読み込む ⇒ count_codePoint()メソッドを実行 ⇒ Unicodeのコードポイントの出現頻度によりベクトル化する
# (ファイル内の文字列を連結後、Unicodeのコードポイント頻度測定し、入力データに設定
file_str = ''
for line in open(file, 'r', encoding='utf8'):
file_str = file_str + line
x_test.append(count_codePoint(file_str))
index = 0
x_test = []
y_test = []
for file in glob.glob('./test/*.txt'): # ファイル一覧の取得
# 言語情報の取得し、ラベルに設定(8文字目から9文字目までを切り取る)
y_test.append(file[7:9])
# ファイルの中の文字列を読み込む ⇒ count_codePoint()メソッドを実行 ⇒ Unicodeのコードポイントの出現頻度によりベクトル化する
# (ファイル内の文字列を連結後、Unicodeのコードポイント頻度測定し、入力データに設定
file_str = ''
for line in open(file, 'r', encoding='utf8'):
file_str = file_str + line
x_test.append(count_codePoint(file_str))
# 評価する
y_pred = clf.predict(x_test)
y_pred = clf.predict(x_test)
print(y_pred)
print("正解率 = " , accuracy_score(y_test, y_pred))
結果
['de' 'en' 'es']
正解率 = 1.0
正解率 = 1.0