89 lines
1.9 KiB
Python
89 lines
1.9 KiB
Python
|
import nlu
|
||
|
|
||
|
examples = [
|
||
|
{
|
||
|
"text": "icecream is cold",
|
||
|
"parsed": ("exists-property-with-value", 'icecream', 'cold'),
|
||
|
},
|
||
|
{
|
||
|
"text": "lava is dangerous",
|
||
|
"parsed": ("exists-property-with-value", 'lava', 'dangerous')
|
||
|
},
|
||
|
{
|
||
|
"text": "earth is a planet",
|
||
|
"parsed": ("pertenence-to-group", 'earth', 'planet'),
|
||
|
},
|
||
|
{
|
||
|
"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')
|
||
|
}
|
||
|
]
|
||
|
|
||
|
base_knowledge = {
|
||
|
'icecream': {
|
||
|
"groups": ['noun', 'object', 'comestible', 'sweet'],
|
||
|
},
|
||
|
'lava': {
|
||
|
"groups": ['noun', 'object'],
|
||
|
},
|
||
|
'earth': {
|
||
|
"groups": ['noun', 'object', 'planet'],
|
||
|
},
|
||
|
'green': {
|
||
|
"groups": ['noun', 'color', 'concept'],
|
||
|
},
|
||
|
'plane': {
|
||
|
"groups": ['noun', 'object', 'vehicle', 'fast'],
|
||
|
},
|
||
|
'car': {
|
||
|
"groups": ['noun', 'object', 'vehicle', 'slow-ish'],
|
||
|
},
|
||
|
'wale': {
|
||
|
"groups": ['noun', 'object', 'living-being']
|
||
|
},
|
||
|
'cold': {
|
||
|
"groups": ['property', 'temperature'],
|
||
|
},
|
||
|
'dangerous': {
|
||
|
"groups": ['property'],
|
||
|
},
|
||
|
'planet': {
|
||
|
"groups": ['noun', 'group'],
|
||
|
},
|
||
|
'color': {
|
||
|
"groups": ['property', 'group'],
|
||
|
},
|
||
|
'fly': {
|
||
|
"groups": ['verb'],
|
||
|
},
|
||
|
'swim': {
|
||
|
"groups": ['verb'],
|
||
|
},
|
||
|
}
|
||
|
|
||
|
|
||
|
def main():
|
||
|
knowledge = nlu.KnowledgeBase(
|
||
|
examples=[],
|
||
|
trained=[],
|
||
|
knowledge=base_knowledge
|
||
|
)
|
||
|
|
||
|
knowledge = nlu.train(knowledge, examples)
|
||
|
for test in [{'text': 'a bus can run'}, {'text': 'io is a moon'}]:
|
||
|
row = test['text'].lower().split()
|
||
|
fit = nlu.get_fit(knowledge, row)
|
||
|
print(test['text'], fit)
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
main()
|