• mind0037.pl Strong AI Perl6 Killer App artificial intelligence

    From mentificium@gmail.com@21:1/5 to All on Mon Jan 18 14:00:31 2016
    #!/usr/bin/perl
    use strict; # PERL by Example (2015) p. 77
    use warnings; # PERL by Example (2015) p. 85
    # use feature 'say'; # PERL by Example (2015) p. 73
    # mind0037.pl being coded on Mon.18.JAN.2016 or later.
    our $. = 0; # 2016jan14: line counter variable; PbyX 299
    our $_ = " "; # 2016jan10: hidden variable; P_by_Ex p. 301
    our $act = 0; # 2016jan16: quasi-neuronal activation-level
    our $age = 0; # 2015apr26: Temporary age for loop-counting.
    our $audbeg = 0; # 2016jan17: auditory-beginning tag for start of word.
    our $audnew = 0; # 2016jan17: holds onset-tag while rest of word comes in. our $audpsi = 0; # 2016jan17: concept number of word in @aud array
    our $audrecog = 0; # 2017jan18: auditory recognition concept-number
    our $audrv = 0; # 2015may13: auditory recall-vector for speech()
    our $audult = 0; # 2016jan17: ultimate-tag for end of word-engram
    our $bias = 0; # 2015jun04: EnParser; NewConcept: expected POS
    our $char = " "; # 2016jan12: For use with getc in FileInput()
    our $cns = 32; # 2015apr26: size of central nervous system
    our $coda = 0; # 2016jan16: "tail" or time of word-end.
    our $dba = 0; # 2016jan16: doing-business-as noun-case or verb-person our $engram = " ";
    our $enpanel = " "; # 2016jan16: Explicit package for showing $en array.
    our $entag = " "; # 2016jan16: Explicit package for showing $en array.
    our $lex = " "; # 2016jan16: Explicit package for showing $en array.
    our $krt = 0; # 2015may10: knowledge representation time
    our $lftell = 0; # 2016jan18: linefeed-tell for final word in line
    our $mfn = 0; # 2016jan16: masculine-feminine-neuter gender flag.
    our $msg = " "; # 2015may01: $msg is for "message" of input.
    our $nen = 0; # 2015may15: English lexical concept number
    our $num = 0; # 2016jan16: number-flag for grammatical number.
    our $onset = 0; # 2016jan16: onset-tag for use as recall-vector.
    our $panel = " "; # 2015may17: explicit package from @panel array
    our $penult = 0; # 2016jan17: penultimate for going back one unit.
    our $pho = ""; # $pho is for a "phoneme" or character of input.
    our $pos = "5"; # 2016jan16: part-of-speech for the @en English array.
    our $pov = 35; # 2015jun04: point-of-view: #35 internal; *42 external
    our $rv = 55; # 2016jan16: recall-vector for auditory memory.
    our $t = 0; # 2015may02: Lifetime experiential time "$t"
    our $t2s = 0; # 2015apr26: auditory text-to-speech index
    our $tult = 0; # 2016jan18: t penultimate, or time-minus-one
    our @aud = " "; # 2015may02: auditory memory array
    our @en = " "; # 2015apr25: English lexical array
    our @psi = " "; # 2015apr25: psi conceptual array
    sub AudBuffer; # 2016jan15 PbEx5e p. 351: Forward declaration.
    sub AudInput; # 2015apr25 PbEx5e p. 351: Forward declaration.
    sub AudMem; # 2015apr25 PbEx5e p. 351: Forward declaration.
    sub emotion; # 2015apr25 PbEx5e p. 351: Forward declaration.
    sub EnVocab; # 2015jun01 PbEx5e p. 351: Forward declaration.
    sub FileInput; # 2016jan10 PbEx5e p. 351: Forward declaration.
    sub motorium; # 2015apr25 PbEx5e p. 351: Forward declaration.
    sub NewConcept; # 2015may28 PbEx5e p. 351: Forward declaration.
    sub OldConcept; # 2016jan14 PbEx5e p. 351: Forward declaration.
    sub sensorium; # 2015apr24 PbEx5e p. 351: Forward declaration.
    sub think; # 2015apr24 PbEx5e p. 351: Forward declaration.
    sub VisRecog; # 2015apr26 PbEx5e p. 351: Forward declaration.
    sub volition; # 2015apr24 PbEx5e p. 351: Forward declaration.

    TabulaRasa: { # 2015may28: Establish size and emptiness of CNS.
    print "Size of AI memory is $cns \n"; # 2015apr26
    my $trc = 0; # 2015apr2015 $trc is "tabula rasa counter".
    until ($trc == $cns) { # 2015apr26 PbEx5e p. 193 "Loops".
    $psi[$trc] = 0; # 2015may28: Fill conceptual @psi with zeroes.
    $en[$trc] = "0,0,0,0,0,0,0"; # 2016jan17: seven flags set to zero.
    $aud[$trc] = " "; # 2015may18: Fill CNS with space-32's.
    $trc++; # 2015apr26: Increment tabula-rasa-counter $trc.
    } # End of loop filling auditory memory with blank engrams.
    }; # End of TabulaRasa "clean slate" sequence.


    sub AudBuffer() { # http://code.google.com/p/mindforth/wiki/AudBuffer
    # print "AudBuffer has been called. \n"; # 2016jan15
    } # End of AudBuffer; return to AudInput or FileInput or VerbGen.


    # 2015jun01 Requirements of EnVocab English vocabulary mind-module:
    # EnVocab shall create nodes of lexical memory with associative tags.
    # EnVocab shall only _control_ English words and not _hold_ English words.
    sub EnVocab() { # http://mind.sourceforge.net/envocab.html
    $act = 8; # 2016jan16: Test with typical activation $act
    $num = 1; # 2016jan16: Test with typical number = 1.
    $mfn = 3; # 2016jan16: Test with mfn=3 neutral gender.
    $dba = 1; # 2016jan16: Test with dba=1 for subject or first person.
    $pos = 5; # 2016jan16: Test with pos=5 for typically a noun.
    $rv = $audnew; #2016jan17: $audnew is from FileInput().
    $audnew = 0; # 2016jan18: Reset $audnew after transferring value.
    $tult = $t - 1; # 2016jan18: penultimate time
    print " EnVocab: Stores rv ", $rv, " at tult = ", "$tult\n"; #2016jan18
    $en[$tult] = "$nen,$act,$num,$mfn,$dba,$pos,$rv"; # 2016jan18
    my @lex=split(',',$en[$tult]); # 2016jan18 PbE
    print " EnV: $lex[0],$lex[1],$lex[2],$lex[3],$lex[4],$lex[5],$lex[6] \n"; #2016jan18
    } # http://code.google.com/p/mindforth/wiki/EnVocab


    # 2015jun04 Requirements of EnParser English Parser module:
    # EnParser shall at first assume that a new word is a noun.
    # EnParser shall call InStantiate to complete the parsing process.
    sub EnParser() { # http://mind.sourceforge.net/parser.html
    $bias = 5; # 2015jun04: Expect a noun until overruled.
    print "EnParser has been called by NewConcept \n"; # 2016jan14
    # print join(", ", caller , "\n"); # 2016jan15: PERL Black Book p. 306
    } # http://code.google.com/p/mindforth/wiki/EnParser


    sub OldConcept() { # http://mind.sourceforge.net/oldcept.html
    print "\nOldConcept() has been called by FileInput().\n"; # 2016jan15
    } # http://code.google.com/p/mindforth/wiki/OldConcept


    # 2015may28 Requirements of NewConcept mind-module:
    # NewConcept shall create a concept in @psi for each new word encountered.
    # A flag-panel shall lodge associative tags with each new @psi concept.
    sub NewConcept() { # http://mind.sourceforge.net/newcept.html
    print " NewConcept calls EnVocab for nen $nen \n"; # 2016jan18
    $psi[$t] = $nen; # 2016jan17: Assign the newest concept number.
    EnVocab(); # 2015jun01: To create an English vocabulary node.
    EnParser(); # 2015jun04: In preparation to call InStantiate().
    $nen++; # 2016jan18: Increment for each new English word/concept.
    } # http://code.google.com/p/mindforth/wiki/NewConcept


    sub AudMem() { # http://code.google.com/p/mindforth/wiki/AudMem
    $pho = $char; # 2016jan12: $char comes from FileInput() getc
    print " AudMem: Storing ", $pho, " at time = ", "$t"; #2016jan16
    $audpsi = $nen; # 2015jun01: Store new words with new concept numbers.
    $aud[$t] = "$pho, $act, $audpsi"; # 2015may18: neuronal flag-panel
    if ($pho ne " ") { # 2016jan16: If a $char is being stored...
    $coda++ ; # 2016jan16: Increment word-coda with each stored $char.
    } # 2016jan16: End of test for a non-empty $char
    $coda = ($coda - 1); # 2016jan16: lower #coda by one unit.
    print " nen $nen audpsi $audpsi coda $coda \n"; # 2016jan18
    } # http://mind.sourceforge.net/audstm.html


    sub AudInput() { #2015apr25
    print "Enter a word of input, then press RETURN: "; # 2015may02
    $msg = <STDIN>; # 2015may01: PERL by Example (2015), p. 50
    print "AudInput: You entered the word: $msg"; # 2015may02
    NewConcept(); # 2015may28: Create a concept in the @psi array.
    AudMem(); # 2015jun01: Calling memory-insertion subroutine
    } # http://mind.sourceforge.net/audition.html


    sub FileInput() { #2016jan10
    my $_ = " "; # 2016jan10: PERL by Example (2015), p. 301
    print "PerlAI will open and read input.txt file \n"; # 2016jan10
    open (my $fh, "<", "input.txt") or die "Can't open: $!\n"; #2016jan14
    while (defined($char=getc $fh)) { # 2016jan12 P_B_Book p. 569
    AudBuffer(); #2016jan15: to manipulate inflections in OutBuffer()
    if ($char eq " ") { # 2016jan18: i.e., at the end of a word
    $onset = $audnew; # 2016jan18 set only at start of each word
    # $lftell = $.; # 2016jan18: assign line-count value
    # print "\n FileInput lftell = $lftell \n"; # 2016jan18
    print "\n FileInput for audnew $audnew calling NewConcept \n"; # 2016jan18
    NewConcept(); # 2016jan17 test
    print "\n"; #2016jan18: new-line for diagnostic clarity
    }; # 2016jan13 Stop at end of word.
    $rv = $t; # 2016jan16: Capture auditory onset as recall-vector.
    $coda = $t; # 2016jan16: base-value before AudMem() increments
    if ($char ne " ") { # 2016jan14: Avoid storing blank characters.
    if ($audnew == 0) {$audnew = $t}; # 2016jan17 Set only once.
    print "FileInput calls AudMem for char $char audnew $audnew\n"; # 2016jan18
    AudMem(); # 2016jan12: Calling memory-insertion subroutine
    $coda++ ; # 2016jan16: Increment word-coda with each stored $char.
    my @panel=split(',',$aud[$t]); # 2016jan18 PbEx p. 115
    if ($panel[0] ne " ") { # 2016jan18
    print " FInput: aud=$panel[0]$panel[1],$panel[2]\n"} # 2016jan18
    } # 2016jan14: End of test for a non-blank character.
    # else { print " PROBLEM \n "} # 2016jan18
    $t++; # 2016jan16: Increment time "t" here instead of AudMem()
    } # 2016jan12 P_B_Book p. 569
    close ($fh); #2016jan10: P_by_Ex p. 301
    # open (my $fh, "<", "input.txt"); # 2016jan18: PbE p. 302
    # while (my $line = <$fh>) { # 2016jan18: PbE p. 302
    # print "$line ZZZZZZZZZZZZZ \n"; # 2016jan18: PbE p. 302
    # print "FileInput(): line counter is at $. \n"; #2016jan18
    # } # 2016jan18: PbE p. 302
    if ($audpsi > 0) { # 2016jan17: would be from AudRecog()
    OldConcept() } # 2016jan17:
    else { # 2016jan18: Expanding for diagnostic message.
    print "FileInput-else calling NewConcept"; # 2016jan18
    NewConcept()
    } ; # 2016jan17: as in MindForth AudInput module.
    } # http://mind.sourceforge.net/audition.html


    sub sensorium() { # http://mind.sourceforge.net/perl.html
    # AudInput(); #2015apr25: Calling the sense of audition.
    FileInput(); #2016jan10: Inputting from file, not keyboard.
    # VisRecog(); #2015apr26: Opportunity to add vision.
    } # http://mind.sourceforge.net/sesorium.html


    EnBoot: { # 2015apr24 Bootstrap prior to learning new words.
    $t = 0; # 2015apr26: bootstrap stretches over time "$t".
    print "English bootstrap is loading... \n"; #2015apr24
    $t = 0; $aud[$t] = " "; # 2015may13: blank to detect "beg(ining)".
    $t = 1; $aud[$t] = "E, $act, $audpsi"; # 2015may13: flag-panel
    $t = 2; $aud[$t] = "R, $act, $audpsi"; # 2015may13: flag-panel
    $t = 3; $aud[$t] = "R, $act, $audpsi"; # 2015may13: flag-panel
    $t = 4; $aud[$t] = "O, $act, $audpsi"; # 2015may13: flag-panel
    $t = 5; $aud[$t] = "R, $act, $audpsi"; # 2015may13: flag-panel
    $t = 6; $aud[$t] = " "; # 2015may18: blank to signify end of word.
    $t = 7; # 2015may13: Time for storage of next word.
    $audpsi++; # 2015may28: increment for any new concept.
    $nen = 900; # 2016jan17: Value based on MindForth AI.
    }; # http://mind.sourceforge.net/enboot.html


    # 2015may28 Requirements of Speech mind-module:
    # Speech module shall output a single word starting at given @aud point.
    # Outputted word shall be fed internally into the ReEntry module.
    # Inflected nouns and verbs shall be spoken from NounGen or VerbGen.
    sub speech() {
    my $detour;
    $t2s = $audrv; # Start from the auditory recall-vector $audrv
    print "\npho act audpsi spoken from auditory memory. \n"; # columns
    do { # PERL by Example (2015), p. 194: do/until Loops
    @main::engram = (); # 2015may15
    @main::engram = $aud[$t2s]; # 2015may15
    $detour = reverse @main::engram;
    print chop($detour); # 2015may15
    $t2s++ ; # Increment time-to-speech to advance thru memory.
    if ($aud[$t2s] eq " ") { # 2015may13: No longer using Forth "ctu".
    print "\nSpeech: blank space found at $t2s\n"; # 2015may15
    $audrv = $t2s; # 2015may13: Prepare for next word in memory.
    return; # 2015may13: Exit at end of a word
    }; # 2015may13: End of if-clause
    } until $t2s eq $cns; # Show the whole array of AI Mind memory.
    } # http://mind.sourceforge.net/speech.html


    sub think() { # Start showing output as if generated by thinking.
    print "\nThink: Calling speech() \n"; # Display new-line before "Think: "
    speech(); # Calling the speech() module as if to say a thought
    } # http://mind.sourceforge.net/think.html


    while ($t < $cns) { # 2015may02: Until CNS memory is full.
    $age = $age + 1; # 2015apr26
    print "\nMain loop cycle ", $age, " \n"; # 2015may02
    sensorium(); # PERL by Example p. 350: () empty parameter list
    think(); # PERL by Example p. 350: () empty parameter list
    if ($age eq 999) { die "Perlmind dies when time = $t \n" }; # 2015may02
    if ($t > 30) { # 2015may10: Wait until near the time limit of 32.
    print "\nMainLoop shows contents of AI memory \n"; # 2016jan16
    # print "Now showing psi-concept, English and auditory arrays \n"; # 2015jun01
    print "time psi English pho act audpsi \n"; # 2016jan16
    do { # 2015may10: Show @aud array line-by-line.
    my @lex=split(',',$en[$krt]); # 2016jan17 PbE
    print "t=$krt. psi=$psi[$krt], "; # 2016jan17
    print "en= $lex[0],$lex[1],$lex[2],$lex[3],$lex[4],$lex[5],$lex[6] "; #2016jan17
    my @panel=split(',',$aud[$krt]); # 2016jan17 PbEx p. 115
    if ($panel[0] ne " ") {
    print "aud=$panel[0]$panel[1],$panel[2]\n"} # 2016jan17
    else {print " NO ENGRAM \n"}; # 2016jan17 PbEx 185: if/else construct
    $krt++; # increment $krt
    } while ($krt < 31); # show @aud array at all time-points
    }; # 2015may10: outer braces of if-clause
    } # http://code.google.com/p/mindforth/wiki/MainLoop

    our $birth = localtime($^T); #2015apr26: PERL Black Book p. 444
    print "End of mind.pl AI running since $birth\n"; #2016jan10
    print "The AI requires an input.txt file in the same directory.\n";
    print "input.txt Line 1: boys play games Line 2: john is boy\n";
    # 2015apr26: Claim your bragging rights for oldest living AI Mind.

    # http://perl6.org main resource for Strong AI Perl6 Killer App.
    # http://irclog.perlgeek.de/perl6/2016-01-18 Internet Relay Chat
    # http://ai.neocities.org/perlmind.txt rename as mind0037.pl etc.
    # http://ai.neocities.org/P6AI_man.html -- Perl6 AI User Manual
    # http://ai.neocities.org/PMPJ.html -- Perl Mind Programming Journal
    # http://www.cpan.org/authors/id/M/ME/MENTIFEX/mind.txt
    # http://www.nlg-wiki.org/systems/Mind.Forth
    # http://www.nasa.gov/planetarydefense

    # 2015apr23 First upload of code while learning Perl.
    # 2015apr24 sensorium() gets input; think() shows input.
    # 2015apr24 think() shows engrams fetched from @aud array.
    # 2015apr25 Saving mind0011.pl as stable; coding mind0012.pl
    # 2015apr25 AudMem() stores AudInput() data in @aud array.
    # 2015apr26 EnBoot: uses time $t instead of recall-vector $rv.
    # 2015apr26 TabulaRasa: will both fill and reserve CNS memory.
    # 2015may01 AudMem() stores and Think() displays whole words.
    # 2015may02 Prototype Perlmind dies when CNS memory fills up.
    # 2015may10 @aud array stores words with associative flag-panel.
    # 2015may13 speech() module shows engrams until any blank space.
    # 2015may15 speech() displays single words retrieved from @aud.
    # 2015may16 Requirements for speech() module prepended as comments.
    # 2015may17 Perl "split" function separates retrieval of aud-panel.
    # 2015may18 Using if/else construct to print @aud array contents.
    # 2015may28 Introducing rudimentary NewConcept called from AudInput.
    # 2015jun01 EnVocab module stubbed in for control of English vocabulary.
    # 2015jun04 EnParser() English Parser module stubbed in.
    # 2016jan10 FileInput() replaces AudInput() to read input.txt file.
    # 2016jan11 WHILE loop in FileInput() calls NewConcept() and AudMem().
    # 2016jan12 FileInput() uses getc to send single characters to AudMem(0).
    # 2016jan13 uses "return" to exit from FileInput() at end of first word.
    # https://groups.google.com/forum/#!topic/comp.programming/_F4PTlvJy_8
    # 2016jan14 FileInput() calls AudMem() to store multiple input.txt words.
    # https://groups.google.com/forum/#!topic/comp.sys.super/XcaLu5p1_gU
    # 2016jan15 stubs in AudBuffer(); resets module sequence as in MindForth.
    # https://groups.google.com/forum/#!topic/comp.lang.forth/nzEsANtlBGk
    # 2016jan18 EnVocab() and AudMem() use flag-panels simpler than MindForth.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)