Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Build dynamic JSON-like data structure

Status
Not open for further replies.

Kirsle

Programmer
Jan 21, 2006
1,179
US
Hi,

I'm porting a Perl module for a chatterbot reply scripting language over to C++. The chatterbot scripting language consists of text files containing commands and data that is parsed by the module and turned into usable data in-memory so that the chatterbot app can receive messages from a user and return an appropriate response.

Here is a snippit of what the script source files look like:

Code:
// this is a comment
// the default "topic" is random

+ hello bot // this is what the human says
- Hello, human! // this is how the bot replies
- Hello mortal. // there can be multiple replies, they're chosen at random

+ hello robot
@ hello bot // this redirects so when the user says "hello robot" it's as if they just said "hello bot"

> topic my_topic
  // this is a "topic" and its replies are isolated from
  // the default "random" topic
  + test input
  - test reply
< topic

The data structure that the Perl module comes up with after parsing this is:

Code:
$topics = {
   "random" => {
      "hello bot" => {
         "replies" => [ // this is always an array
           "Hello, human!",
           "Hello mortal."
         ]
      },
      "hello robot" => {
         "redirect" => "hello bot" // this is always a string
      }
   },
   "my_topic" => {
      "test input" => {
         "replies" => [
            "test reply"
         ]
       }
   }
};

The structure is basically,

topics["topic name"]["trigger text"]["replies"]->[0]
topics["topic name"]["trigger text"]["redirect"]
etc.

How do you implement a data structure like this in C++? I discovered map and multimap so far but these don't seem very flexible for having a multi-tiered data structure.

Kirsle.net | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Looks like it would be something like this:
Code:
map<string, map<string, map<string, string> > >
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top