예제 #1
0
def cfg_handler():
    rules = request.args.get('rules', '')
    base = request.args.get('base', '')
    results = []
    if len(rules):
        cfg_rules = cfg.read_rules(rules.split('\n'))
        results = [cfg.expand(base, cfg_rules) for i in xrange(10)]

    return render_template('cfg.html', rules=rules, base=base, results=results)
예제 #2
0
def index_handler():
    subject = request.args.get('subject', '')
    rules = {}
    article = ''
    tree = []
    if subject:
        rules = generate_rules(subject)
        rules['SUBJECT'] = [subject]
        merge(rules, base_rules)
        merge(rules, cfg.read_rules(open('rules/%s.rules' % subject)))

        article = cfg.expand('START', rules, tree)

    return render_template('index.html',
        subjects=subjects, subject=subject,
        article=article, rules=rules, tree=tree)
예제 #3
0
def generate_rules(subject):
    rules = {}
    words = collections.defaultdict(list)
    for line in open('data/%s.tags' % subject):
        word, tag = line.strip().rsplit('/', 1)
        if word not in word_blacklist:
            words[tag].append(word)
    for t in tag_whitelist:
        if len(words[t]) < 15:
            rules[t] = words[t]
        else:
            rules[t] = random.sample(words[t], 15)
    return rules

base_rules = cfg.read_rules(open('rules/base.rules'))

@app.route('/')
def index_handler():
    subject = request.args.get('subject', '')
    rules = {}
    article = ''
    tree = []
    if subject:
        rules = generate_rules(subject)
        rules['SUBJECT'] = [subject]
        merge(rules, base_rules)
        merge(rules, cfg.read_rules(open('rules/%s.rules' % subject)))

        article = cfg.expand('START', rules, tree)
예제 #4
0
#!/usr/bin/env python
import sys, json, cfg
print json.dumps(cfg.read_rules(sys.stdin))