今時の開発環境で動きそうなELIZA実装は?

2018/07/02
藤田昭人

 

『ELIZAをめぐるあれこれ(仮)』について その1 

ワイゼンバウムの作ったオリジナルとの互換性を維持し、今時のソフトウェア開発環境でも比較的容易に動作する ELIZA 実装を探して見ました。

 

まずは LISP の実装から…

書籍 "Artificial Intelligence: A Modern Approach" (邦題『エージェントアプローチ人工知能 第2版』)の著者として有名な Peter Norvig のもう一つの名著 "Paradigms of AI Programming: Case Studies in Common Lisp" (邦題『実用 Common Lisp』)の5章では ELIZA について実装ベースで解説しています。ちなみに、いずれも翻訳本は1万円を越す馬鹿高い書籍ですが、原書はタダでダウンロードできるので頑張って英語を読む方が安上がりです。もちろん原書に掲載されているソースコードも公開されています。

github.com

それから、この書籍の Lisp コードを Python に書き直す paip-python なるプロジェクトも存在しているようです。

github.com

もちろん ELIZA の実装も収録されています。他にも Python ベースの ELIZA 実装が比較的簡単に見つけられます。

ちなみに『ひょっとしたら?』と思い立ち、PAIP in Clojure も調べてみたところ、そういう狙いのプロジェクトがいくつか見つかりました。が、ELIZA の実装は見つけられませんでした。

ご存知ない方も多いと思いますが、元祖スクリプト言語とも呼べそうな Lisp は今時のウェブ系の開発でも重宝するとのこと。頭の柔らかい方々にはこちらの方がオススメなんじゃないかと思います。

 

もう少し頭の固い人向けには…

とかオススメしておきながら何ですが、昔から Lisp ができない(苦手な)僕のような少し頭の固い人向けには、よくできた Java の実装があります。

Charles HaydenEliza test はワイゼンバウムのオリジナルと同じ動作をするように注意して開発された Java による Eliza 実装です。

まずはページの The Source にあるソースコードを全部ダウンロードしてください。

15本あるJavaファイルを全部ダウンロードし終えたら、 "Eliza" という名前のディレクトリを作成して、そこへダウンロードしたファイルを全て移動させてください。あとは "Eliza" ディレクトリの上位ディレクトリで普通に javac でコンパイルするだけです。

$ ls
Eliza
$ javac Eliza/*.java
注意:一部の入力ファイルは推奨されないAPIを使用またはオーバーライドしています。
注意:詳細は、-Xlint:deprecationオプションを指定して再コンパイルしてください。
注意:入力ファイルの操作のうち、未チェックまたは安全ではないものがあります。
注意:詳細は、-Xlint:uncheckedオプションを指定して再コンパイルしてください。
$ java Eliza/ElizaApp
There was a problem reading the script file.
Tried script
$

 ワーニングは無視してください。おそらく Java のバージョンも問わないはずです。(僕は Oracle JDK10 でコンパイルしました)

次に実行に必要な2つのファイルを用意します。1つ目は DOCTOR スクリプトHayden Eliza はスクリプトシンタックスが異なります。"How Eliza Works" なるドキュメントの後ろ半分にDOCTOR スクリプトですので、このファイルをダウンロードしてきて、後ろ半分(initialから始まる行以降全部)を切り出し、"script" という名前のファイルとしてセーブしてください。もう1つは Eliza への入力データです。以下はワイゼンバウムの論文に掲載されていた実行例の入力のみを "test" というファイルにセーブして Eliza を実行した例です。

$ ls
Eliza script
$ cat > test
Men are all alike.
They're always bugging us about something or other.
Well, my boyfriend made me come here.
He says I'm depressed much of the time.
It's true. I am unhappy
I need some help, that much seems certain.
Perhaps I could learn to get along with my mother.
My mother takes care of me.
My father.
You are like my father in some ways.
You are not very aggressive but I think you don't want me to notice that.
You don't argue with me.
You are afraid of me.
My father is afraid of everybody.
Bullies.
$ java Eliza/ElizaApp
>> Hello.
How do you do. Please state your problem.
>> Men are all alike.
In what way ?
>> They're always bugging us about something or other.
Can you think of a specific example ?
>> Well, my boyfriend made me come here.
Your boyfriend made you come here ?
>> He says I'm depressed much of the time.
I am sorry to hear that you are depressed.
>> It's true. I am unhappy
Do you think that coming here will help you not to be unhappy ?
>> I need some help, that much seems certain.
What would it mean to you if you got some help ?
>> Perhaps I could learn to get along with my mother.
Tell me more about your family.
>> My mother takes care of me.
Who else in your family takes care of you ?
>> My father.
Your father ?
>> You are like my father in some ways.
What resemblence do you see ?
>> You are not very aggressive but I think you don't want me to notice that.
What makes you think I am not very aggressive but you think I don't want you to notice that ?
>> You don't argue with me.
Why do you think I don't argue with you ?
>> You are afraid of me.
Does it please you to believe I am afraid of you ?
>> My father is afraid of everybody.
What else comes to mind when you think of your father ?
>> Bullies.
Lets discuss further why your boyfriend made you come here .
$

ワイゼンバウム論文と同じ返答が返ってくることを確認できましたでしょうか?このように ELIZA は非常にポータブルなプログラムでもありました。では、Eliza のもう一つの有名な会話録を実行して見ましょう。

$ java Eliza/ElizaApp script parry
>> Hello.
How do you do. Please state your problem.
>> People get on my nerves sometimes.
Your nerves sometimes ?
>> You should pay more attention.
We were discussing you -- not me.
>> You're entitled to your own opinion.
What makes you think I am entitled to my own opinion ?
>> It bothers me just to be around people in general.
Does that have anything to do with the fact that your nerves sometimes ?
>> I went to the track at Bay Meadows a while back.
You say you went to the track at bay meadows a while back ?
>> I went to the races.
Can you elaborate on that ?
>> Horseracing interests me.
I'm not sure I understand you fully.
>> There's a lot of crooked gambling going on there.
Please go on.
>> It's clear to me if not to you.
Do you think its likely that not to I ?
>> I went to the races.
Do you say you went to the races for some special reason ?
>> I gamble on horses.
That's quite interesting.
>> I went to the races.
You say you went to the races ?
>> Gambling has been nothing but trouble to me.
What does that suggest to you ?
>> I've had experience with crooks gambling at the track.
Do you feel strongly about discussing such things ?
>> I've already told you enough about that.
Oh, I enough about that ?
>> I went to the races.
Can you elaborate on that ?
>> Do you know anything about bookies?
You're not really talking about me -- are you ?
>> A bookie didn't pay me off once.
I'm not sure I understand you fully.
>> I beat up the bookie who did not pay off, and I thought he might use his friends in the underworld to get even with me.
Do you say you beat up the bookie who did not pay off for some special reason ?
>> What do you know about racketeers?
Why do you ask ?
>> What experience have you had gambling?
Does that question interest you ?
>> I like horseraces.
That's quite interesting.
>> I know the mob controls the big rackets.
You say you know the mob controls the big rackets ?
>> Have you ever really thought about the MAFIA?
What are your feelings now ?
>> Let's talk about something else.
Please go on.
>> You keep asking me the same things, why not change the topic?
In what way ?
>> What do you mean?
What is it you really wanted to know ?
>> I thought I told you, people get on my nerves.
Perhaps in your fantasies we thought you told each other.
>> Well, I don't know.
Don't you really know ?
>> I would say so.
Can you elaborate on that ?
>> I would rather not discuss that anymore.
Do you say you would rather not discuss that anymore for some special reason ?
>> I guess so.
That's quite interesting.
>> What are you getting at?
Are such questions much on your mind ?
>> It's not a matter or like or dislike.
What does that suggest to you ?
>> I am getting tired of answering the same questions over and over.
What resemblence do you see ?
>> Why do you need to know that?
What answer would please you most ?
>> I don't understand your motives.
Why don't you understand my motives ?
>> Shouldn't I be?
You say shouldn't you be ?
>> That's just the way it is.
Do you feel strongly about discussing such things ?
>> So?
I'm not sure I understand you fully.
>> You harp on the same questions too much.
What does that similarity suggest to you ?
>> I don't get you.
Do you wish to be able to get I ?
>> It's about time you showed some interest in my feelings.
Why do you say your feelings ?
>> I will help you if I trust you.
Do you wish that you trust I ?
>> Maybe yes, maybe no.
You don't seem quite certain.
>> Really?
Does that have anything to do with the fact that your feelings ?
>> In my opinion you have a one-track mind.
Does that suggest anything else which belongs to you ?
>> I used to go to the track every week.
Can you elaborate on that ?
>> I avoid racetracks now because the races are fixed.
Do you say you avoid racetracks now because the races are fixed for some special reason ?
>> I'd rather not discuss it.
Lets discuss further why your opinion I have a one track mind .
>> That's something I don't want to go into.
Does that trouble you ?
>> I went to the races.
That's quite interesting.
>> It's none of your business.
Why are you concerned over my business ?
>> I don't confide in strangers.
Don't you really confide in strangers ?
>> Let's change the subject.
Please go on.
>> I went to the races.
You say you went to the races ?
>> What's wrong with you that you keep on bugging me about this?
Why do you think I that I keep on bugging you ?
>> The way you repeat yourself is getting ridiculous.
We were discussing you -- not me.
>> There are some tricky operators down there.
Did you think they might not be some tricky operators down there ?
>> The guys involved in the horseracing racket.
What does that suggest to you ?
>> You are a real nag.
Does it please you to believe I am a real nag ?
>> I have had enough of this.
Can you elaborate on that ?
>> Goodbye.
Goodbye. Thank you for talking to me.
$

この会話、実はインターネットのプロトコル仕様のドキュメントである Request for Comment (RFC) に収録されています。

RFC 439 - PARRY encounters the DOCTOR

この時の話し相手である PARRY の正体は後ほど紹介するとして、この事例から見てもELIZA がいかにもっともらしい返事をするか?を十分納得してもらえるのではありませんか?

 

その2に続く