LoginSignup
2

More than 1 year has passed since last update.

Organization

FileMaker + kuromoji.js で形態素解析の序章

はじめに

前書き

対象読者

  • Claris FileMaker Pro 19 ユーザ
  • FileMaker の WEB ビューアを用いて形態素解析をやってみたいという(奇特な)方

検証環境

  • Windows 10 Pro
  • FileMaker Pro 19.4.1
  • kuromoji.js 0.1.2
  • インターネット接続環境

kuromoji.js とは

形態素解析とは

文字列 読み 原形 品詞の種類 活用の種類 活用形
お待ち オマチ お待ち 名詞-サ変接続
する 動詞-自立 サ変・スル 連用形
助詞-接続助詞
おり オリ おる 動詞-非自立 五段・ラ行 連用形
ます マス ます 助動詞 特殊・マス 基本形
記号-句点

準備

kuromoji.js

JSDelivr

  • 今回、WEB ビューアにおけるローカルリソース問題を避けるために、オンライン環境のリソースを使用します
  • 具体的には CDN として有名な JSDELVIR さんを直接参照させていただきます

FileMaker

fmp12 ファイル作成

  • ファイル名は何でも構いませんが今回は kuromoji.fmp12 として作成

テーブル構成

  • 以下のように、texts を作成

image.png

texts テーブル

  • 以下のように。inputg_for_webview があればよい

image.png

グローバル変数設定

  • $$kuromoji_js_path $$kuromoji_dict_path という二つのグローバル変数を設定する
  • 値はそれぞれ以下の通り
"https://cdn.jsdelivr.net/npm/kuromoji@0.1.2/build/kuromoji.js"
"https://cdn.jsdelivr.net/npm/kuromoji@0.1.2/dict/"

image.png
image.png

  • これらを open スクリプトで実行させる

image.png

レイアウト

  • 適当に配置。WEB ビューアの設定は後でやるので、今は置いておくだけでヨシ

image.png

  • OnLayoutEnter をトリガとして open スクリプトを設定

image.png

  • ここまでで下準備は完了です

作成

g_for_webview フィールドの設定

  • 以下のように記述
  • [[[kuromoji_js_path]]] などは、後で WEB ビューアの定義側で Substitute 関数をかけて置換する。ここでは可読性を上げるため、通常有り得ない文字列として置いておく
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div id="result"></div>

<script src="[[[kuromoji_js_path]]]"></script>
<script>
kuromoji.builder({ dicPath: "[[[kuromoji_dict_path]]]" }).build(function (err, tokenizer) {
    var result = tokenizer.tokenize("[[[input]]]")
    var result = JSON.stringify(result, null, 2)
    document.getElementById('result').innerHTML = result
})
</script>
</body>
</html>

WEB ビューアの設定

  • 以下のように記述
Let (
  [
    kuromoji_js_path = $$kuromoji_js_path;
    kuromoji_dict_path = $$kuromoji_dict_path;
    input = texts::input;

    result = texts::g_for_webview;
    result = Substitute ( result ; "[[[kuromoji_js_path]]]" ; kuromoji_js_path );
    result = Substitute ( result ; "[[[kuromoji_dict_path]]]" ; kuromoji_dict_path );
    result = Substitute ( result ; "[[[input]]]" ; input )
  ];

  result
)

出力結果の確認

こんな感じ

  • すもももももももものうち の場合
[ { "word_id": 415760, "word_type": "KNOWN", "word_position": 1, "surface_form": "すもも", "pos": "名詞", "pos_detail_1": "一般", "pos_detail_2": "*", "pos_detail_3": "*", "conjugated_type": "*", "conjugated_form": "*", "basic_form": "すもも", "reading": "スモモ", "pronunciation": "スモモ" }, { "word_id": 93220, "word_type": "KNOWN", "word_position": 4, "surface_form": "も", "pos": "助詞", "pos_detail_1": "係助詞", "pos_detail_2": "*", "pos_detail_3": "*", "conjugated_type": "*", "conjugated_form": "*", "basic_form": "も", "reading": "モ", "pronunciation": "モ" }, { "word_id": 1614710, "word_type": "KNOWN", "word_position": 5, "surface_form": "もも", "pos": "名詞", "pos_detail_1": "一般", "pos_detail_2": "*", "pos_detail_3": "*", "conjugated_type": "*", "conjugated_form": "*", "basic_form": "もも", "reading": "モモ", "pronunciation": "モモ" }, { "word_id": 93220, "word_type": "KNOWN", "word_position": 7, "surface_form": "も", "pos": "助詞", "pos_detail_1": "係助詞", "pos_detail_2": "*", "pos_detail_3": "*", "conjugated_type": "*", "conjugated_form": "*", "basic_form": "も", "reading": "モ", "pronunciation": "モ" }, { "word_id": 1614710, "word_type": "KNOWN", "word_position": 8, "surface_form": "もも", "pos": "名詞", "pos_detail_1": "一般", "pos_detail_2": "*", "pos_detail_3": "*", "conjugated_type": "*", "conjugated_form": "*", "basic_form": "もも", "reading": "モモ", "pronunciation": "モモ" }, { "word_id": 93100, "word_type": "KNOWN", "word_position": 10, "surface_form": "の", "pos": "助詞", "pos_detail_1": "連体化", "pos_detail_2": "*", "pos_detail_3": "*", "conjugated_type": "*", "conjugated_form": "*", "basic_form": "の", "reading": "ノ", "pronunciation": "ノ" }, { "word_id": 62510, "word_type": "KNOWN", "word_position": 11, "surface_form": "うち", "pos": "名詞", "pos_detail_1": "非自立", "pos_detail_2": "副詞可能", "pos_detail_3": "*", "conjugated_type": "*", "conjugated_form": "*", "basic_form": "うち", "reading": "ウチ", "pronunciation": "ウチ" } ]

image.png

  • 世界をリードするワークプレイス・イノベーション・プラットフォーム。 の場合
[ { "word_id": 2633350, "word_type": "KNOWN", "word_position": 1, "surface_form": "世界", "pos": "名詞", "pos_detail_1": "一般", "pos_detail_2": "*", "pos_detail_3": "*", "conjugated_type": "*", "conjugated_form": "*", "basic_form": "世界", "reading": "セカイ", "pronunciation": "セカイ" }, { "word_id": 92880, "word_type": "KNOWN", "word_position": 3, "surface_form": "を", "pos": "助詞", "pos_detail_1": "格助詞", "pos_detail_2": "一般", "pos_detail_3": "*", "conjugated_type": "*", "conjugated_form": "*", "basic_form": "を", "reading": "ヲ", "pronunciation": "ヲ" }, { "word_id": 224280, "word_type": "KNOWN", "word_position": 4, "surface_form": "リード", "pos": "名詞", "pos_detail_1": "サ変接続", "pos_detail_2": "*", "pos_detail_3": "*", "conjugated_type": "*", "conjugated_form": "*", "basic_form": "リード", "reading": "リード", "pronunciation": "リード" }, { "word_id": 3011740, "word_type": "KNOWN", "word_position": 7, "surface_form": "する", "pos": "動詞", "pos_detail_1": "自立", "pos_detail_2": "*", "pos_detail_3": "*", "conjugated_type": "サ変・スル", "conjugated_form": "基本形", "basic_form": "する", "reading": "スル", "pronunciation": "スル" }, { "word_id": 230, "word_type": "UNKNOWN", "word_position": 9, "surface_form": "ワークプレイス・イノベーション・プラットフォーム", "pos": "名詞", "pos_detail_1": "一般", "pos_detail_2": "*", "pos_detail_3": "*", "conjugated_type": "*", "conjugated_form": "*", "basic_form": "*" }, { "word_id": 90940, "word_type": "KNOWN", "word_position": 33, "surface_form": "。", "pos": "記号", "pos_detail_1": "句点", "pos_detail_2": "*", "pos_detail_3": "*", "conjugated_type": "*", "conjugated_form": "*", "basic_form": "。", "reading": "。", "pronunciation": "。" } ]

image.png

  • ね、簡単でしょう?

おわりに

次回予告

感想

  • 形態素解析のやり方は色々あると思いますが、インターネット環境があるのが概ね前提の世の中なので CDN で使えてしまうのがまあよろしかろうなのではないでしょうか
    • 辞書ファイルをローカルリソースとして使おうとすると色々面倒くなったので避けた
  • JSON.stringify(result, null, 2) ってわざわざ書いて spacer の定義しても見た目が変わらず、「アレーなんだろー」って思ってるのですが、なんでだろ
    • ちなみに "<br>" と書くと改行として表示反映されるのですが……
    • 何か WEB ビューア環境ならではの特殊事情??

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
2