6 Replies - 1328 Views - Last Post: 09 May 2016 - 06:51 AM

#1 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5922
  • View blog
  • Posts: 20,246
  • Joined: 05-May 12

Challenge: Well formatted Eliza in 256 lines of C#?

Posted 05 May 2016 - 01:33 PM

Feeling a bit nostalgic, I went to look up the old program Eliza. In one of the links from the Wikipedia article is a pointer the BASIC version of the program.

Scanning the code, it looks to be pretty well formatted for BASIC programs written at that time. The entire program weighs in at 256 lines. Wow! My gut feel is that it's not possible to do the same in a C# console program in the same number of lines, and still keep to modern acceptable coding standards, even if using the 1TBS style. (Taking a peek at the Java implementation linked to by the same Wikipedia article shows the main class weigh in at 386 lines, and that's without the supporting classes.)

So the challenge: Is it possible to write a well formatted Eliza implementation in C# in a comparable number of lines as the the BASIC version?

I think it maybe possible if some of the strings are pushed out to a .RESX or text file, but I've not tried yet. I'll post my results in the next few days, but I think some of the other C# regulars may consider this an interesting challenge.

Is This A Good Question/Topic? 0
  • +

Replies To: Challenge: Well formatted Eliza in 256 lines of C#?

#2 SixOfEleven  Icon User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: Challenge: Well formatted Eliza in 256 lines of C#?

Posted 05 May 2016 - 01:51 PM

I haven't thought about Eliza in ages. It was a very interesting program back in that era and was quite fun to toy with. I might have to squeeze in some time to give this one a try.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5922
  • View blog
  • Posts: 20,246
  • Joined: 05-May 12

Re: Challenge: Well formatted Eliza in 256 lines of C#?

Posted 07 May 2016 - 07:35 AM

Well, the best I could do was about 352 lines using 1TBS, and completely dropping the code that scans through its DATA, by actually building the data structure that it builds in memory.

Spoiler


I could probably trim some more by removing the dependency injection support and moving Main() into the class. If I have time, I'll see if there is a way to use resources instead of keeping the strings in the code.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6995
  • View blog
  • Posts: 14,628
  • Joined: 16-October 07

Re: Challenge: Well formatted Eliza in 256 lines of C#?

Posted 07 May 2016 - 06:12 PM

Hmm... BASIC?!?

The original paper is paywalled, but if you google "ELIZA a computer program for the study of natural language communication between man and machine" you'll find it. Looks like Joseph Weizenbaum wrote the first one in SLIP, which I'd never even heard of.

I'm too lazy to start working from the basic with a screen shot. I found a reprint of an IBM basic version here: https://www.jesperju...eliza/ELIZA.BAS

Found this to be an interesting bit of history: http://gutenberg.us/...000010235/eliza

Which leads to the creative computing this puppy first appears in: https://archive.org/...3n04JulyAug1977

A shorter version shows the IBM PC version reasonably close to the BASIC original: http://shrager.org/v...veComputing.pdf

And, from the guy who wrote the code that appeared in Creative Computing 1977, a decent geneology of the whole thing: http://elizagen.org/

Sorry, research got the best of me. I'll play at a C# version later.
Was This Post Helpful? 1
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5922
  • View blog
  • Posts: 20,246
  • Joined: 05-May 12

Re: Challenge: Well formatted Eliza in 256 lines of C#?

Posted 07 May 2016 - 07:37 PM

For folks just joining in now, I suggest following the link to the IBM BASIC version rather than the links to the Creative Computing versions. The latter has a bug in the repeated input detection.

Also be warned that in both versions, the keyword-to-reply index and count values have bugs in their values. The index and counts all work out correctly in the end, but if you scrutinize the keyword vs. reply, you'll notice that some of replies actually belong to the adjacent keyword. Here is a link to the commit in my Bitbucket repository where I fixed the reply string mappings in my C# code.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5922
  • View blog
  • Posts: 20,246
  • Joined: 05-May 12

Re: Challenge: Well formatted Eliza in 256 lines of C#?

Posted 08 May 2016 - 07:46 AM

Well, duh! What was I thinking? I think it was pretty obvious that I was not.

The point of the BASIC DATA and READ statements was to give the program to ability to read blocks of pre-configured memory. The need for this was that early on, I/O was not yet standardized and so it made more sense to embed the data in along with the program. Nowadays with I/O being pretty much standardized with the language or its supporting libraries, there is really no need to have the data embedded in the program. In fact, it's now considered bad form to do so.

So all that I need to do is move the data structures into JSON and just deserialize. Presto! The code will be much shorter.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5922
  • View blog
  • Posts: 20,246
  • Joined: 05-May 12

Re: Challenge: Well formatted Eliza in 256 lines of C#?

Posted 09 May 2016 - 06:51 AM

108 lines of C# (not using 1TBS) + 145 lines of JSON = 253 lines.
Spoiler

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1