Clock Crew

Community => Flash & Flash Accessories => Topic farted by: zl on May 24, 2013, 09:06:06 AM

Title: Programming a word web
Post by: zl on May 24, 2013, 09:06:06 AM
So for the game I'm working on, I want to have a web of related word concepts.

So if you click on "animals" you get "pig" and "cow" and "dog"

if you click on "pig" you get "slop" and "bacon" and "livestock"

if you click on "bacon" you get "food" and "fat" and "delicious"

and so on.  These are just related words, not increasingly specific ones.  It's meant to simulate a chain of thoughts.

I'm not aiming for this being comprehensive.  I imagine a vocabulary of maybe 200-500 words.  Not every word needs three connections - some could even be dead ends.

I'm programming in AS3, and trying to find a good structure for this.  I'd appreciate any thoughts!
Title: Programming a word web
Post by: Slash on May 24, 2013, 09:22:50 AM
A dictionary with lists as values and strings as keys should work I think.
The lists would contain strings with all the related words to the key.
Title: Programming a word web
Post by: TropicanaClock on May 24, 2013, 05:02:17 PM
Make some arrays :S
Title: Programming a word web
Post by: RomanClock on May 24, 2013, 06:34:05 PM
Luckily, AS3 supports Associative arrays. This lets you define a position in an array using a string:

var list:Array = new Array();
list["Animal"]=["Cow","Pig","Chicken"];
list["Pig"]=["Bacon","Farm"];
list["Cow"]=["Beef","Steak"];
list["Bacon"]=["Food","Fat"];
list["Beef"]=["Food","Burger"];
list["Food"]=["Animal","Plant"];

for (var s:String in list)
{
trace(s + " --> " +list[s]);
}


Food --> Animal,Plant
Beef --> Food,Burger
Cow --> Beef,Steak
Bacon --> Food,Fat
Pig --> Bacon,Farm
Animal --> Cow,Pig,Chicken


Say you are using "Animal". At position "Animal" there is an array of strings associated with "Animal". So if you want to go to "Pig" you could get there with list[list["Animal"][1]]; which resolves to list["Pig"];
Title: Programming a word web
Post by: AstronautClock on May 24, 2013, 07:51:24 PM
I'm glad roman is back when I see this post
Title: Programming a word web
Post by: zl on May 24, 2013, 08:02:30 PM
Quote from: RomanClock;1948889Luckily, AS3 supports Associative arrays. This lets you define a position in an array using a string:

Say you are using "Animal". At position "Animal" there is an array of strings associated with "Animal". So if you want to go to "Pig" you could get there with list[list["Animal"][1]]; which resolves to list["Pig"];

This is awesome! I like the recursive structure of your example.

If I wanted these words to carry some extra information though - like "pig" could be offensive, or "unicorn" is fanciful, or "champagne" is a word you don't know yet - I imagine making a custom Word object with these variables.

Could I define array positions with my custom object?
like:
list[new Word("unicorn",["fanciful","childlike","unlearned"])] = [new Word("horse"),new Word("magic",["fanciful","childlike"])];
edit: but the recursive calls couldn't work with objects being generated in the lists =\
Title: Programming a word web
Post by: RomanClock on May 24, 2013, 09:01:33 PM
You can modify the previous code to create generic objects:

var list:Array = new Array();
list["Animal"]={links:["Cow","Pig","Chicken"],info:"General"};
list["Pig"]={links:["Bacon","Farm"],slang:"Offensive"};
list["Cow"]={links:["Beef","Steak"],sounds:"Says moo"};
list["Bacon"]={links:["Food","Fat"],sounds:"Sizzles"};
list["Beef"]={links:["Food","Burger"]};
list["Food"]={links:["Animal","Plant"]};

for (var s:String in list)
{
trace(s);
for (var q:* in list[s])
{
trace("\t"+q+": "+list[s][q]);
}
}


Pig
slang: Offensive
links: Bacon,Farm
Beef
links: Food,Burger
Food
links: Animal,Plant
Bacon
links: Food,Fat
sounds: Sizzles
Cow
links: Beef,Steak
sounds: Says moo
Animal
info: General
links: Cow,Pig,Chicken


Thus, if you want to traverse to a new object you would do list[list["Animal"].links[0]]; which resolves to list["Pig"];
Title: Programming a word web
Post by: zl on May 24, 2013, 10:42:18 PM
Quote from: RomanClock;1948904You can modify the previous code to create generic objects:

Thus, if you want to traverse to a new object you would do list[list["Animal"].links[0]]; which resolves to list["Pig"];

I could kiss you :O
Title: Programming a word web
Post by: RomanClock on May 24, 2013, 11:51:12 PM
Quote from: Zombie Lincoln;1948916I could kiss you :O

<3
Just spreading the knowledge sir!
Title: Programming a word web
Post by: AbbaZabaClock on May 25, 2013, 12:16:53 AM
Quote from: RomanClock;1948929<3
Just spreading the knowledge sir!

Damn you guys come back AND help people out.


:golfclap: