2020年9月3日木曜日

Supporting social dilemma experiments with App Inventor

Abstract
    In the study of the problem called "social dilemma", various experiments targeting human behavior have been conducted. I created the following app to efficiently implement one of those kinds of experiments. The feature of this app is that it can automatically reflect in real-time the effects of individual actions on others.

Simple examples of social dilemmas and experiments
    According to Toshio Yamagishi (Professor Emeritus, Hokkaido University, Japan) [1], the social dilemma means that everyone can benefit if they cooperate with each other, but everyone suffers if they act only in their own interests. Research (consideration and experiment) about this has been done for a long time. Here we try to reproduce the famous experiment in this book. However, the following is an explanation based on my own understanding.

A) A group consists of four students who are strangers to each other.

B) Do not touch each other during the experiment.

C) Participants fully understand the mechanics of this experiment.

D) 100 yen will be given to each student in each round.

E) Participants can donate d yen arbitrarily.

F) In that case, the donor's money will be (100 - d) yen.

G) Based on the donated d yen, (2d / 3) yen will be given to each of the 3 people other than the donor.

    The experiment was repeated 12 times with the above items (D) to (G) as one set. The part-time job for this experiment was about 1 hour. In fact, the participants took it seriously because they could get that amount of money. In a round, if only one student donates 100 yen and no other students donate, the donating student gets 0 yen and the other student gets 167 yen. That is, only the donated student will lose. If no one donates at all 12 times, each person will get 1,200 yen. On the other hand, if everyone cooperates and donates 100 yen in all rounds, each person can finally get a total of 2,400 yen (this is the maximum amount). This is a very expensive hourly wage!

    It turned out that each person's donation behavior depends on the "interpersonal trustworthiness" that a person has. That is, it was confirmed that one's donation behavior is determined by "whether or not other people can be expected to contribute." Participants in this experiment include people with high confidence and those with low confidence. (This reliability depends on the response scores of the five questionnaires conducted separately.)

    According to the experimental results by Professor Yamagishi, for the 100 yen given in each round, those with high interpersonal reliability donated 55 yen on average, and those with low interpersonal trust donated 33 yen on average. This result seems convincing. In many cases, it seems that there is an awareness that "I don't know how much other people will donate, so I want to keep my donation at this level".

Smartphone app for a social dilemma experiment
    The calculation of the amount of money in the above social dilemma problem is easy, but it is convenient to have an app to proceed with the experiment smoothly. Reference [1] states that a PC was used, but the details are unknown. Here, we have created an app to perform this experiment using smartphones. Finally, I made a smartphone app with a design like Fig.1. This will be installed on the participant's smartphone. Data exchange between users is fully automated as described below. We thought that it would be desirable for each person to use a smartphone in order to proceed with the experiment without touching each other.

 

Fig.1 A support tool for social dilemma experiments

    An example of running this app with 4 students (4 smartphones) is shown below. Fig.2 shows the beginning of the first round, and each student decides how much to donate. Fig.3 is the execution result of the first round, reflecting those donations. 

Fig.2 Beginning of the first round (determining the donation amount)

Fig.3 Results of the first round (reflecting the distribution of donations)

Use of CloudDB (real-time database)
    The feature of this smartphone app is that each person's donation can be immediately reflected in other people's income without explicit communication or administrator intervention, as shown in Fig.3. That was easily accomplished by using CloudDB (real-time DB) built into MIT App Inventor. The main part is only the following figures. Fig.4 stores the donation amount in CloudDB together with the donor ID. When the donation is stored, the block of Fig.5 of the application of each smartphone is automatically called in real-time. Then, the distribution amount is calculated from the donation amount and added. In this way, the program has become very simple thanks to the real-time database.

 

Fig4. Blocks for registering donations to CloudDB

Fig.5 Blocks automatically called when registering donation to CloudDB

Notice
    By default, anyone can use CloudDB immediately with a common id and token. However, you cannot guarantee how long your data will live. Therefore, you can apply for your own id and token. Although the capacity of the free tier is small, it is stable and useful if you use it.

References
[1] Toshio Yamagishi : Social Dilemma ,PHP Laboratory, July 2000. (in Japanese)


2020年9月2日水曜日

国立国語研究所「言語資源活用ワークショップ2020」(その1)

 【要旨】国立国語研究所 コーパス開発センター主催の「言語資源活用ワークショップ2020」が、9月8日と9日に開催されます。ただし、新型コロナウィルス感染予防の観点からWeb開催です。情報系の研究者や技術者にとっては、少し分野が違いますが、ご参考までに(小生の理解の範囲で)概要を書きます。→続編はこちらにあります。

■ワークショップの趣旨
 本研究所は、日本語の全貌を把握するために様々な観点から研究開発を行っています。多様で大規模な言語コーパスの開発もその一貫です。今回は、そのようなコーパス言語学および言語資源研究の開かれた研究交流の場を提供するとのことで、一般からも関連の研究成果を募集しました。
https://pj.ninjal.ac.jp/corpus_center/lrw2020.html

■ワークショップの概要
 2日間で、シングルセッション(4セッション)で合計37件のポスター発表だけというゆったりしたスケジュールです。第1セッション(6件)は学生発表、残りは一般研究者です。もちろん「自然言語処理」に特化した研究ばかりではありませんが、この分野でも機械学習は重要な手段になりつつあるようです。実際、いくつかの発表タイトルには、このことを示唆する以下のようなキーワードが含まれています。

    単語N-gramを用いた分析
    JSON形式テキストの自動生成
    実践医療用語を構成する語の意味分布
    BERTによる単語埋め込み表現の分散値
    教師データを必要としない感情分析
    BERT の Masked Language Model
    語義曖昧性解消
    日本語学術文技術文長単位解析
    『分類語彙表』の質的拡張

■ワークショップの運営
    当初は、OSFのMeetings機能を使う予定でしたが、何らかの都合で、SLACKの掲示版(チャット形式)でポスター発表することになりました。ただし、発表者は、自分で開設したZOOMの部屋を用意すればそこでリアルタイムビデオ会話もできます。SLACKには要旨とポスター配置し、参加者とディスカッションします。質問したい人は、SLACKへログインする必要があります。(以下に小生のポスター例を示します。)


    しかしながら、いきなりポスターを見ただけでは質問もしづらいので、すでに予稿集(10ページ程度のフルペーパー)が公開されていますので、それを読んでから質問することになるでしょう。以下にプログラムと予稿があります。
(小生も12ページの予稿を書きました。セッション2のP2-5です。)

■予稿原稿の情報系一般との違い
    予稿原稿の形式ですが、情報系の一般的な論文形式と異なる点がありましたので、それを簡単に示します。
  • 情報系では、一般にダブルカラムだが、ここではシングルカラム
  • 参考文献に文献番号を記さない(これにはちょっと戸惑いました!)
  • 予稿原稿の枚数は、4枚以上で制限なし(多くの人は、10枚前後のようです)

2020年8月5日水曜日

散歩で見つけた昆虫をGoogle Lensで調べる(2020年度)

【趣旨】身近な昆虫等をスマホで撮影して、その名前をGoogle Lensで見つけましょう!

===============================================
ようやく梅雨が明け、本格的な夏のおとづれ。昆虫たちの動きも活発に!
昨年度(2019年度)は、以下の様に、24連勝でした!今年も続けます。
===============================================

25. アオスジアゲハ(ちょっと遠くから撮影のため、画像全体に占める蝶は非常に小さいのですが、特徴的な模様を見事に認識しているようです!)
 この例は、AIの本質的な特性を考える好例かもしれません。あなた(AI)は、この画像のどこに興味があるのですか?自分で判断してよいですよ。私(人間)は、私自身の興味と合致していれば受け入れるし、そうでないなら棄却しますから。

26. ハラビロカマキリ(かなり攻撃的な昆虫ですね。腹部を強く反り返させるこのポーズは、幼虫によく見られるようです。)


27. イトトンボ(美しい。玉の様な眼、赤み帯びた黄金の背、精密な縁紋と翅脈、輝く青銅色の細い胴体、軽やかなこの姿勢、どこからきたものなのでしょうか。もう、Google Lensの結果は要りません。ただただ、この画像を眺めるだけで十分です。)

    美しくも悲しい出来事が。There are things I don't want to say. Two of the three legs on the left side of this dragonfly are missing for some reason. Another dragonfly in the picture below has three legs normally on one side. Nature is in a harsh environment. However, this degree of damage will not affect survival. Good luck!

2020年7月28日火曜日

プログラムソースリスト等を綺麗にブログに貼り付ける

 できました。(えっ、今ごろ分かったの?)

 今まで、プログラムのソースリストやデータテーブル(表)を、手っ取り早く画像にして掲載していました。その場合、かなり高解像度の画像にしないと文字が綺麗にならないので、どうしたものか、と思っていました。

 でも、少し調べた結果、テキストのまま簡単に、書式付きで(背景もつけて)貼り込むことができました。HTMLをちょっと編集すればそれが可能なのですが、それが億劫で行っていませんでした。今後、以下のようにテキストで掲載しようかと思います。

 (ただし、このwebページをスマホの画面で見た場合は、横幅サイズがうまく調整されない場合があると思います。そのような場合は、画像の方が有利です。ともかく、本格的にソースコードを公開するのであれば、GitHubを利用ということになるでしょうが、なかなかそこまでは考えていません。)

 取りあえず、以下に、今回のきれいな貼り付けの成果を。

●前回記事(こちらです)にあったJavaScriptソースリスト
「植物の種類(区分)毎に、葉の特徴に関する記述をまとめる。」

 

   // “plants_real.json”は、2,965種(別名含む)の植物を記載したJSONファイル

 // 植物の種類(7区分)毎に「葉」の特徴に関する記述を纏める(集約する)

    $.getJSON("plants_real.json", d => {

        var obj = d.plants  //JSONファイルから植物の配列を取り出す

            .filter(p => p.alias == "N") //別名は除く

            .reduce((acc, x) =>{ //accは集約のための配列、xは個々の植物

                if (!acc[x.group]) acc[x.group] =[]; //groupは種類(区分)

                if (x.leaf.type != "") acc[x.group].push(x.leaf.type);

                if (x.leaf.what != "") acc[x.group].push(x.leaf.what);

                acc[x.group] = Array.from(new Set(acc[x.group]));

                return acc; 

            },{}); //以上で、objは集約結果の配列となる。{}はaccの初期設定用

        Object.keys(obj).forEach(k =>{ //kは種類(区分), obj[k]は集約結果

            if (k != "null") document.write("●"+k+":"+obj[k]+"<br>");

        });

    });


●上記プログラムの実行結果

●双子葉木本:葉,奇数羽状複葉,掌状複葉,楕円状の,小葉,3出複葉の,長楕円形の,葉腋,旧葉,若葉,掌状に裂けた,3出複葉,棘が大きく,被針形の,細長い,鱗片状の,卵形の,緑白色の丸い,奇数羽状複葉の,葉柄,倒卵形の,奇数羽状複葉で棘のある,常緑樹の,羽状複葉,偶数羽状複葉,線形の,披針形の細鋸歯の,掌状複葉の,革質の,卵形で束生する,2回偶数羽状複葉,葉裏,大きな,丸い,苞葉,白い2枚の,2回羽状複葉,羽状複葉の,広卵形の,3枚の,先が3浅裂した,有毛の,葉脈,3回羽状複葉

●双子葉草本:葉,3出複葉,卵形の,2回奇数羽状複葉,茎葉,奇数羽状複葉,丸い,小葉,複葉の,羽状深裂した,3出複葉の,苞葉,茎や,楕円形の,茎や広卵形の,3小葉,羽状複葉,円心形の,円心形の薄い,葉裏,茎も,茎と,線形の,偶数羽状複葉,心形の,2回3出複葉,矛形の,披針形の,掌状の,へら形の,裂片が鋭くとがった,小さくて厚い,2回羽状複葉,偶数,ほとんど毛のないへら状の,根生葉,水辺に生え,頂小葉の大きい,長い,広卵形の,羽状に深裂した,葉先,掌状複葉,掌状に深裂する,数回羽状複葉,繊毛のある,上部の,2~3回3出複葉,楕円状葉,2~3回羽状複葉,扁平な肉質の,狭披針形の,下葉,羽状複葉の,2出複葉,大きな掌状の,楕円状被針形の,葉柄,3出羽状複葉,奇数羽状複葉の,細長い,長楕円形の,狭卵形の,掌状複葉の,倒被針形の,羽状深裂した大きい,卵円形の,3角状の,無毛に近い,束生する,偶数複葉,幅広の,葉腋,腎円形の,卵心形の柄の長い,葉身が糸状に裂けた,偶数花状複葉,掌状に深裂した,3回3出羽状複葉,葉身,大きく丸い,広線形の,狭長楕円形の,裏に灰白色の毛のある,卵状被針形の,茎や波状鋸歯のある

●被子植物:省略

●単子葉草本:省略

●裸子植物:省略

●シダ植物:省略

●単子葉木本:省略

2020年7月27日月曜日

今さらJSONをJavaScriptで検索?なんて言わないで

【要旨】植物に関する自由形式記述からJSONファイルをJAVAを用いて自動生成しました。その結果のJSONの検索プログラムを、今回はJavaSriptで書きます。初心者の方にとって、ご参考になる点があれば幸いです。

■JSONを扱うならJavaScriptを使うのが普通?
 これまで、日本語の係り受け解析(MeCabとCaboChaによる)を使ってJSONを自動生成するプログラムをJAVAで作りました。その流れで、結果のJSONを検索するプログラムもJAVAで書いてきました。そこでは、ラムダ式とストリームが有効に働きました。

 しかし、JSONは元々JavaScriptと相性が良く、特にwebアプリにする際はJavaScriptの方が何かと便利かも知れない。そこで、今さら、という思いもあるのですが、いくつかの形態の検索をJavaScriptで作成してみました。具体的な方法はいくつかあるのですが、今回はjQueryを利用します。結論としては、JAVAの場合とほぼ同様の書き方でできました。

 JAVAで実施した場合の記事は以下にあります:
(対象としたJSONファイルの説明はこの記事の後半にあります。)

■JavaScriptによる検索(上記JAVAの場合の3例に対応)
 以下にソースコードを示します。JAVAの場合と同様に、.filter、.map、.reduce、.forEachなどを使っています。従来型の書き方と比較すると、このような検索をするのに、明示的なforループが全然出てきません。(テキストで綺麗に表示する方法が分からないので、画像にしてあります。)出力は、上記のJAVAの場合とほぼ同じ(形式が若干異なるが)になりましたので、省略します。

↑葉が"倒披針"で、花が"白"または"紫"の植物
↑掲載されている植物の果実の種類(type)を重複なく列挙する
↑植物の種類(区分)毎に、それに属する植物の葉の特徴を纏める

 こんな感じでJSONを検索できるので、フォームやボタンをつけて、ユーザフレンドリーな検索画面は作れそうですね。

2020年7月23日木曜日

梅雨の合間のアゲハチョウ (Swallowtail butterfly)

Atsugi-shi, Japan,  2020-07-22
It's still rainy season, but sometimes it's a little sunny.
At that time, I saw a swallowtail butterfly. Enjoy this short video!


2020年7月19日日曜日

植物の自由形式説明文からJSON形式テキストを自動生成(まとめ)

 これまで、植物の自由形式説明文からJSON形式テキストを自動生成して、多様な検索を可能とする試みについて書いてきました。8回くらい連載してきたと思います。今回は、これを分かりやすく(ポスター形式で)纏めてみました。

2020年7月5日日曜日

Beautiful mountain view comes automatically to your smartphone

revised [Version2, 2020-7-5] [Version1, 2019-12-19]

Abstract
The purpose of this project is to create an application that uses AI (artificial intelligence) to determine that a mountain is now in a beautiful landscape, and automatically notify it to the smartphone. You can enjoy the scenery with a live camera even in a distant mountain. However, the mountains are often covered with clouds, and the weather changes over a short period of time. Therefore, there will not be many opportunities to see the beautiful scenery. The smartphone app described below will automatically notify you when a photo that is so beautiful that you can see it up to the top of the mountain is obtained. Wouldn't it be fun if you received such a notification while drinking coffee?

Outline of the project
The weather in the mountains changes in a short time. Therefore, the scenery is not always beautiful so that you can see the summit. Here, we have created an app that automatically stores only the photos in which the mountain summit is clearly visible. As an example, this app recognizes the scenery of Rishiri mountain that is towering in northern Hokkaido of Japan in real-time and saves good photos. This app incorporates a trained model of this mountain images, but of course, it is easy to replace them with your favorite mountain images. 

Preparing mountain images
For image recognition, we used MIT's Personal Image Classifier [1]. First, we should classify many photos of this mountain into four categories, as shown in Fig1, depending on how they look. For this purpose, we quote images of a live camera [2] installed at the foot of the mountain. Get the image at appropriate intervals and give it a category label, as shown in Fig.1 and Fig.2. For each label, it is good to have more than 10 images. Enter them into MIT's Personal Image Classifier via your PC's web browser.

Fig.1 Classification into four categories

Fig.2 Preparing images for classification training

Training of the recognition model
When the images are ready, perform training as shown in Fig.3. Training takes place on the Neural Network. Adjust the network structure and some hyperparameter values if necessary. At the scale of this example, training usually ends in tens of seconds. Then download the trained model to your PC and use it in the App Inventor program as shown below.

Fig.3 Train our own model using MIT Personal Image Classifier

Using the trained model in App Inventor
The extension (named personalimageclassifier.aix) for using the downloaded model in App Inventor can be obtained from the reference [1]. The usage is very simple as shown in Fig.4. First, set the downloaded model to the Classifier properties, and then give the image you want to recognize to the block for classifying. As a result of recognition, the degree to which the image should be classified into each label is shown. That is, you get a list of label/confidence pairs. Finally, the image is classified into the label with the highest confidence value.

Fig.4 Blocks of the Personal Image Classifier

Getting the weather forecast
Before using this app, it is better to check the weather forecast for this mountain. If the weather is too bad, you can't expect a good picture. Press the button shown in Fig.5 to automatically display mountain weather up to 6 hours ahead. The weather forecast requires parsing a JSON file from OpenWeatherMap [3]. So we use our own JSON Decoding Extension (named FoYoJSON.aix) [4]. Fig.6 provides an overview of it. In order to use OpenWeatherMap, you need to get your own API key.

Fig.5 Checking weather forecast

Fig.6 Getting and decoding weather forecast JSON file

Running the app
An example of running the app is shown in Fig.7. In this case, it was recognized as "Cloudy but looks good" because there are many clouds but the mountainside looks good. Such a case is normal and it is rare to see it well up to the top.

Fig.7 Get an image and then recognize

Automatic notification of good scenery
If you are lucky, you will encounter a scene where you can see the summit as shown in Fig.8. In such a case, the recognition results in "Clear enough to see the summit". In that case, You can know that in two ways. One way is to feel the vibration of your smartphone, another way is to receive an e-mail automatically. If you choose the latter, the recognized image is automatically sent to the registered mail ID. Whichever you choose, if you have a PC nearby you will be able to enjoy the larger, clearer images immediately on your PC screen rather than your smartphone.

Fig.8 Automatic notification of beautiful scenery via email

Probably the easiest way to send emails automatically without human intervention is to use Taifun's extension [5] shown in Fig. 9 but note that it is a paid extension. 

Fig.9 Using an extension to send emails via SMTP without user interaction

Notice
(1) If the live camera image of your choice has a high resolution, you may need to reduce the resolution and pass it to the image classifier.
(2) To get and display the weather forecast, you need your own API Key for OpenWeatherMap. 
(3) If Error_908 is displayed when running the app, please allow access to External_Storage on your Android settings.

References
[1] MIT's Personal Image Classifier
https://appinventor.mit.edu/explore/resources/ai/personal-image-classifier
[2] Live camera at Rishiri Town Hall (in Japanese)
http://www.town.rishiri.hokkaido.jp/rishiri/
[3] OpenWeatherMap
https://openweathermap.org
[4] JSON Decoding Extension by FoYo
http://sparse-dense.blogspot.com/2019/01/a-json-decoding-extension-for-app.html?m=0
[5] Taifun's mail extension (paid)
https://puravidaapps.com/mail.php

2020年7月2日木曜日

係り受け解析は格フレーム辞書に強く依存するらしい

【要旨】3つの係り受け解析器CaboCha、KNP、GiNZAを試用してみて、素人ながら分かってきたことが少しあります。それをここ簡単に書き留めておきたい。

■3種類の日本語係り受け解析器
 十分使い込んだとは言えませんが、これまでに、以下の3つの係り受け解析器を利用させていただきました。いずれも、なんらかの自動構築の格フレーム辞書を利用していると思われます。(その具体的な違いは小生には分かりませんが。)

 ・CaboCha(参考資料[1])
 ・KNP (参考資料[2])
 ・GiNZA(参考資料[3])

■2つの例文についての解析結果の比較
 今回は、以下の2つの例文について、3つの解析器の解析結果を比較してみます。

例文(ア):葉は葉柄の長い卵形である。
例文(イ):葉は虎柄の長い卵形である。


 まず、例文(ア)に対する結果をみます。KNPは、「葉柄の長い」という係り受け関係をきちんと捕らえていますが、CaboChaとGiNZAはいずれも、「長い卵形」と判断しており、妥当ではありません。


 次に、例文(イ)です。これは例文(ア)に出現する"葉"が"虎"に変わっただけです!それだけの違いであれば、係り受け解析結果は変わらないのでは?と思いませんか。結果はどうでしょうか。(ア)の場合の判定結果(○×)が逆転してしまいました。すなわち、KNPは、「虎柄の長い」という不自然な係り受け関係を採用してしまいました。


■格フレーム辞書が解析精度を左右する
 上記の解析は、一例(2つの例文のみの)に過ぎません。しかしながら、人間が理解するのと同じく(自然な)係り受け関係を見つけるには、格フレーム辞書(詳しくは参考資料[4][5]を参照)が重要な役割を果たしているようです。参考資料[5]には、「Webから自動構築する膨大な格フレーム辞書は、蓄積が高まれば効果が出てくる反面、どうしても雑音も入ってしまうので、結果的に解析精度は頭打ちになるだろう」という趣旨の記述があります。共感できるように思います。

■Google翻訳はどうなっているか
 最後に、上の2つの例文をGoogle自動翻訳(英訳)にかけてみました。妥当な英文を生成するには、適切な係り受け解析が必要なはずです。結論として、2例文とも、妥当な英文になっています。このことから、相当に優れた格フレーム辞書を使っていることが推察できます。

 Google翻訳結果:
例文(ア):葉は葉柄の長い卵形である。
 -> The leaves are oval with long petioles.
例文(イ):葉は虎柄の長い卵形である。
 -> The leaves are long ovals with a tiger pattern.

参考資料
[1] CaboCha/南瓜: Yet Another Japanese Dependency Structure Analyzer
[2] 日本語構文・格・照応解析システム KNP 
[3] GiNZA - Japanese NLP Library
[4] 杉本徹、岩下志乃:Javaで学ぶ自然言語処理と機械学習、オーム社、平成30年9月発行
[5] 山本和英:箱庭言語処理のための格フレーム辞書構築の意義

2020年7月1日水曜日

Thank you, AppyBuilder, for a long time!

In my long-time development of smartphone apps, MIT App Inventor and its derivatives, AppyBuilder, and Thunkable, have been very useful. Among them, AppyBuilder completely stopped its service yesterday (2020-6-30). AppyBuilder has continued to offer a uniquely designed GUI and new features.  I would like to express my gratitude to Hossein, who played a central role in this, and to the colleagues who had useful discussions in the community. My first Extension program (with JAVA) was created using the CodeEditor published by Hossein. As many know, AppyBuilder is already on a new path as Kodular. I look forward to further development in the future. Finally, to commemorate, here are some screenshots of some of the apps I've developed so far with AppyBuilder.


I am very pleased that AppyBuilder's CodeEditor is still available in Kodular.  Kodular says "If you are an advanced user, why not code in Java and create amazing Extensions for the Creator?"