Gk: tavamõtlemise mehhaniseerimise proge koos näidetega
Arendusversioon tavamõtlemise järeldamismootorist gk koos väikeste näidetega. Gk töötab ainult linuxis.
gk23nov.gz tee gunzip ja chmod +x gk23nov
Co-occurrence maatriksitega katsetamiseks
- Lae alla nende wikipediast põhjal ehitatud gk_relatedwords.txt ja gk_similarity_wiki.txt. Taustaks: need failid on ehitatud nende vektorite baasil, mis on omakorda ehitatud nendest andmefailidest: a compacted pure-text version of full wikipedia ja A lemmatized version of wikipedia texts.
- muuda faili gk_similarity_wiki.txt nimi selliseks: gk_similarity.txt
- kopeeri endale fail dummy.js
- Lase gk23nov niimoodi käima (see võtab minu laptopil ca 5 sekundit ja ehitab mällu 3 gigase andmebaasi): ./gk3nov dummy.js -readkb -similarities -relatedwords -mbsize 3000
- Katseta ja muuda 'paths,...' sõnu: ./gk23nov -usekb -task 'paths,king,queen'
Järeldusmootoriga katsetamiseks eelmainitud faile peale gk23nov executable vaja ei ole. Kopeeri endale järgmisi faile ja katseta nendega nii (failinimi asemel pane tegelik failinimi.): ./gk23nov failinimi
Süntaksist arusaamiseks uuri ja loe siit näiteid ja ehk siit lisaks baasmehhanismidest arusaamiseks (logictools.org näidetes ja mehhanismides ei ole tõenäosusi, erandeid, sarnasusi jne, mis on olemas gk23nov mehhanismides).
Lihtne info kumuleerimine:
[ { "@name":"a1", "@confidence": 0.5, "@role":"axiom", "@logic":["bird","a"] }, { "@name":"a2", "@confidence": 0.6, "@role":"axiom", "@logic":["bird","a"] }, { "@name":"q", "@role": "question", "@logic": ["bird","a"] } ]
Esimene sotsiaalsete mõjude näide:
[ {"@confidence": 0.3, "@logic": ["smokes","?:X"]}, {"@confidence": 0.1, "@logic": ["friends","?:X","?:Y"]}, {"@confidence": 0.9, "@logic": [["friends","?:X","?:Y"],"=>",["friends","?:Y","?:X"]]}, {"@confidence": 0.6, "@logic": ["susceptible","?:X"]}, {"@confidence": 0.2, "@logic": [[["smokes","?:Y"],"&",["friends","?:X","?:Y"],"&",["susceptible","?:X"]],"=>",["smokes","?:X"]]}, {"@confidence": 1.0, "@logic": ["friends","chris","sam"]}, {"@confidence": 1.0, "@logic": ["smokes","chris"]}, {"@question": ["smokes","sam"]} ]
Teine sotsiaalsete mõjude näide:
[ {"@confidence": 0.8, "@logic": ["stress","ann"]}, {"@confidence": 0.4, "@logic": ["stress","bob"]}, {"@confidence": 0.6, "@logic": ["influences","ann","bob"]}, {"@confidence": 0.2, "@logic": ["influences","bob","carl"]}, {"@confidence": 1.0, "@logic": [["stress","?:X"],"=>",["smokes","?:X"]]}, {"@confidence": 1.0, "@logic": [[["smokes","?:Y"],"&",["influences","?:Y","?:X"]],"=>",["smokes","?:X"]]}, {"@question": ["smokes","carl"]} ]
Läheduse näide:
// a chain of objects near each other [ {"@logic":["near","c1","c2"]}, {"@logic":["near","c2","c3"]}, {"@logic":["near","c3","c4"]}, {"@logic":["near","c4","c5"]}, {"@logic":["near","c5","c6"]}, {"@logic":["near","c6","c7"]}, {"@logic":["near","c7","c8"]}, {"@logic":["near","c8","c9"]}, {"@logic":["near","c9","c10"]}, // add the following additional information // for seeing the effects of cumulating information // {"@logic":["near","c1","c5"], "@confidence": 50}, // {"@logic":["near","c5","c10"], "@confidence": 50}, {"@logic":[["-near","?:X","?:Y"],["-near","?:Y","?:Z"],["near","?:X","?:Z"]], "@confidence": 90}, {"@question": ["near","c1","c10"]} ]
Pingviini näide vaikimisi reeglitega: taustaks tasub vaadata Default logic
[ {"@logic": ["bird","b"]}, {"@logic": ["penguin","p"]}, {"@logic": ["ostrich","ostr"]}, {"@logic": ["object","o"]}, {"@logic": [["-ostrich","?:X"],["bird","?:X"]]}, {"@logic": [["-penguin","?:X"],["bird","?:X"]]}, {"@logic": [["bird","?:X"],"=>",["object","?:X"]]}, {"@logic": [["penguin","?:X"],"=>",["-flies","?:X"]]}, {"@logic": [["ostrich","?:X"],"=>",["-flies","?:X"]]}, {"@logic": [["-bird","?:X"],["flies","?:X"], ["$block", 3, ["$not", ["flies", "?:X"]]]]}, {"@logic": [["-object","?:X"],["-flies","?:X"], ["$block", 2, ["flies", "?:X"]]]}, {"@question": ["-flies","?:X"]} // who does not fly? // {"@question": ["flies","?:X"]} // who does fly? ]
Klassifitseerimise näide: siin on nii tõenäosused kui vaikimisi reeglid.
[ // the task is to classify three objects: h1 (likely human), b1 (likely bird), a1 (likely airplane) // // the question at the end asks to find objects which are likely to be organisms, along with the // confidence that they are organisms // here we give observations about parts our three objects have {"@logic": ["has_part","h1","leg"]}, {"@logic": ["has_part","b1","leg"]}, {"@logic": ["has_part","b1","wing"]}, {"@logic": ["has_part","b1","feathers"]}, {"@logic": ["has_part","a1","engine"]}, {"@logic": ["has_part","a1","wing"]}, // here we give default rules with confidences about classifying as animals, birds, airplanes // the $block literal says that the application of this rule is not allowed, if the second argument of // the $block predicate can be proved // as an example, the first rule says that if something has a leg, then with the 50% confidence it is an animal, unless we can prove // that it is not an animal {"@logic": [["has_part","?:X","leg"],"=>",[["isa","?:X","animal"],"|",["$block", ["$","animal"], ["$not", ["isa","?:X","animal"]]]]], "@confidence":50}, {"@logic": [["has_part","?:X","leg"],"=>",[["isa","?:X","bird"],"|",["$block", ["$","bird"], ["$not", ["isa","?:X","bird"]]]]], "@confidence":50}, {"@logic": [["has_part","?:X","wing"],"=>",[["isa","?:X","bird"],"|",["$block", ["$","bird"], ["$not", ["isa","?:X","bird"]]]]], "@confidence":60}, {"@logic": [["has_part","?:X","feathers"],"=>",[["isa","?:X","bird"],"|",["$block", ["$","animal"], ["$not", ["isa","?:X","animal"]]]]], "@confidence":80}, {"@logic": [["has_part","?:X","wing"],"=>",[["isa","?:X","airplane"],"|",["$block", ["$","airplane"], ["$not", ["isa","?:X","airplane"]]]]], "@confidence":40}, // a strict rule saying that if something has an engine as a part, then this thing is not an organism {"@logic": [["has_part","?:X","engine"],"=>",["-isa","?:X","organism"]]}, // a taxonomy: animals and birds are organisms, airplanes are not organisms {"@logic": [["isa","?:X","animal"],"=>",["isa","?:X","organism"]]}, {"@logic": [["isa","?:X","bird"],"=>",["isa","?:X","organism"]]}, {"@logic": [["isa","?:X","airplane"],"=>",["-isa","?:X","organism"]]}, // a question to find objects which are organisms, along with the confidence: {"@question": ["isa","?:X","organism"]} ]
Sarnasuse näide: selle jooksutamiseks kopeeri kõigepelt järgmine jupp failiks nimega gk_similarity.txt:
queen,12,king,0.9,male$female king,12,queen,0.9,male$female penguin,12,ostrich,0.9, ostrich,12,penguin,0.9, emu,12,ostrich,0.9, ostrich,12,emu,0.9, flies,12,inair,0.8, inair,12,flies,0.8,
ja siis salvesta järgmine näide mingi nimega failinimi ja jooksuta masinat niimoodi -similarities võtmega: ./gk23nov failinimi -similarities
[ {"@logic": ["isa","queen","q1"]}, {"@logic": ["isa","queen","q2"]}, {"@logic": ["prop","male","q2"]}, {"@logic": ["isa","king","k1"]}, {"@logic": [["isa", "queen","?:X"], "=>", ["prop","female","?:X"]]}, {"@logic": [["prop","female","?:X"], "=>", ["-prop","male","?:X"]]}, {"@logic": [["isa","king","?:X"], "=>", ["prop","rich","?:X"]]}, {"@logic": [["isa","king","?:X"], "=>", ["prop","male","?:X"]]}, {"@logic": [["isa","king","?:X"], "=>", ["-prop","poor","?:X"]]}, //{"@logic": [["-queen","?:X"],["king","?:X"]], "@confidence":0.9}, //{"@logic": [["-king","?:X"],["male","?:X"]]}, {"@question": ["prop","?:X","q1"]} ]
Olukordade ja sündmuste jada näide:
// Story: // "A man entered a room containing a table. He wore a black suite. Then the man left the room". // We have three situations: s1, s2, s3: // s1: nothing initially known // s2: man has entered the room // s3: man has exited the room // New situations are generated by events. // We generate new unique identifiers room1, sit1, table1, man1, sit2, sit3 etc // while encoding the story to logic. // The encoder has to understand that the room and the man is the same in all // situations and thus use the same identifiers man1 and room1 in all situations. // * Event 1: A man entered a room containing a table. [ // initial status generated by the first sentence {"@logic":["isa","room1","room"]}, {"@logic":["isa","table1","table"]}, {"@logic":["in","table1","room1","sit1"]}, {"@logic":["isa","event1","event"]}, {"@logic":["event_type","event1","enter"]}, {"@logic":["event_actor","event1","man1"]}, {"@logic":["event_target","event1","room1"]}, {"@logic":["isa","man1","man"]}, {"@logic":["situationfrom","event1","sit1"]}, // a default "unlikely anything is inside" rule for the initial situation {"@logic":[["-in","?:X","?:Y","sit1"], ["$block",["$","in"],["in","?:X","?:Y","sit1"]]], "@confidence": 90}, // * Situation 2: generated by the entering event. {"@logic":["situationto","event1","sit2"]}, {"@logic":["nextsituation","sit1","sit2"]}, //{"@logic":["in","man1","room1","sit2"]}, // this is generated by the entering rule {"@logic":["in",["clothes","man1","sit1"],"room1","sit2"]}, {"@logic":["in",["carriedstuff","man1","sit1"],"room1","sit2"]}, /* // we use a generic rule instead {"@logic":[["in","?:X","?:Y","sit1"], "=>", [["in","?:X","?:Y","sit2"],"|",["$block",["$","in"],["$not",["in","?:X","?:Y","sit2"]]]]]}, {"@logic":[["-in","?:X","?:Y","sit1"], "=>", [["-in","?:X","?:Y","sit2"],"|",["$block",["$","in"],["in","?:X","?:Y","sit2"]]]]}, */ // * Situation 2 statement: He wore a black suit. {"@logic":["color",["clothes","man1","sit2"],"#:black","sit2"]}, // #:black says that #:black is not equal to #:red, #:dog, #:man1 etc: anything different {"@logic":["clothingtype",["clothes","man1","sit2"],"suite","sit2"]}, // no question examples for clothingtype: making questions is an exercise :) // * Event 2: Then the man left the room. {"@logic":["isa","event2","event"]}, {"@logic":["event_type","event2","leave"]}, {"@logic":["event_actor","event2","man1"]}, {"@logic":["event_target","event2","room1"]}, {"@logic":["situationfrom","event2","sit2"]}, {"@logic":["situationto","event2","sit3"]}, {"@logic":["nextsituation","sit2","sit3"]}, /* // we use a generic rule instead {"@logic":[["in","?:X","?:Y","sit2"], "=>", [["in","?:X","?:Y","sit3"],"|",["$block",["$","in"],["$not",["in","?:X","?:Y","sit3"]]]]]}, {"@logic":[["-in","?:X","?:Y","sit2"], "=>", [["-in","?:X","?:Y","sit3"],"|",["$block",["$","in"],["in","?:X","?:Y","sit3"]]]]}, */ // rules for entering and exiting {"@logic":[["&",["isa","?:E","event"],["event_type","?:E","enter"],["event_actor","?:E","?:A"],["event_target","?:E","?:T"], ["situationto","?:E","?:S"]], "=>", ["in","?:A","?:T","?:S"]]}, {"@logic":[["&",["isa","?:E","event"],["event_type","?:E","leave"],["event_actor","?:E","?:A"],["event_target","?:E","?:T"], ["situationto","?:E","?:S"]], "=>", ["-in","?:A","?:T","?:S"]]}, // rule for default persistance of being inside something {"@logic":[[["in","?:X","?:Y","?:S1"],"&",["nextsituation","?:S1","?:S2"]], "=>", [["in","?:X","?:Y","?:S2"],"|",["$block",["$","in"],["$not",["in","?:X","?:Y","?:S2"]]]]]}, // rules for default persistance of clothes {"@logic":[["&",[["clothes","?:X","?:S1"],"=","?:C1"],["nextsituation","?:S1","?:S2"]], "=>", [["=",["clothes","?:X","?:S2"],"?:C1"],"|",["$block",["$","color"],["$not",["=",["clothes","?:X","?:S2"],"?:C1"]]]]] }, // rules for default persistance of color {"@logic":[["&",["color","?:X","?:C","?:S1"],["nextsituation","?:S1","?:S2"]], "=>", [["color","?:X","?:C","?:S2"],"|",["$block",["$","color"],["$not",["color","?:X","?:C","?:S2"]]]]] }, // rules for uniqueness of color //{"@logic": ["-=","black","red"]}, // we do not need this if we use #:black and #:red {"@logic": [[["color","?:X","?:C","?:S"],"&",["!=","?:C","?:C1"]], "=>", ["-color","?:X","?:C1","?:S"]]}, // useless rules generating infinite amounts of stuff to avoid simple termination /* {"@logic":[["-isa","?:X","?:Y"],["isa",["foo","?:X"],"?Y"]]}, {"@logic":[["-isa",["foo","?:X"],"?:Y"],["isa","?:X","?Y"]]}, */ // default rule for persistance of not being inside {"@logic":[[["-in","?:X","?:Y","?:S1"],"&",["nextsituation","?:S1","?:S2"]], "=>", [["-in","?:X","?:Y","?:S2"],"|",["$block",["$","in"],["in","?:X","?:Y","?:S2"]]]]}, // some questions to try //{"@question": ["isa","?:X","man"]} //{"@question": ["in","table1","room1","sit3"]} //{"@question": ["in","chair1","room1","sit3"]} // {"@question": ["in","man1","room1","sit2"]} //{"@question": ["-in","man1","room1","sit1"]} //{"@question": ["-in","man1","room1","sit2"]} {"@question": ["in","man1","room1","?:S"]} // in which situations is there a man in the room? //{"@question": ["-in","man1","room1","?:S"]} //{"@question": ["=",["clothes","man1","sit2"],["clothes","man1","sit3"]]} //{"@question": ["color",["clothes","man1","sit3"],"#:red","sit3"]} ]