6 Replies - 154 Views - Last Post: 12 July 2020 - 01:46 PM

#1 ToiToiToi   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 11-July 20

Arificial Intelligence

Posted 11 July 2020 - 05:17 PM

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;

struct elemento {
    char valor[100];
    struct elemento *prox;
    struct elemento *ant;
    struct elemento *down;
    struct elemento *up;
    struct elemento *backtostart;

};
typedef struct elemento ELEM;
ELEM *head = NULL;

int oldornew=0; //will determine if it is a new word or a old word (0 stands for old, 1 stands for new)
int wordexists=0; // will determine if a word does not have to be registered again (0 stands for old, 1 stands for new)

void Speak(ELEM* start, char valor[100]);
void AddWord(ELEM* start, char valor[100]);
void DivideWordsforAdd(ELEM* start, char valor[100]);
void DivideWordsforSpeak(ELEM* start, char valor[100]);
void Hear(ELEM* start, char valor[100]);
void CheckifExist (ELEM* start, char valor[100]);

void Speak(ELEM* start, char valor[100]){
    int newword=0;
    ELEM *elem = start->prox;
    while(elem->prox!=NULL){
        elem=elem->prox; //go to the last element
    }
    while(elem){
        int written=1;
        for(int i=0;i<100;i++){ //check if the word was written
            if(elem->valor[i]==valor[i] || valor[i]==0){
                if(valor[i]==' ' || valor[i]==0){
                    break;
                }
            }else{
                written=0;
            }
        }
        if(written==1){
            if (elem->down==NULL){
                newword=1; //do a check for a new word
                break;
            }
        }
        elem=elem->ant;
    }
    if(newword==0){
        elem=start->prox;
        while(elem->prox!=NULL){
            elem=elem->prox; //go to the last element
        }
    }
    while (elem){
        if (newword==1){ //new word
            int num=0;
            while(elem->valor[num]!=' '){
                num++;
            }
            if (elem->down==NULL){
                cout<<"Me: What is ";
                for(int i=0;i<num;i++){
                    cout << elem->valor[i];
                }
                cout << " ?" << endl;
                oldornew=1;
                break;
            }
        }else{ //if there is no new word
            int match=1;
            for(int i=0;i<100;i++){   //check if its the right word to output
                if (elem->valor[i]==valor[i] || valor[i]==0){
                    if(valor[i]==0 || valor[i]==' '){
                        break;
                    }
                }else{ //if it is a different value
                    match=0;
                }
            }
            if (match==1){
                for (int i=0;i<100;i++){
                    if(elem->valor[i]!=' '){
                        cout << elem->valor[i];
                    }else{
                        cout<<": "<<endl;
                        break;
                    }
                }
                elem=elem->down;
                while (elem->prox!=NULL){ //goto last element
                    elem=elem->prox;
                }
                while (elem){
                    for(int i=0;i<100;i++){
                        if(elem->valor[i]!=' ' || (elem->valor[i]==' ' && elem->valor[i+1]!=' ')){ //this makes sure the whole sentence gets printed
                            cout<<elem->valor[i];
                        }
                    }
                    if (elem->ant==NULL){
                        break;
                    }
                    elem=elem->ant;
                    cout<<endl;
                }
                cout<<endl<<endl;
                oldornew=0;
            }
        }
        if (elem->up){
            elem=elem->up;
        }
        elem = elem->ant;
    }
    cout<<endl;
    if (oldornew==1){
        Hear(elem,valor);
    }
}

void AddWord(ELEM* start, char valor[100]){
    ELEM *elem = (ELEM*)malloc(sizeof(ELEM));
    if (oldornew==1){ //if it is a sub word
        if (valor[0]==0){ //check if first value is [enter]
            oldornew=0;
            for(int i=0;i<100;i++){
                if(valor[i]==0){
                    valor[i]=' ';
                }
            }
            Hear(start->backtostart,valor); //Call Hear with 'start' element in parameters
        }
        for(int i=0;i<100;i++){
            if(valor[i]==0){
                valor[i]=' ';
            }
        }
        for(int i=0;i<100;i++){
            elem->valor[i]=' ';
        }
        for(int i=0;i<100;i++){
            if (valor[i]==' '&& valor[i-1]==' '){ //transfer every word until two spaces found (if bug appears, might remove && valor[i-1]==' ')
                break;
            }
            elem->valor[i] = valor[i];
        }
        elem->down=NULL;
        elem->ant=NULL;
        elem->up=start;
        if (start->down==NULL){
            elem->prox=NULL;
        }else{
            elem->prox=start->down;
            elem->prox->ant=elem;
        }
        start->down=elem;
        Hear(start,valor);
    }else{ //if is is a main word
        for(int i=0;i<100;i++){
            if(valor[i]==0){
                valor[i]=' ';
            }
        }
        for(int i=0;i<100;i++){
            elem->valor[i]=' ';
        }
        for(int i=0;i<100;i++){
            if (valor[i]==' ' && valor[i-1]==' '){//stop transferring the word at space key (if bug appears, might change ' ' to 0... or not)
                break;
            }
            elem->valor[i] = valor[i];
        }
        elem->down=NULL;
        elem->up=NULL;
        elem->ant=NULL;
        if (start->prox!=NULL){
            elem->prox=start->prox;
            elem->prox->ant=elem;
        }else{
            elem->prox=NULL;
        }
        elem->backtostart=start;
        start->prox=elem;
    }
}

void CheckifExist(ELEM* start, char valor[100]){
        int same=0;
        int checkfornew=1;
        ELEM* elem=start->prox;
        while(elem){
            for(int i=0;i<100;i++){ //check if word exists
                if(elem->valor[i]==valor[i] || valor[i]==0){
                    same++;
                    if (valor[i]==0 || valor[i]==' '){
                        checkfornew=0;
                        break;
                    }
                }else{
                    break;
                }
            }
            elem=elem->prox;
        }
        if (checkfornew==1){ //return to add new word to database
            wordexists=1;
            return;
        }else{
            wordexists=0;
        }
}

void DivideWordsforSpeak(ELEM* start,char valor[100]){
    char word[100];
    int k=0;
    for(int i=0;i<100;i++){
        word[k]=valor[i];
        if (valor[i]==' '){
            if (valor[i-1]!=' '){
                Speak(start,word);
                for(int j=0;j<i;j++){ //reset char array (word)
                    word[j]=' ';
                }
            }
        k=-1;
        }
    k++;
    }
    Hear(start,valor);
}

void DivideWordsforAdd(ELEM* start, char valor[100]){
    if (oldornew==1){
        AddWord(start,valor);
    }else{
        char word[100];
        int k=0;
        for(int i=0;i<100;i++){
            word[k]=valor[i];
            if (valor[i]==' '){
                if (valor[i-1]!=' '){
                    CheckifExist(start,word);
                    if (wordexists==1){
                        AddWord(start,word); //send word including the last space key
                    }
                    for(int j=0;j<i;j++){ //reset char array (word)
                        word[j]=' ';
                    }
                }
            k=-1;
            }
        k++;
        }
        DivideWordsforSpeak(start,valor);
    }
}

void Hear(ELEM* start, char valor[100]){
    cout<<"You: ";
    char x[100];
    for(int i=0;i<100;i++){
        x[i]=' ';
    }
    cin.getline(x,100);
    cout << endl;
    DivideWordsforAdd(start,x);
}

int main()
{
    head = (ELEM*)malloc(sizeof(ELEM));
    head->valor[0] = 's';
    head->valor[1] = 't';
    head->valor[2] = 'a';
    head->valor[3] = 'r';
    head->valor[4] = 't';
    head->prox=NULL;
    head->ant=NULL;
    head->down=NULL;
    head->up=NULL;
    head->backtostart=NULL;
    cout << "Hello! I am AI to talk." << endl << endl << "You: ";
    char x[100];
    for(int i=0;i<100;i++){
        x[i]=' ';
    }
    cin.getline(x,100);
    cout << endl;
    DivideWordsforAdd(head,x);
    return 0;
}
:code:

Make sure to just press enter after describing a word to return to main loop!

This post has been edited by modi123_1: 11 July 2020 - 06:46 PM
Reason for edit:: In the future please use the [code] tag button in the editor


Is This A Good Question/Topic? 0
  • +

Replies To: Arificial Intelligence

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15804
  • View blog
  • Posts: 63,308
  • Joined: 12-June 08

Re: Arificial Intelligence

Posted 11 July 2020 - 05:45 PM

What is this? What does it do? Is there a question?

Adding code tags and moving to 'share your project'.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7462
  • View blog
  • Posts: 25,113
  • Joined: 05-May 12

Re: Arificial Intelligence

Posted 12 July 2020 - 08:31 AM

Is this code supposed to be C or C++? Currently it looks like it was C code that was poorly transliterated into C++. For example there's all the uses of malloc().

And there's this C idiomatic code:
struct elemento {
    char valor[100];
    struct elemento *prox;
    struct elemento *ant;
    struct elemento *down;
    struct elemento *up;
    struct elemento *backtostart;

};
typedef struct elemento ELEM;


which is simply this in C++:
struct ELEM {
    char valor[100];
    ELEM *prox;
    ELEM *ant;
    ELEM *down;
    ELEM *up;
    ELEM *backtostart;
};


Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7462
  • View blog
  • Posts: 25,113
  • Joined: 05-May 12

Re: Arificial Intelligence

Posted 12 July 2020 - 09:09 AM

Yikes, and that code is going to run out of stack space because it recursively calls itself to get a looping effect. Stack overflow, here we come!
Was This Post Helpful? 0
  • +
  • -

#5 Radius Nightly   User is offline

  • D.I.C Regular

Reputation: 64
  • View blog
  • Posts: 391
  • Joined: 07-May 15

Re: Arificial Intelligence

Posted 12 July 2020 - 09:44 AM

Anyway, can someone explain to me what AI are actually?

Because most of the time i see two different things, and i dont consider it as AI.

1. Peoples who make many options, and variables, considering that complex code as AI.
In that way, we humans consider "random" number as "random", because we dont know how its calculated, and for us its magically randomly picked number based on pure magic, but if we know the whole detailed calculation behind "random", then its a not a random, as we know how its been calculated. For example, if we toss a coin, its 50:50 chance to get head or tail, we consider it random, but if we calculate density of the air, gravity, weight, angles, power from finger, friction, distance, etc., we will know what the outcome will be, means its not random. As much options as we can put in, as much complex as it can be, its not AI. For example, dog toy as a robot, who can do 30 different things, giving us option to rate his each of that things, so he can do more or less of this 30 things based on our rating what we like him to do and what we hate are called AI. Thats not AI.

2. Making application using RAM to do various things with inputs, in order to get better score as we specify. That can be AI, thats how computer can learn how to drive a car, how to play Super Mario, doing it more and more, from generation to generation, failing many times, die many times, over time, it will become a professional one. I think its more of a exploring and exploiting, doing single thing, multiple times, trying to get best score each time, as we humans do, playing same game for years, to become professional, and since we humans can fail even then for various reasons, computer cant fail as much as humans can. And thats practice taking time, to become better and better each time, each hour, each match, each day, each generation. AI here make sense, but instead of making fast decisions, computer will decide to do anything (eg. while driving a car, it will crash multiple times deliberately, just to experience the crash, instead of "thinking" after the first wall touch, "i should not touch the wall because i lose points, instead of gaining points"), just to try it, not looking forward, and that is not "intelligent" but rather "experiment" where AI see whats the outcome, without thinking or calculating what can happen, even it has information what will happen. So peoples usually do many generations, over many cores, lots of random calculations, and AI ending up doing "random" things, and its not perfect, because 1 of a 1000 or more AI will eventually "survive" by luck. This kind of AI are only good for data analysis with big numbers and tests, it can basically predict future by statistics.

What i consider AI are code that can create his own variables and data, expanding its knowledge, learning, while at the same time been able to use that and make connections between all new things it created. I mean, kind of a new born baby, an empty brain, like fresh format drive, that only got main functions, like firmware, or reptilian brain, that can create other functions and learn whats good and what bad on other feedback, as main code for sensors, like human senses, eyes, hearing, etc., so baby will move arms and legs, and the main code can make a new code to use that part of a body, to get aware of it, to learn how that kind of a hardware works. Later on, main code, eg. baby, will develop multiple functions for various things, merging them together, always learning (by all senses) and becoming better, over time creating complex functions and connections, like real brain, making history, tracking experience, creating memory, creating personality, identity, likes, dislikes, favorites, hobbies, etc., that depends on environment where baby grow and spend times (so it can be normal, moral, human, or crazy cold blooded murderer), combining all of that together in the end, while starting point are single main function. In computer world, that would be artificial intelligence, not some unknown previously made stuff, not some random experiments with 99.99% failures.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7462
  • View blog
  • Posts: 25,113
  • Joined: 05-May 12

Re: Arificial Intelligence

Posted 12 July 2020 - 10:15 AM

In the old days, natural language processing was considered a sub field of the much broader field of artificial intelligence. The chat bot that the OP was trying to present above is a very primitive attempt at natural language processing. I think that implementing ELIZA would have been much more interesting.
Was This Post Helpful? 0
  • +
  • -

#7 Radius Nightly   User is offline

  • D.I.C Regular

Reputation: 64
  • View blog
  • Posts: 391
  • Joined: 07-May 15

Re: Arificial Intelligence

Posted 12 July 2020 - 01:46 PM

View PostSkydiver, on 12 July 2020 - 06:15 PM, said:

ELIZA would have been much more interesting.

My attempt. :huh:
Posted Image
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1