miraculixx / pyrules

Python Rules Engine
MIT License
22 stars 9 forks source link

Add natural language support #10

Open miraculixx opened 9 years ago

miraculixx commented 9 years ago

Implement rules.NaturalLanguageRule

Expected behaviour:

  1. Parse a text string that has a rule in it in natural language (similar to a TableRuleset, but without the structure given in a dictionary)
  2. Create a TableRuleset

    Sample code to parse text

# draft parser for natural language rule
import re
rule_text = re.split('If(.*)Then(.*)', 'If something and something else Then context.target = context.fare * 2, context.target2 = context.fare2 * 5')
conditions = rule_text[1]
actions = re.split('(.*=.*)[,]+', rule_text[2])
print "split rule", rule_text
print "conditions" , conditions
print "actions", actions
for action in actions[1:]:
    split_action = re.split('context\.(.*)=(.*)', action)
    print "target", split_action

outputs this:

split rule ['', ' something and something else ', ' context.target = context.fare * 2, context.target2 = context.fare2 * 5', '']
conditions  something and something else 
actions ['', ' context.target = context.fare * 2', ' context.target2 = context.fare2 * 5']
target [' ', 'target ', ' context.fare * 2', '']
target [' ', 'target2 ', ' context.fare2 * 5', '']