Ejemplo n.º 1
0
    def testSection11(self):
        lexicon = Lexicon.getDefaultLexicon()
        nlgFactory = NLGFactory( lexicon )
        realiser = Realiser( lexicon )

        pA = nlgFactory.createClause( "Mary", "chase", "the monkey" )
        pA.addComplement( "in the park" )
        outputA = realiser.realiseSentence( pA )
        self.assertEqual( "Mary chases the monkey in the park.", outputA )

        # alternative build paradigm
        place = nlgFactory.createNounPhrase( "park" )
        pB = nlgFactory.createClause( "Mary", "chase", "the monkey" )

        # next line is depreciated ~ may be corrected in the API
        place.setDeterminer( "the" )
        pp = nlgFactory.createPrepositionPhrase()
        pp.addComplement( place )
        pp.setPreposition( "in" )
        pB.addComplement( pp )
        outputB = realiser.realiseSentence( pB )
        self.assertEqual( "Mary chases the monkey in the park.", outputB )
        place.addPreModifier( "leafy" )
        outputC = realiser.realiseSentence( pB )
        self.assertEqual( "Mary chases the monkey in the leafy park.", outputC )
Ejemplo n.º 2
0
    def testSection10(self):
        lexicon = Lexicon.getDefaultLexicon()      # default simplenlg lexicon
        nlgFactory = NLGFactory( lexicon )  # factory based on lexicon
        realiser = Realiser( lexicon )

        subject1 = nlgFactory.createNounPhrase( "Mary" )
        subject2 = nlgFactory.createNounPhrase( "your", "giraffe" )

        # next line is not correct ~ should be nlgFactory.createCoordinatedPhrase ~ may be corrected in the API
        subj = nlgFactory.createCoordinatedPhrase( subject1, subject2 )
        verb = nlgFactory.createVerbPhrase( "chase" )
        p = nlgFactory.createClause()
        p.setSubject( subj )
        p.setVerb( verb )
        p.setObject( "the monkey" )

        outputA = realiser.realiseSentence( p )
        self.assertEqual( "Mary and your giraffe chase the monkey.", outputA )
        object1 = nlgFactory.createNounPhrase( "the monkey" )
        object2 = nlgFactory.createNounPhrase( "George" )

        # next line is not correct ~ should be nlgFactory.createCoordinatedPhrase ~ may be corrected in the API
        obj = nlgFactory.createCoordinatedPhrase( object1, object2 )
        obj.addCoordinate( "Martha" )
        p.setObject( obj )
        outputB = realiser.realiseSentence( p )
        self.assertEqual( "Mary and your giraffe chase the monkey, George and Martha.", outputB )

        obj.setFeature( Feature.CONJUNCTION, "or" )
        outputC = realiser.realiseSentence( p )
        self.assertEqual( "Mary and your giraffe chase the monkey, George or Martha.", outputC )
Ejemplo n.º 3
0
 def testSection3(self):
     lexicon = Lexicon.getDefaultLexicon()
     nlgFactory = NLGFactory(lexicon)
     s1 = nlgFactory.createSentence("my dog is happy")
     r = Realiser(lexicon)
     output = r.realiseSentence(s1)
     self.assertEqual("My dog is happy.", output)
Ejemplo n.º 4
0
 def testSection5(self):
     lexicon = Lexicon.getDefaultLexicon()
     nlgFactory = NLGFactory(lexicon)
     realiser = Realiser(lexicon)
     p = nlgFactory.createClause()
     p.setSubject("my dog")
     p.setVerb("chase")
     p.setObject("George")
     output = realiser.realiseSentence(p)
     self.assertEqual("My dog chases George.", output)
Ejemplo n.º 5
0
 def testSection5A(self):
     lexicon = Lexicon.getDefaultLexicon()
     nlgFactory = NLGFactory( lexicon )
     realiser = Realiser( lexicon )
     p = nlgFactory.createClause()
     p.setSubject( "Mary" )
     p.setVerb( "chase" )
     p.setObject( "the monkey" )
     output = realiser.realiseSentence( p )
     self.assertEqual( "Mary chases the monkey.", output )
Ejemplo n.º 6
0
    def __init__(self):
        verb2noun, noun2verb, verb2actor, actor2verb = utils.noun_verb(
            prop.morph_verb)
        self.verb2noun = verb2noun
        self.verb2actor = verb2actor

        sub2word = utils.subgraph_word(prop.verbalization)
        self.sub2word = sub2word

        lexicon = Lexicon.getDefaultLexicon()
        self.nlgFactory = NLGFactory(lexicon)
        self.realiser = Realiser(lexicon)
Ejemplo n.º 7
0
    def testSection7(self):
        lexicon = Lexicon.getDefaultLexicon()
        nlgFactory = NLGFactory( lexicon )
        realiser = Realiser( lexicon )

        p = nlgFactory.createClause()
        p.setSubject( "Mary" )
        p.setVerb( "chase" )
        p.setObject( "the monkey" )
        p.addComplement( "very quickly" )
        p.addComplement( "despite her exhaustion" )

        output = realiser.realiseSentence( p )
        self.assertEqual( "Mary chases the monkey very quickly despite her exhaustion.", output )
Ejemplo n.º 8
0
def replace_plural_head(head):
    print(head.text)
    if (head.text == "'re"):
        return "'s"
    regex = re.compile('[a-z]+')
    lex = Lexicon.getDefaultLexicon()
    realiser = Realiser(lex)
    nlgFactory = NLGFactory(lex)
    p = nlgFactory.createClause()
    p.setVerb(head.text)
    p.setSubject("she")
    verb_match = regex.match(realiser.realiseSentence(p).split(" ")[1])
    if (verb_match):
        return verb_match.group()
    else:
        return head.text
Ejemplo n.º 9
0
    def testVariants(self):
        lexicon = Lexicon.getDefaultLexicon()
        nlgFactory = NLGFactory(lexicon)
        realiser = Realiser(lexicon)

        p = nlgFactory.createClause()
        p.setSubject("my dog")
        p.setVerb("is")  # variant of be
        p.setObject("George")

        output = realiser.realiseSentence(p)
        self.assertEqual("My dog is George.", output)

        p = nlgFactory.createClause()
        p.setSubject("my dog")
        p.setVerb("chases")  # variant of chase
        p.setObject("George")

        output = realiser.realiseSentence(p)
        self.assertEqual("My dog chases George.", output)

        p = nlgFactory.createClause()
        p.setSubject(nlgFactory.createNounPhrase("the", "dogs"))   # variant of "dog"
        p.setVerb("is")  # variant of be
        p.setObject("happy")  # variant of happy
        output = realiser.realiseSentence(p)
        self.assertEqual("The dog is happy.", output)

        p = nlgFactory.createClause()
        p.setSubject(nlgFactory.createNounPhrase("the", "children"))   # variant of "child"
        p.setVerb("is")  # variant of be
        p.setObject("happy")  # variant of happy
        output = realiser.realiseSentence(p)
        self.assertEqual("The child is happy.", output)

        # following functionality is enabled
        p = nlgFactory.createClause()
        p.setSubject(nlgFactory.createNounPhrase("the", "dogs"))   # variant of "dog"
        p.setVerb("is")  # variant of be
        p.setObject("happy")  # variant of happy
        output = realiser.realiseSentence(p)
        self.assertEqual("The dog is happy.", output) #corrected automatically
Ejemplo n.º 10
0
    def testSection6(self):
        lexicon = Lexicon.getDefaultLexicon()
        nlgFactory = NLGFactory(lexicon)
        realiser = Realiser(lexicon)

        p = nlgFactory.createClause()
        p.setSubject("Mary")
        p.setVerb("chase")
        p.setObject("George")

        p.setFeature(Feature.TENSE, Tense.PAST)
        output = realiser.realiseSentence(p)
        self.assertEqual("Mary chased George.", output)

        p.setFeature(Feature.TENSE, Tense.FUTURE)
        output = realiser.realiseSentence(p)
        self.assertEqual("Mary will chase George.", output)

        p.setFeature(Feature.NEGATED, True)
        output = realiser.realiseSentence(p)
        self.assertEqual("Mary will not chase George.", output)

        p = nlgFactory.createClause()
        p.setSubject("Mary")
        p.setVerb("chase")
        p.setObject("George")

        p.setFeature(Feature.INTERROGATIVE_TYPE, InterrogativeType.YES_NO)
        output = realiser.realiseSentence(p)
        self.assertEqual("Does Mary chase George?", output)

        p.setSubject("Mary")
        p.setVerb("chase")
        p.setFeature(Feature.INTERROGATIVE_TYPE, InterrogativeType.WHO_OBJECT)
        output = realiser.realiseSentence(p)
        self.assertEqual("Who does Mary chase?", output)

        p = nlgFactory.createClause()
        p.setSubject("the dog")
        p.setVerb("wake up")
        output = realiser.realiseSentence(p)
        self.assertEqual("The dog wakes up.", output)
Ejemplo n.º 11
0
    def testSection8(self):
        lexicon = Lexicon.getDefaultLexicon()
        nlgFactory = NLGFactory( lexicon )
        realiser = Realiser( lexicon )

        subject = nlgFactory.createNounPhrase( "Mary" )
        object = nlgFactory.createNounPhrase( "the monkey" )
        verb = nlgFactory.createVerbPhrase( "chase" )
        subject.addModifier( "fast" )

        p = nlgFactory.createClause()
        p.setSubject( subject )
        p.setVerb( verb )
        p.setObject( object )

        outputA = realiser.realiseSentence( p )
        self.assertEqual( "Fast Mary chases the monkey.", outputA )
        verb.addModifier( "quickly" )
        outputB = realiser.realiseSentence( p )
        self.assertEqual( "Fast Mary quickly chases the monkey.", outputB )
Ejemplo n.º 12
0
    def testSection14(self):
        lexicon = Lexicon.getDefaultLexicon()
        nlgFactory = NLGFactory( lexicon )
        realiser = Realiser( lexicon )

        p1 = nlgFactory.createClause( "Mary", "chase", "the monkey" )
        p2 = nlgFactory.createClause( "The monkey", "fight back" )
        p3 = nlgFactory.createClause( "Mary", "be", "nervous" )
        s1 = nlgFactory.createSentence( p1 )
        s2 = nlgFactory.createSentence( p2 )
        s3 = nlgFactory.createSentence( p3 )
        par1 = nlgFactory.createParagraph( [s1, s2, s3] )
        output14a = realiser.realise( par1 ).getRealisation()
        correct = "Mary chases the monkey. The monkey fights back. Mary is nervous.\n\n"
        self.assertEqual(correct, output14a )

        section = nlgFactory.createSection( "The Trials and Tribulation of Mary and the Monkey" )
        section.addComponent( par1 )
        output14b = realiser.realise( section ).getRealisation()
        correct = "The Trials and Tribulation of Mary and the Monkey\nMary chases the monkey. " + \
                  "The monkey fights back. Mary is nervous.\n\n"
        self.assertEqual(correct, output14b )
Ejemplo n.º 13
0
    def testSection13(self):
        lexicon = Lexicon.getDefaultLexicon()
        nlgFactory = NLGFactory( lexicon )
        realiser = Realiser( lexicon )

        s1 = nlgFactory.createClause( "my cat",   "like", "fish"  )
        s2 = nlgFactory.createClause( "my dog",  "like",  "big bones" )
        s3 = nlgFactory.createClause( "my horse", "like", "grass" )
        c = nlgFactory.createCoordinatedPhrase()
        c.addCoordinate( s1 )
        c.addCoordinate( s2 )
        c.addCoordinate( s3 )
        outputA = realiser.realiseSentence( c )
        correct = "My cat likes fish, my dog likes big bones and my horse likes grass."
        self.assertEqual(correct, outputA )

        p = nlgFactory.createClause( "I", "be",  "happy" )
        q = nlgFactory.createClause( "I", "eat", "fish" )
        q.setFeature( Feature.COMPLEMENTISER, "because" )
        q.setFeature( Feature.TENSE, Tense.PAST )
        p.addComplement( q )
        outputB = realiser.realiseSentence( p )
        self.assertEqual( "I am happy because I ate fish.", outputB )
Ejemplo n.º 14
0
 def __init__(self, process_proper_nouns=False):
     self.lexicon = Lexicon.getDefaultLexicon()
     self.realiser = Realiser(self.lexicon)
     self.process_proper_nouns = process_proper_nouns
Ejemplo n.º 15
0
from simplenlg import NLGFactory, Realiser
from simplenlg.lexicon import Lexicon
from Translation.Model.father import Father
from Translation.Model.sentece_plan import SentencePlan

lexicon = Lexicon.getDefaultLexicon()
nlg_factory = NLGFactory(lexicon)
realiser = Realiser(lexicon)


def parse_tree_to_sentence_plan(tree_list):
    for tree in tree_list:
        sentence_plan = sentence_plan_build(tree)
        use_simplenlg(sentence_plan)
        print("------------------")


def sentence_plan_build(tree):
    sentence_plan = SentencePlan()
    frontier = tree
    len_frontier = len(frontier)
    father = Father(tree.height(), tree.label())
    if len_frontier == 1:
        sentence_plan.subject.value = "It"

    while len_frontier > 0:
        subtree = frontier.pop(0)
        if subtree.label() in ["VP", "PP"]:
            father.set(subtree.height(), subtree.label())
        elif subtree.label() == "NP":
            if father.label == "PP" and subtree.height() < father.height:
Ejemplo n.º 16
0
 def setUp(self):
     self.lexicon = Lexicon.getDefaultLexicon()
     self.phraseFactory = NLGFactory(self.lexicon)
     self.realiser = Realiser(self.lexicon)
Ejemplo n.º 17
0
sys.path.append(os.path.join(os.path.dirname(__file__), "simplenlg.jar"))

# Bottle will handle the HTTP side of things
from bottle import route, run, request, response

# SimpleNLG will do the NLG generation
from simplenlg.framework import NLGFactory, CoordinatedPhraseElement, ListElement, PhraseElement
from simplenlg.lexicon import Lexicon
from simplenlg.realiser.english import Realiser
from simplenlg.features import Feature, Tense, NumberAgreement
from simplenlg.phrasespec import NPPhraseSpec

from java.lang import Boolean

# We only need one instance of these, so we'll create them globally.
lexicon = Lexicon.getDefaultLexicon()
nlgFactory = NLGFactory(lexicon)
realiser = Realiser(lexicon)

# Process the request to http://host:port/generateSentence
@route('/generateSentence', method="POST")
def process_generate_sentence_request():
    try:
        # Generate the sentence from the JSON payload.
        return realiser.realiseSentence(generate_sentence(request.json))
    except Exception, e:
        print e
        response.status = 400
        # If any exceptions are thrown, set status to 400, and return the error string
        return str(e)