Advertisement

XML

Started by June 18, 2005 11:01 AM
3 comments, last by Nice Coder 19 years, 7 months ago
Hi everybody. In the last couple of days I've been trying to read an XML file, and randomly select a sentence to ouput depending on what the user inputs... In other words, the user enters "Hi" into a textbox, and from my XML file it outputs the sentence in the default "Hi" category. I have tried my best at giving it a go, and have been going through the AIML (http://www.alicebot.org/aiml.html) information but I'm totally stuck. Could someone point me in the right direction? Thanks a lot, - Kurt
Sounds like you need to parse the Xml file into some 'dictionary' lookup in memory and respond according to that.

Imagine your Xml entries look like this:

<response key="hi">Hello, this is some text</response><response key="hi">y helo thar</response><response key="hello">Hey there</response>


You will probably want to store the words in a structure:-

struct WordNode{  std::string wordKey;  std::vector< std::string > responses;  void addResponse( std::string &a_text ) { responses.push_back( a_text ); }  std::string randomResponse() { // Take a random number and go to that vector pos   }};


And these would be stored in a map:

std::map< std::string, WordNode >   wordResponses;


Every time you encounter a word in the Xml file, you should look to see if one exists in the map - if so add another response to the node, if it doesn't exist create a new node and add it to the map.

Whenever the user enters a word you should look to see if it exists in the map, if so pick a random response from it, if not then report an error. For best results, you'd probably want to store your keywords in lower/upper case and convert the user input in the same way.

Anyway, just an idea and good luck!
Advertisement
Thanks for the reply.

I forgot to mention that I'm writing this in C#, can what you said be done in it too?

- Kurt

C# in .Net has excellent XML functionality. Just look up XmlDocument or XmlTextreader in MSDN... You will find some examples of reading nodes...

Edo
Edo
Quote:
Original post by KurtLives
Hi everybody.

In the last couple of days I've been trying to read an XML file, and randomly select a sentence to ouput depending on what the user inputs...

In other words, the user enters "Hi" into a textbox, and from my XML file it outputs the sentence in the default "Hi" category.

I have tried my best at giving it a go, and have been going through the AIML (http://www.alicebot.org/aiml.html) information but I'm totally stuck. Could someone point me in the right direction?

Thanks a lot,

- Kurt


you should look at what we are doing in the alpha project.... (its a chatbot, made to beat the current best.). (Contact me at Amadscientist10@hotmail.com

In another point, what your doing isn't the best of options. (you will never get a full text match on anything other then "hi").

Try keyword matching.

And xml isn't the best format for this...

From,
Nice coder
Click here to patch the mozilla IDN exploit, or click Here then type in Network.enableidn and set its value to false. Restart the browser for the patches to work.

This topic is closed to new replies.

Advertisement