Esempio n. 1
0
def answer(sentence):
    index = sentence.index('when')+2
    text =  nltk.word_tokenize(sentence)
    #print('Question',text)
    val = nltk.pos_tag(text)
    #print('Tokens',val)
    #print(val)
    info = Response.getNextSoloTerm(val[index:],['VBN','NN'])
    if info=="":
        #print("HERE WE GO")
        return randAnswer()
    subject = info[0]
    index+=info[1]
    #print('subject',subject)
    if subject=='arnold':
        info = Response.getNextSoloTerm(val[index:],['NN'])
        if info=="":
            return randAnswer()
             #randNum = random.randint(0,len(randomAnswers)-1)
             #return(randomAnswers[randNum])
        attribute = info[0]
        index+=info[1]
        #print('ATTRIBUTE',attribute)
        form = ''
        answer = Response.buildSentence([subject,'when was',attribute,form])
        if answer=="":
            return randAnswer()
            #randNum = random.randint(0,len(randomAnswers)-1)
            #return(randomAnswers[randNum])
        return(answer)
    elif subject=='movie':
         form = ''
         #print('Form',form)
         info = Response.getMovieTitle(val[index:],['VBN','VBD'])
         if info=="":
             return randAnswer()
         title = info[0]
         index = index + info[1]
         #print('Title:',title)
         info = Response.getNextTerm(val[index:],['VBN','VBD'])
         if info=="":
             return randAnswer()
         attribute = info[0]
         index = index + info[1]
         #print('NEWcounter',counter)
         #print('Attribute:',attribute)
         answer = Response.buildSentence([subject,'when was',title,attribute,form])
         if answer=="":
             return randAnswer()
         return answer
    else:
        return randAnswer()
Esempio n. 2
0
def answer(sent):
    #print(sent)
    sent = sent.lower()
    locTerms = ['city','country''province']
    dateTerms = ['month','year','day','date']
    curiousTerms = ['is','was']
    if re.match('^(what).*$',sent):
        tokes= nltk.pos_tag(nltk.word_tokenize(sent))
        if tokes[1][0] in dateTerms:
            #print('we\'re in the date object')
            counter = 2
            info = Response.getNextSoloTerm(tokes[counter:],['VBN','NN'])
            if info=="":
                return randAnswer()
            subject = info[0]
            counter = counter+info[1]
            #print('Subject:',subject)
            if subject =='movie':
                form = tokes[1][0]
                #print('Form',form)
                info = Response.getMovieTitle(tokes[counter:],['VBN','VBD'])
                if info=="":
                    return randAnswer()
                title = info[0]
                counter = counter + info[1]
                #print('Title:',title)
                info = Response.getNextTerm(tokes[counter:],['VBN','VBD'])
                if info=="":
                    return randAnswer()
                attribute = info[0]
                counter = counter + info[1]
                #print('NEWcounter',counter)
                #print('Attribute:',attribute)
                answer = Response.buildSentence([subject,'when was',title,attribute,form])
                if answer=="":
                    return randAnswer()
                return answer
            elif subject=='arnold':
                form = tokes[1][0]
                #print('Form:',form)
                info = Response.getNextTerm(tokes[counter:],['NN','NNP'])
                if info=="":
                    return randAnswer()
                attribute = info[0]
                counter = counter + info[1]
                #print('Attribute:',attribute)
                #print('OBJECT:\nSubject: {}\nQuestType: when\nAttribute: {}\nForm: {}'.format(subject,attribute,form))
                answer = Response.buildSentence([subject,'when was',attribute,form])
                if answer=="":
                     return randAnswer()
                return answer
        elif tokes[1][0] in locTerms:
            #print('we\'re in the location object')
            counter = 2
            info = Response.getNextSoloTerm(tokes[counter:],['VBN','NN'])
            if info=="":
                return randAnswer()
            subject = info[0]
            counter = counter+info[1]
            #print('Subject:',subject)
            if subject=='arnold':
                form = tokes[1][0]
                #print('Form:',form)
                info = Response.getNextTerm(tokes[counter:],['NN','NNP'])
                if info=="":
                    return randAnswer()
                attribute = info[0]
                counter = counter + info[1]
                #print('Attribute:',attribute)
                return Response.buildSentence([subject,'where was',attribute,form])
        elif tokes[1][0] in curiousTerms:
            return randAnswer()
            #randNum = random.randint(0,len(randomAnswers))
            #return(randomAnswers[randNum])
        else:
            return randAnswer()