2017-05-11 17:54:02 +00:00
|
|
|
import json
|
2017-05-17 21:54:14 +00:00
|
|
|
import logging
|
2017-05-11 17:54:02 +00:00
|
|
|
|
|
|
|
from knowledge_base import KnowledgeBase
|
2017-05-16 20:46:22 +00:00
|
|
|
from modifiable_property import ModifiableProperty
|
2017-05-10 23:05:07 +00:00
|
|
|
|
|
|
|
examples = [
|
|
|
|
{
|
|
|
|
"text": "icecream is cold",
|
|
|
|
"parsed": ("exists-property-with-value", 'icecream', 'cold'),
|
|
|
|
},
|
2017-05-11 19:13:27 +00:00
|
|
|
{
|
|
|
|
"text": "is icecream cold?",
|
|
|
|
"parsed": ("question", ("exists-property-with-value", 'icecream', 'cold'))
|
|
|
|
},
|
2017-05-15 14:51:39 +00:00
|
|
|
{
|
|
|
|
"text": "lava is dangerous",
|
|
|
|
"parsed": ("exists-property-with-value", 'lava', 'dangerous')
|
|
|
|
},
|
2017-05-16 22:27:23 +00:00
|
|
|
{
|
|
|
|
"text": "is lava dangerous?",
|
|
|
|
"parsed": ("question", ("exists-property-with-value", 'lava', 'dangerous')),
|
|
|
|
},
|
2017-05-16 20:46:22 +00:00
|
|
|
{
|
|
|
|
"text": "earth is a planet",
|
|
|
|
"parsed": ("pertenence-to-group", 'earth', 'planet'),
|
|
|
|
},
|
2017-05-16 22:27:23 +00:00
|
|
|
{
|
|
|
|
"text": "io is a moon",
|
|
|
|
"parsed": ("pertenence-to-group", 'io', 'moon'),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"text": "is earth a moon?",
|
|
|
|
"parsed": ("question", ("pertenence-to-group", 'earth', 'moon')),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"text": "Green is a color",
|
|
|
|
"parsed": ("pertenence-to-group", 'green', 'color'),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"text": "a plane can fly",
|
|
|
|
"parsed": ("has-capacity", 'plane', 'fly')
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"text": "a wale can swim",
|
|
|
|
"parsed": ("has-capacity", 'wale', 'swim')
|
|
|
|
},
|
2017-05-10 23:05:07 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
base_knowledge = {
|
|
|
|
'icecream': {
|
2017-05-11 17:54:02 +00:00
|
|
|
"groups": set(['noun', 'object', 'comestible', 'sweet']),
|
2017-05-10 23:05:07 +00:00
|
|
|
},
|
|
|
|
'lava': {
|
2017-05-11 17:54:02 +00:00
|
|
|
"groups": set(['noun', 'object']),
|
2017-05-10 23:05:07 +00:00
|
|
|
},
|
|
|
|
'earth': {
|
2017-05-11 17:54:02 +00:00
|
|
|
"groups": set(['noun', 'object', 'planet']),
|
2017-05-10 23:05:07 +00:00
|
|
|
},
|
2017-05-16 22:27:23 +00:00
|
|
|
'io': {
|
|
|
|
"groups": set(['noun', 'object']),
|
|
|
|
},
|
2017-05-10 23:05:07 +00:00
|
|
|
'green': {
|
2017-05-11 17:54:02 +00:00
|
|
|
"groups": set(['noun', 'color', 'concept']),
|
2017-05-10 23:05:07 +00:00
|
|
|
},
|
|
|
|
'plane': {
|
2017-05-11 17:54:02 +00:00
|
|
|
"groups": set(['noun', 'object', 'vehicle', 'fast']),
|
2017-05-10 23:05:07 +00:00
|
|
|
},
|
|
|
|
'car': {
|
2017-05-11 17:54:02 +00:00
|
|
|
"groups": set(['noun', 'object', 'vehicle', 'slow-ish']),
|
2017-05-10 23:05:07 +00:00
|
|
|
},
|
|
|
|
'wale': {
|
2017-05-11 17:54:02 +00:00
|
|
|
"groups": set(['noun', 'object', 'living-being']),
|
2017-05-10 23:05:07 +00:00
|
|
|
},
|
|
|
|
'cold': {
|
2017-05-11 17:54:02 +00:00
|
|
|
"groups": set(['property', 'temperature']),
|
|
|
|
"as_property": "temperature",
|
2017-05-10 23:05:07 +00:00
|
|
|
},
|
|
|
|
'dangerous': {
|
2017-05-11 17:54:02 +00:00
|
|
|
"groups": set(['property']),
|
|
|
|
"as_property": "safety",
|
2017-05-10 23:05:07 +00:00
|
|
|
},
|
|
|
|
'planet': {
|
2017-05-11 17:54:02 +00:00
|
|
|
"groups": set(['noun', 'group']),
|
2017-05-10 23:05:07 +00:00
|
|
|
},
|
2017-05-16 22:27:23 +00:00
|
|
|
'moon': {
|
|
|
|
"groups": set(['noun', 'group']),
|
|
|
|
},
|
2017-05-10 23:05:07 +00:00
|
|
|
'color': {
|
2017-05-11 17:54:02 +00:00
|
|
|
"groups": set(['property', 'group']),
|
2017-05-10 23:05:07 +00:00
|
|
|
},
|
|
|
|
'fly': {
|
2017-05-11 17:54:02 +00:00
|
|
|
"groups": set(['verb']),
|
2017-05-10 23:05:07 +00:00
|
|
|
},
|
|
|
|
'swim': {
|
2017-05-11 17:54:02 +00:00
|
|
|
"groups": set(['verb']),
|
2017-05-10 23:05:07 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-11 18:36:49 +00:00
|
|
|
def test_assumption(expectedResponse, knowledge, query):
|
2017-05-17 21:54:14 +00:00
|
|
|
logging.info("Query: {}".format(query['text']))
|
|
|
|
logging.info("Expected: {}".format(expectedResponse))
|
2017-05-11 18:36:49 +00:00
|
|
|
|
2017-05-11 19:13:27 +00:00
|
|
|
result, abstract_tree, diff = knowledge.process(query['text'])
|
2017-05-16 20:46:22 +00:00
|
|
|
end_result = result.getter() if isinstance(result, ModifiableProperty) else result
|
|
|
|
|
2017-05-17 21:54:14 +00:00
|
|
|
logging.info("\x1b[0;3{}mResult: {}\x1b[0m".format("1" if end_result != expectedResponse else "2", end_result))
|
2017-05-16 20:46:22 +00:00
|
|
|
assert(end_result == expectedResponse)
|
2017-05-11 18:36:49 +00:00
|
|
|
|
|
|
|
|
2017-05-10 23:05:07 +00:00
|
|
|
def main():
|
2017-05-11 17:54:02 +00:00
|
|
|
knowledge = KnowledgeBase(
|
|
|
|
knowledge=base_knowledge,
|
2017-05-10 23:05:07 +00:00
|
|
|
)
|
|
|
|
|
2017-05-11 17:54:02 +00:00
|
|
|
differences = knowledge.train(examples)
|
|
|
|
|
2017-05-17 21:54:14 +00:00
|
|
|
logging.info("----")
|
|
|
|
logging.info(differences())
|
|
|
|
logging.info("----")
|
2017-05-15 14:51:39 +00:00
|
|
|
|
2017-05-16 20:46:22 +00:00
|
|
|
test_assumption(True, knowledge, {'text': 'earth is a planet'})
|
2017-05-15 14:51:39 +00:00
|
|
|
test_assumption(True, knowledge, {'text': 'is lava dangerous?'})
|
2017-05-17 21:54:14 +00:00
|
|
|
for test in [{'text': 'a bus can run'}, {'text': 'io is a moon'}]:
|
|
|
|
row = test['text']
|
|
|
|
result, inferred_tree, differences = knowledge.process(row)
|
|
|
|
|
|
|
|
logging.info("result:", result)
|
|
|
|
logging.info(differences())
|
|
|
|
logging.info("---")
|
|
|
|
logging.info('-----')
|
|
|
|
logging.info(json.dumps(sorted(knowledge.knowledge.keys()), indent=4))
|
|
|
|
logging.info('-----')
|
|
|
|
|
2017-05-16 22:27:23 +00:00
|
|
|
queryTrue = {
|
|
|
|
"text": "is io a moon?",
|
|
|
|
"parsed": ("question", ("pertenence-to-group", "io", "moon"))
|
|
|
|
}
|
|
|
|
queryFalse = {
|
|
|
|
"text": "is io a planet?",
|
|
|
|
"parsed": ("question", ("pertenence-to-group", "io", "planet"))
|
|
|
|
}
|
2017-05-10 23:05:07 +00:00
|
|
|
|
2017-05-16 22:27:23 +00:00
|
|
|
test_assumption(False, knowledge, queryFalse)
|
|
|
|
test_assumption(True, knowledge, queryTrue)
|
2017-05-10 23:05:07 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|