HUO Writer: A simple AI writer with basic Neural Network capabilities (QB64)

News

  • 2021-02-14: Updated phrases the AI uses. Added more comments to make the code more readable and easier to configure new phrases.

Goal

The goal of Huo Writer is to be a program that thinks philosophically. By getting user input on a specific topic, it can generate phrases with deep philosphical meaning so as to trigger thoughts on the human user.

The goal of the article is to serve as a simple tutorial on how to build a program that can have a conversation with a human, for people with limited or no programming skills.

An Appendix describes how the program can be enhanced with neural network capabilities, by showing a totally primitive way of helping the program improve its responses based on human feedback.

Go directly at the end to get the code of the program.

Other programming tutorials

Introduction

Programming is easy. And difficult.

It is easy in the sense that all you have to do in order to do it, is… do it! As in all things. There is no magic words to use, no secret key, no hidden doors to unlock. Simply open your computer and start programming. As simple as getting up in the morning and making coffee.

Of course, you must know how to make coffee.

That is the difficult part of programming. But do not worry. As in all other programming tutorials in our portal, we will try to keep it simple.

First of all, we will use the simplest high-level language for our tutorial, i.e. QBasic (and in particular, the 64-bit version which can run in any modern Operating System, QB64). BASIC is, as the name implies, basic. Very easy to learn. There are tons of resources in the Internet on the language; be sure to search for QB64 (QBasic 64 bit) mainly, so as to make sure you search for the right thing.

Secondly, the program itself is ultra-simple. The code is based on a very limited set of commands (mainly PRINT, IF, CASE, INPUT, RANDOMIZE) which you can easily learn. And even if you don’t you are able to understand what they do by reading this article!

Huo Writer on faith, problems, helping…

Yet and despite the above, the actual result is very good! The program actually does sound like a philosopher and writes deep stochastic phrases which will trouble you and invoke thoughts.

Look at the text below…

Never do faith supports us.
Would faith exist without you?
Ignorant men.
Others create reality.
Don’t you remember?
Stop thinking. And you will see…

~ HUO Writer, 2020-09-01

It was written by the program when discussing about the tags faith, helping, problems. Taking into account how simple and ultra-light the program is, it is really good! Don’t you agree? (Note the small gramatical errors, these are still part of the problems still to be solved)

Is it Artificial Intelligence though?

Well, surely it wasn’t created on its own, so yes it is artificial. And it does include some very basic rules to randomly generate what it says. Last but not least, the result seems like it is intelligent, so that is the greatest argument in favor of answering yes to this question. After all, how to I know that you are not robot?

I. How the program works

The program works in the following simple steps:

  1. Ask input from the user: The program needs from the user to enter some tags related to topic of discussion. For each tag, the gender (male, female, neutral, adjective, indifferent) and the number (plural, single, indifferent) must be defined. Note that some of the values are not exactly ‘correct’, for example the ‘adjective’ is not a gender. The values entered are then used by the program to properly formulate the phrases it will generate to speak with the human.
  2. Generate the phrases to say, by not-fully randomly combining the tags provided by the user with specific words and phrases in the database of the program.

Simple huh?

Well, it is!

The most difficult part is to enter meaningful and cleverly selected words and phrases in the database so that the phrases generated by the program can sound as philosophical and meaningful. As in any other thing, the ‘Garbage in-Garbage out’ principle stands strong.

II. Step 1: Get user input

Getting user input is as simple as using the… INPUT command in QBasic and ask from the user the input.

Just see the code below on how the program asks from that input.

PRINT "What is our theme of discussion to-day?"
PRINT ""
INPUT "Number of tags: ", tagsNo
PRINT "Thank you"
SLEEP 2
PRINT "": PRINT "Now…": PRINT ""
SLEEP 2
PRINT "Please enter related tags one by one…"
SLEEP 2
PRINT "One word only per tag please. All in small letters."
PRINT "Enter Bye or just press Enter to exit."
'Read related tags for the subject of discussion
FOR I = 1 TO tagsNo

PRINT ""
INPUT "Enter Tag: ", Tags$(I, 1)
IF Tags$(I, 1) = "Bye" OR Tags$(I, 1) = "" THEN END

TagGender:
INPUT "ENTER THE TAG'S GENDER (m, f, n, a, i) : ", Tags$(I, 2)
IF Tags$(I, 2) <> "m" AND Tags$(I, 2) <> "f" AND Tags$(I, 2) <> "n" AND Tags$(I, 2) <> "a" AND Tags$(I, 2) <> "i" THEN GOTO TagGender

TagNumber:
INPUT "ENTER THE NUMBER OF THE TAG (s, p, i) : ", Tags$(I, 3)
IF Tags$(I, 3) <> "s" AND Tags$(I, 3) <> "p" AND Tags$(I, 3) <> "i" THEN GOTO TagNumber

NEXT I

Initially the program asks for the number of the tags to be entered and then, for each tag (hence the FOR… NEXT loop), the user is asked to enter the parameters (gender, number) of the tag.

As you can see, all the tags entered as placed inside the Tags$ table. This table will be then used to generate the phrases.

The tag itself is stored in dimension 1 of the table, while the parameters of the tag are stored in the dimensions 2 and 3 of the table.

Tags table dimentions

  • Dimension 1: The tag itself
  • Dimension 2: This stores the gender
  • Dimension 3: This stores the number

These parameters will be used later on in validation rules necessary for the correct creation of the phrases.

III. Step 2: Generating the phrases

Based on the input of the user, the program combines the tags with specific phrases or words in the database of the program.

The program generates the following phrases:

  1. Phrase H.1: The first phrase, combining phrases from tables H1$, H2$, one of the tags and a phrase from table H3$.
  2. Phrase H.2: The second phrase, combining a phrase from table H4$, one of the tags and a phrase from table H5$.
  3. Intermediate phrase 1: A connecting phrase, which includes a phrase from table I1$.
  4. Phrase K: The third phrase, combining phrases from tables K1$, K2$ and K3$.
  5. Intermediate phrase 2: A connecting phrase, which includes a phrase from table I2$.
  6. Terminating phrase: The closing phrase, which includes a phrase from table T$.

As mentioned, the elements used for each phrase as documented in the program’s database. By ‘database’ we do not refer to a relational database, but to tables (e.g. tables H1$, H2$, H3$ used for the first phrase) which are populated with data with the code of the program.

READ ALSO:  Thinking. Not with the brain?!

These rables are filled in as follows…

'PHRASE 3 (Phrase K)
'Set Variables for third phrase (Phrase K) generation
'----------------------------------------------------
K1$(1, 1) = "Death": K1$(1, 2) = "i": K1$(1, 3) = "i"
K1$(2, 1) = "Life": K1$(2, 2) = "i": K1$(2, 3) = "i"
K1$(3, 1) = "Self": K1$(3, 2) = "i": K1$(3, 3) = "i"
K1$(4, 1) = "God": K1$(4, 2) = "i": K1$(4, 3) = "i"
K1$(5, 1) = "Existence": K1$(5, 2) = "i": K1$(5, 3) = "i"
K1$(6, 1) = "Being": K1$(6, 2) = "i": K1$(6, 3) = "i"
K1$(7, 1) = "Reality": K1$(7, 2) = "i": K1$(7, 3) = "i"
K1$(8, 1) = "Others": K1$(8, 2) = "i": K1$(8, 3) = "p"
K1$(9, 1) = "Thought": K1$(9, 2) = "i": K1$(9, 3) = "i"
K1$(10, 1) = "Knowledge": K1$(10, 2) = "i": K1$(10, 3) = "i"
'-----------------------------------------------------
K2$(1, 1) = " defines ": K2$(1, 2) = "i": K2$(1, 3) = "s"
K2$(2, 1) = " is defined by ": K2$(2, 2) = "i": K2$(2, 3) = "s"
K2$(3, 1) = " creates ": K2$(3, 2) = "i": K2$(3, 3) = "s"
K2$(4, 1) = " is created by ": K2$(4, 2) = "i": K2$(4, 3) = "s"
K2$(5, 1) = " define ": K2$(5, 2) = "i": K2$(5, 3) = "p"
K2$(6, 1) = " are defined by ": K2$(6, 2) = "i": K2$(6, 3) = "p"
K2$(7, 1) = " create ": K2$(7, 2) = "i": K2$(7, 3) = "p"
K2$(8, 1) = " are created by ": K2$(8, 2) = "i": K2$(8, 3) = "p"
'------------------------------------------------
K3$(1, 1) = "death.": K3$(1, 2) = "i": K3$(1, 3) = "i"
K3$(2, 1) = "life.": K3$(2, 2) = "i": K3$(2, 3) = "i"
K3$(3, 1) = "self.": K3$(3, 2) = "i": K3$(3, 3) = "i"
K3$(4, 1) = "being.": K3$(4, 2) = "i": K3$(4, 3) = "i"
K3$(5, 1) = "others.": K3$(5, 2) = "i": K3$(5, 3) = "i"
K3$(6, 1) = "existence.": K3$(6, 2) = "i": K3$(6, 3) = "i"
K3$(7, 1) = "reality.": K3$(7, 2) = "i": K3$(7, 3) = "i"
K3$(8, 1) = "God.": K3$(8, 2) = "i": K3$(8, 3) = "i"
K3$(9, 1) = "thought.": K3$(9, 2) = "i": K3$(9, 3) = "i"
K3$(10, 1) = "knowledge.": K3$(10, 2) = "i": K3$(10, 3) = "i"

As you can see, as with tags, dimensions 2 and 3 are used to store the gender and the number of the elements.

The tables documented above are for the creation of the K Phrase (i.e. the third phrase). The program randomly selects an element from each table and finally creates the phrase to show to the use.

The phrases are generated, by generating random number and then using these numbers to get the relevant entries from the tables defined above.

An example of the code used to get random numbers and create the phrases (the example is from Phrase K) is depicted below.

'----------------------
'Generate third phrase
'----------------------
PhraseK:
RANDOMIZE TIMER
A = INT(RND * 9) + 1: B = INT(RND * 7) + 1: C = INT(RND * 9) + 1
IF C = A THEN GOTO PhraseK
'If number is not equal (plural with plural, single with single) or the number of the next element is not indifferent, then generate phrase again
IF K1$(A, 3) <> K2$(B, 3) AND K2$(B, 3) <> "i" THEN GOTO PhraseK

As said before, the code is simple. In most cases it is. It is the idea that could be complex either in percieving it, implementing it or both. (or selling it I would say, but that is part of another bigger discussion)

Phrases generation rules

There are some basic validation rules applied for the phrases generation. As the program progresses, these rules will be amended and improved.

One rule for example is the following: the number of an element must match with the number of the tag combined with that element. The code below does exactly that thing: If the number (i.e. the 3rd dimension) of the first and the second element do not match, then the program is instructed to go back and generate the phrase again.

IF K1$(A, 3) <> K2$(B, 3) AND K2$(B, 3) <> "i" THEN GOTO PhraseK

If the validation fails, then the program generates a new phrase.

Printing the phrase

After the phrases are generated, they are printied on the screen. With what else than the… PRINT command.

Voila!

A philospher at your own hands!

APPENDIX I – Neural Network

Neural networks are all about having a program learn from its mistakes. Usually this happens with feedback from its user, while the most advanced programs have the ability to learn also on their own. I attempted to start implementing a primitive type of neural network capability.

If the user activates the Learning Mode, the program asks for input after each phrase it produces. If the input is negative (i.e. the user says that he did not like the phrase generated) then the program stores the combination in a table storing ‘bad’ combinations with the code below.

PRINT "": PRINT "Was this sentence a good one? (y/n)"
e$ = ""
DO
    DO
        hyn$ = INKEY$
    LOOP WHILE hyn$ = ""
    SELECT CASE hyn$
        CASE "y", "Y"
            e$ = ""
        CASE "n", "N"
            BadCombinationsK(Kcounter, 1) = A
            BadCombinationsK(Kcounter, 2) = B
            BadCombinationsK(Kcounter, 3) = C
        CASE ELSE
            e$ = "A"
    END SELECT
LOOP WHILE e$ <> ""
PRINT "Thank you for your input.": PRINT ""

The next time the program speaks with the user, the bad combinations are not used in the phrase generation. In that sense, the program learns from the human input. One can visualize the nodes and their combinations are human brain neurons – the neurons which are not used often are gradually disabled while the neurons which are used often are strengthened. Now the program only has the capability to totally ‘delete’ one set of nodes from the possible responses, but future enhancements could see it behave in a more elaborate way.

An example of the code doing that is depicted below.

'Neural network: Check if the combination selected was discarded by human previously. If yes, generate a different one!
FOR I = 1 TO Kcounter
IF BadCombinationsK(I, 1) = A AND BadCombinationsK(I, 2) = B AND BadCombinationsK(I, 3) = C THEN GOTO PhraseK
NEXT I

As mentioned above, this is the simplest form of a neural network: The program ‘learns’ from human interaction and adjusts the ‘nodes’ (elements used for the phrases) inside its ‘brain’ (tables holding the elements used for the phrases) accordingly.

Note that this works only inside the same instance of the program. Whenever you restart the program the ‘bad nodes’ are forgotten, since they are not stored permanently anywhere.

Improvements that are coming soon

  • Make the learning not only disable nodes (0 or 1), but also decrease the likelihood of a node being used (with probability values between 0 and 1).
  • Store what the program has learned into a txt file, so that the next time you execute it, it still ‘remembers’ what it has learnt.

APPENDIX II – The source code

You can click at the link below to get the source code.

Simply copy-paste it into a QBasic editor and execute.

APPENDIX III – How to configure the program

The program is easy to configure. And that is why it is so fun! I have added phrases which are related to my personality and the way I am thinking. You can alter them to reflect yours!

You can also add new elements for the phrases! Simple go and add new elements in the relative tables. When doing that, rememeber to also define the gender and number of the element.

After having added the new element, do not forget to increase the relative constant which defines the size of the table! The constants defining the size of the elements’ tables can be found in the beginning of the program.

Comments (

)

Discover more from Harmonia Philosophica

Subscribe now to keep reading and get access to the full archive.

Continue reading

Verified by ExactMetrics