Ejemplo n.º 1
0
def run_pipeline():
    templates = {
        'john': NNP(Male('John')),
        'paul': NNP(Male('Paul')),
        'george': NNP(Male('George')),
        'ringo': NNP(Male('Ringo')),
        'guitar': Noun('guitar'),
        'bass': Noun('bass guitar'),
        'drums': Noun('drum', features={
            NUMBER.plural,
        }),
        'Happy': Clause(NP(Var(0)), VP('be', AdjP('happy'))),
        'Play': Clause(NP(Var(0)), VP('play', NP(Var(1)))),
    }

    lex = Lexicaliser(templates=templates)
    # FIXME: embedded coordination doesn't work; flatten or fix in simplenlg?
    input_str = 'Play(john, guitar) & Play(paul, guitar) & ' \
                'Play(george, bass) & Play(ringo, drums)'
    sentence = lex(formula_to_rst(expr(input_str)))
    for e in sentence.elements():
        print(repr(e))

    output_str = realise(sentence)
    print(output_str)
Ejemplo n.º 2
0
def translate(formula, templates=None, simplifications=None):
    if isinstance(formula, str):
        formulas = [expr(f) for f in formula.split(';') if f.strip()]
    pipeline = Nlg()
    context = Context(ontology=None)
    context.templates = templates or {}
    doc = []
    for f in formulas:
        if simplifications:
            for s in filter(lambda x: x in simplification_ops, simplifications):
                f = simplification_ops[s](f)
        doc.append(formula_to_rst(f))

    translations = pipeline.process_nlg_doc2(doc, None, context)
    return translations
Ejemplo n.º 3
0
def translate(request):
    """Read a given FOL formula as jason data and return it as text. """
    response_data = {}
    formula = request.POST['formula'].strip()
    response_data['formula'] = formula
    simplifications = [
        x.strip() for x in request.POST.get('simplifications', '').split('|')
    ]
    try:
        realise = Realiser(host='roman.kutlak.info')
        if formula:
            template_instances = TemplateModel.objects.all()
            templates = {}
            errors = []
            for t in template_instances:
                name = t.name
                try:
                    template = eval(t.content)
                    templates[name] = template
                except Exception as e:
                    errors.append(
                        ('danger', str(e)))  # use bootstrap terminology...
            lex = Lexicaliser(templates=templates)
            response_data['text'] = realise(lex(formula_to_rst(expr(formula))))
            response_data['status'] = 'success'
            response_data['messages'] = json.dumps(errors)
        else:
            response_data[
                'text'] = "Enter a formula first. E.g., 'happy(roman)'"
            response_data['status'] = 'default'
    except nltk.sem.logic.LogicalExpressionException as e:
        response_data['text'] = str(e)
        response_data['status'] = 'error'
    except Exception as e:
        logger.exception(e)
    return JsonResponse(response_data)
Ejemplo n.º 4
0
 def test_simple_predicate(self):
     p = expr('Happy(john)')
     spec = formula_to_rst(p)
     self.assertEqual(PredicateMsg('Happy', 'john'), spec)
Ejemplo n.º 5
0
 def test_simple_predicate(self):
     p = expr('Happy(john)')
     spec = formula_to_rst(p)
     self.assertEqual(PredicateMsg(Expr('Happy', ['john'])), spec)