示例#1
0
    def __init__(self, datasets_path, corpus_name, parse_type, lang='english'):
        self.datasets_path = datasets_path
        self.corpus_name = corpus_name
        self.corpus_path = path.join(datasets_path, corpus_name)
        self.docs_path = path.join(self.corpus_path, "docs")
        self.topics_file = path.join(self.corpus_path, "topics.xml")
        self.models_path = path.join(self.corpus_path, "models")
        self.smodels_path = path.join(self.corpus_path, "smodels")
        self.jar_path = path.join(PROJECT_PATH, "summarizer", "jars")
        os.environ['CLASSPATH'] = self.jar_path
        self.cleaned_path = path.join(datasets_path, "processed")

        if parse_type == 'parse':
            if lang == 'english':
                self.parser = stanford.StanfordParser(
                    model_path="%s/englishPCFG.ser.gz" % (self.jar_path))
            if lang == 'german':
                self.parser = stanford.StanfordParser(
                    model_path="%s/germanPCFG.ser.gz" % (self.jar_path))
                # self.cleaned_path = path.join(datasets_path, "processed.parse")
        if parse_type == 'props':  # TODO
            if lang == 'english':
                self.props_parser = ClausIE.get_instance()
            if lang == 'german':
                self.parser = stanford.StanfordParser(
                    model_path="%s/germanPCFG.ser.gz" % (self.jar_path))
示例#2
0
 def __init__(self):
     try:
         self.cl = ClausIE.get_instance(
             jar_filename=
             '/data/defacto/github/fever/clausie/clausie/clausie.jar')
     except Exception as error:
         raise error
示例#3
0
def main():
    sents = process_data_from_input_file()

    cl = ClausIE.get_instance()

    triples = cl.extract_triples(sents)

    for t in triples:
        r = process_relation_triplet(t)
        # print(r)

    # bob = add_person('Bob')
    # print(bob.likes,bob.has)
    # joe = add_person('Joe')
    # print(joe.likes,joe.has)
    # mary = add_person('Mary')
    # print(mary.likes,mary.has)
    # print(persons)
    # print(pets)
    # print(trips)
    question = ' '
    while question[-1] != '?':
        question = raw_input("Please enter your question: ")

        if question[-1] != '?':
            print('This is not a question... please try again')



    answer_question(question)
示例#4
0
    def enumerateSPtuples_Translate(self,
                                    keyword=None,
                                    maxTweet=99999,
                                    reader=dataReader()):
        reader = reader
        c = reader.cursor_tweet(keyword)
        tweetContent = {}
        SPtuples = []
        translator = googleTranslatror()
        cl = ClausIE.get_instance(jar_filename='./clausie/clausie.jar')
        for tweetIndex, item in enumerate(c):
            print tweetIndex + 1, ':'
            content = item['Content'].replace(u'\u200b', '')
            content = content.replace(u'\xa0', '')
            content = content.replace(u'\u5168\u6587', '')
            content = content.encode('utf-8')
            content = re.sub(r'#.*?#|@.*?\s|\[.*?\]|\s|【|】|全文|的秒拍视频|(|)|“|”',
                             '', content)
            print content
            content = translator.translate_zhCN2en(content)
            print content

            if tweetIndex + 1 == maxTweet:
                break
        return tweetContent, SPtuples
示例#5
0
def process_data_from_input(file_path):
    sents = get_data_from_file(file_path)
    cl = ClausIE.get_instance()
    triples = cl.extract_triples(sents)
    for t in triples:
        r = process_relation_triplet(t)
    print('triples processed')
示例#6
0
def process_data_from_input_file():
    sents = get_data_from_file()
    cl = ClausIE.get_instance()
    triples = cl.extract_triples(sents)
    for t in triples:
        # print(t)
        process_relation_triplet(t)
def main():
    sents = get_data_from_file()

    cl = ClausIE.get_instance()

    triples = cl.extract_triples(sents)

    for t in triples:
        r = process_relation_triplet(t)
        print(r)

    question = ' '
    while question[-1] != '?':
        question = raw_input("Please enter your question: ")

        if question[-1] != '?':
            print('This is not a question... please try again')

    q_trip = cl.extract_triples([preprocess_question(question)])[0]

    # (WHO, has, PET)
    # here's one just for dogs
    if q_trip.subject.lower() == 'who' and q_trip.object == 'dog':
        answer = '{} has a {} named {}.'

        for person in persons:
            pet = get_persons_pet(person.name)
            if pet and pet.type == 'dog':
                print(answer.format(person.name, 'dog', pet.name))
def process_data_from_input_file(path='assignment_01.data'):
    sents = get_data_from_file(path)

    cl = ClausIE.get_instance()

    triples = cl.extract_triples(sents)

    for t in triples:
        r = process_relation_triplet(t)
示例#9
0
def process_data_from_input_file(path='assignment_01.data'):
    sents = get_data_from_file(path)
    cl = ClausIE.get_instance()
    triples = cl.extract_triples(sents)
    try:
        process_relation_triplet(triples)
    except Exception as e:
        print(
            """There was an error when processing following triplet in the data file: {}\nSENT: {}\nERROR: {}\n\n"""
            .format(t, sents[int(t.index)], e))
def load_data():
    input_data = process_data_from_input_file()  # get input data
    cl = ClausIE.get_instance()  # setup the NLP thing
    triples = cl.extract_triples(
        input_data)  # turn the input data into some triples
    for t in triples:
        try:
            r = process_relation_triplet(t)
        except:
            pass
    return cl, triples
def main():
    sents = get_data_from_file()

    cl = ClausIE.get_instance()

    triples = cl.extract_triples(sents)

    for t in triples:
        r = process_relation_triplet(t)
        # print(r)
    print(trips)
    question = ' '
    while question[-1] != '?':
        question = raw_input("Please enter your question: ")

        if question[-1] != '?':
            print('This is not a question... please try again')
    answer_question(question)
def answer_questions(string):
    sents = get_data_from_file()
    cl = ClausIE.get_instance()
    triples = cl.extract_triples(sents)
    q_trip = cl.extract_triples([preprocess_question(string)])[0]
    answers = []
    print(q_trip)

    if q_trip.subject.lower() == 'who' and q_trip.object == 'dog':
        answer = '{} has a dog.'
        for person in persons:
            pet = get_persons_pet(person.name)
            if pet and pet.type == 'dog':
                answer = (answer.format(person.name))
                answers.append(answer)


    if q_trip.subject.lower() == 'who' and q_trip.object == 'cat':
        answer = '{} has a cat.'
        for person in persons:
            pet = get_persons_pet(person.name)
            if pet and pet.type == 'cat':
                answer = (answer.format(person.name))
                answers.append(answer)
def main():
    sents = get_data_from_file()

    cl = ClausIE.get_instance()

    triples = cl.extract_triples(sents)

    for t in triples:
        r = process_relation_triplet(t)
        print(r)


    #print(persons)

    #print(pets)





    question = ' '
    while question[-1] != '?':
        question = raw_input("Please enter your question: ")

        if question[-1] != '?':
            print('This is not a question... please try again')

    question = question.lower()
    q_trip = cl.extract_triples([preprocess_question(question)])[0]

    # person_name = q_trip.subject.name
    # print(person_name)

    # print(q_trip.subject.label=='PROPN')
    # (WHO, has, PET)
    # here's one just for dogs
    if q_trip.subject.lower() == 'who' and q_trip.object == 'dog':
        answer = '{} has a {} named {}.'

        for person in persons:
            pet = get_persons_pet(person.name)
            if pet and pet.type == 'dog':
                print(answer.format(person.name, 'dog', pet.name))

    # here's one for cats
    elif q_trip.subject.lower() == 'who' and q_trip.object == 'cat':
        answer = '{} has a {} named {}.'

        for person in persons:
            pet = get_persons_pet(person.name)
            if pet and pet.type == 'cat':
                print (answer.format(person.name, 'cat', pet.name))

    # for this Does John have a cat? question
    # we need to get the subject in the quesion and than, get_person_pet(person.name)==subject's name
    elif q_trip.subject.startswith('does') and q_trip.object == ('cat' or 'dog'):
        answer = '{}, {} does not have a {}.'
        new_subj_doc = q_trip.subject
        people = new_subj_doc[5:].capitalize()
        #new_subj_doc = nlp(unicode(q_trip.subject))
        #name_doc = new_subj_doc.char_span(0, len(q_trip.subject))
       # people = [token.text for token in name_doc if token.ent_type_ == 'PERSON']
        # print (people)


       # s_people = [token.text for token in subj_doc if token.ent_type_ == 'PERSON']

        # for person in persons:
        if people in persons:
            pet = get_persons_pet(people)
            if q_trip.object == pet:
                print(answer.format('Yes', people, q_trip.object))
            else:
                print(answer.format('No', people, q_trip.object))

        else:
            print (answer.format('No', people, q_trip.object))
def generate_triplet(file_path='./assignment_01.data'):
    sents = get_data_from_file(file_path)
    cl = ClausIE.get_instance()
    triples = [(sent, cl.extract_triples([sent])) for sent in sents]
    return triples
示例#15
0
def answer_question(question_string):
    answers = []
    cl = ClausIE.get_instance()
    try:
        q_trip = cl.extract_triples([preprocess_question(question_string)])[0]
        sentence = q_trip.subject + ' ' + q_trip.predicate + ' ' + q_trip.object
        doc = nlp(unicode(sentence))

        for t in doc:
            if t.pos_ == 'VERB' and t.head == t:
                root = t

        #print(q_trip)
        #print(root.lemma_)
        # (WHO, has, PET)
        if q_trip.subject.lower(
        ) == 'who' and q_trip.object == 'dog' or q_trip.object == 'cat':
            answer = '{} has a {} named {}.'

            for person in persons:
                pet = get_persons_pet(person.name)
                if q_trip.object == 'dog' and pet and pet.type == 'dog':
                    answers.append(answer.format(person.name, 'dog', pet.name))
                elif q_trip.object == 'cat' and pet and pet.type == 'cat':
                    answers.append(answer.format(person.name, 'cat', pet.name))
        for answer in answers:
            print(answer)

        # (Who, go, SOMEWHERE)
        if q_trip.subject.lower() == 'who' and (root.lemma_ == 'take'
                                                or root.lemma_ == 'fly'
                                                or root.lemma_ == 'go'
                                                or root.lemma_ == 'leave'):
            answer = '{} goes to {}.'
            for a in [e.text for e in doc.ents if e.label_ == 'GPE']:
                for person in persons:
                    for b in person.travels:
                        if b != None:
                            if b.departs_to == a:
                                answers.append(answer.format(person.name, a))
            if answers == []:
                print("No one is going to", a)
            else:
                for answer in answers:
                    print(answer)

        # (Does, PERSON, like, PERSON)
        if not has_question_word(question_string):
            temp = q_trip.subject.split(' ')
            flag = False
            for a in temp:
                if a in [e.text for e in doc.ents if e.label_ == 'PERSON']:
                    try:
                        select_person(a).likes
                    except AttributeError:
                        print(q_trip.object,
                              "is not a person in the database.")
                        break
                    if select_person(q_trip.object) in select_person(a).likes:
                        print("Yes.")
                        flag = True
                    if flag == False:
                        print("No.")

        # (When is <person> [going to|flying to|traveling to] <place>)
        if q_trip.object.lower().endswith('when'):
            flag = False
            a = select_person(q_trip.subject)
            if a != None:
                for b in a.travels:
                    for c in [e.text for e in doc.ents if e.label_ == 'GPE']:
                        if b != None:
                            if b.departs_to == c:
                                print(b.departs_on)
                                flag = True
                        else:
                            print(q_trip.subject, "is not going to anywhere.")
            else:
                print(q_trip.subject,
                      "is not in the database. Try another person name.")
                flag = True
            try:
                c
            except NameError:
                print("It is not even a place!!")
                flag = True
            if flag == False:
                print(q_trip.subject, "is not going to", c)

        # (Who likes <person>?)
        if q_trip.subject.lower() == 'who' and root.lemma_ == 'like':
            answer = '{} like(s) {}.'
            people = []
            temp = ''
            for person in persons:
                for like in person.likes:
                    if like.name == q_trip.object:
                        people.append(person)
            for a in people:
                temp += a.name
                temp += ', '
            temp = temp[:-2]
            if temp == '':
                print(answer.format('No one', q_trip.object))
            else:
                print(answer.format(temp, q_trip.object))

        # (Who does <person> like?)
        if q_trip.object.lower() == 'who':
            answer = '{} likes {}.'
            temp = ''
            try:
                for a in select_person(q_trip.subject).likes:
                    temp += a.name
                    temp += ', '
                temp = temp[:-2]
                print(answer.format(q_trip.subject, temp))
            except AttributeError:
                print(q_trip.subject, "is not a person in the database.")
    except ValueError:
        answer = 'The name of {}\'s {} is {}.'
        ques = question_string.split(' ')
        petType = ques[-1][:-1]
        personName = ques[-2][:-2]
        try:
            for a in select_person(personName).has:
                if a.type == petType:
                    print(answer.format(personName, petType, a.name))
        except AttributeError:
            print(personName, "is not a person in the database.")
def main():
    sents = get_data_from_file()

    cl = ClausIE.get_instance()

    triples = cl.extract_triples(sents)

    for t in triples:
        r = process_relation_triplet(t)
    #   print(r)

    #print(persons)

    #print(pets)

    question = ' '
    while question[-1] != '?':
        question = raw_input("Please enter your question: ")

        if question[-1] != '?':
            print('This is not a question... please try again')

    question = question.lower()
    q_trip = cl.extract_triples([preprocess_question(question)])[0]
    q_doc = nlp(unicode(preprocess_question(question)))

    # (WHO, has, PET)
    # here's one just for dogs

    if q_trip.subject.lower() == 'who' and q_trip.object == 'dog':
        answer = '{} has a {}.'

        for person in persons:
            pet = person.has
            if 'dog' in pet:
                print(answer.format(person.name, 'dog'))

    elif q_trip.subject.lower() == 'who' and q_trip.object == 'cat':
        answer = '{} has a {}.'

        for person in persons:
            pet = person.has
            if 'cat' in pet:
                print(answer.format(person.name, 'cat'))

    # here's one for cats
    elif q_trip.subject.lower() == 'who' and q_trip.object == 'cat':
        answer = '{} has a {}.'

        for person in persons:
            pet = get_persons_pet(person.name)
            if pet and pet.type == 'cat':
                print(answer.format(person.name, 'cat'))

    # for this Does John have a cat? question
    # we need to get the subject in the quesion and than, get_person_pet(person.name)==subject's name
    elif (q_trip.object.startswith('like')) and (q_trip.subject == 'does'):
        answer = '{}, {} {} {}.'
        # new_subj_doc = nlp(unicode(q_trip.subject))
        #       # new_subj_doc = nlp(unicode(q_doc.subject))
        p1 = q_trip.predicate.capitalize()
        p2 = q_trip.object[5:].capitalize()
        host_person = [
            str(person.likes) for person in persons if person.name == p1
        ][0]
        if p2 in host_person:
            print(answer.format('Yes', p1, 'likes', p2))
        else:
            print(answer.format('No', p1, 'does not like', p2))
    # if p2 in select_person(p1).likes:
    #    print('yes')
    #  else:
    #    print ('no')

    # people = [str(e.text) for e in q_doc.ents]


#        new_subj_doc_childern = new_subj_doc[0].children
#people = str([t.text.capitalize() for t in new_subj_doc_childern])
#        print (people)

    elif q_trip.subject.startswith('does') and (q_trip.object == ('cat')
                                                or q_trip.object == ('dog')):
        answer = '{}, {} does not have a {}.'
        new_subj_doc = nlp(unicode(q_trip.subject))
        #people = new_subj_doc[5:].capitalize()
        # ents = list(new_subj_doc.ents)
        # people = [ents[i].text for i in range(len(ents)) if ents[i].label_=='PERSON']
        new_subj_doc_childern = new_subj_doc[0].children
        people = str([t.text.capitalize() for t in new_subj_doc_childern])

        if people in persons:
            pet = get_persons_pet(people)
            if q_trip.object == pet:
                print(answer.format('Yes', people, q_trip.object))
            else:
                print(answer.format('No', people, q_trip.object))

        else:
            print(answer.format('No', people, q_trip.object))
示例#17
0
def main():
    sents = get_data_from_file()

    cl = ClausIE.get_instance()

    triples = cl.extract_triples(sents)
    for t in triples:
        r = process_relation_triplet(t)
        #print(r)

    question = ' '
    while question[-1] != '?':
        question = raw_input("Please enter your question: ")

        if question[-1] != '?':
            print('This is not a question... please try again')

    q_trip = cl.extract_triples([preprocess_question(question)])[0]
    q_sentence = q_trip.subject + ' ' + q_trip.predicate + ' ' + q_trip.object
    q_doc = nlp(unicode(q_sentence))
    # (WHO, has, PET)
    # here's one just for dogs
    # Q.1> Who has a <pet type>?
    if q_trip.subject.lower() == 'who' and (q_trip.object == 'dog' or q_trip.object == 'cat'):
        answer = '{} has a {} named {}.'
        print()
        for person in persons:
            pet = get_persons_pet(person.name)
            if pet and pet.type == 'dog' and q_trip.object == 'dog':
                print(answer.format(person.name, 'dog', pet.name))
            elif pet and pet.type == 'cat' and q_trip.object == 'cat':
                print(answer.format(person.name,'cat',pet.name))
    # Q.2> Does <person> like <person>?
    elif 'does' in q_trip.subject.lower() and 'like' in q_sentence:
        answer = '{} likes {}'
        print()
        person_a = [token.text for token in q_doc.ents if token.label_== 'PERSON'][0]
        select_a = select_person(person_a)
        person_b = q_trip.object
        select_b = select_person(person_b)
        if select_b in select_a.likes:
            print(answer.format(select_a,select_b))
        else:
            print('No relation given')

    # Q.3> Who is [going to|flying to|traveling to] <place>?
    elif q_trip.subject.lower() == 'who' and 'GPE' in [e.label_ for e in q_doc.ents]:
        answer = '{} is traveling to {}'
        print()
        check = 0
        travel_d = str([t.text for t in q_doc.ents if t.label_=="GPE"][0])
        for person in persons:
            if travel_d in [trip.departs_to for trip in person.travels]:
                print(answer.format(person.name,travel_d))
                check = 1
        if check==0:
            print(answer.format('Nobody',travel_d))

    # Q.4> When is <person> [going to|flying to|traveling to] <place>?
    elif 'when' in q_sentence.lower() and 'GPE' in [e.label_ for e in q_doc.ents]:
        answer = '{} is traveling to {} on {}'
        print()
        person_t = str([t.text for t in q_doc.ents if (t.label_=="PERSON" or t.label_=="ORG")][0])
        dest = str([t.text for t in q_doc.ents if t.label_=="GPE"][0])
        check=0
        for person in persons:
            if person_t == person.name:
                if dest in [trip.departs_to for trip in person.travels]:
                    print(answer.format(person_t, dest, trip.departs_on))
                    check =1
        if check==0:
            print(person_t+' is NOT traveling to '+dest+' anytime soon')
    # Q.5> Who likes <person>?
    elif 'who' in q_sentence.lower() and ('ORG' in [e.label_ for e in q_doc.ents] or 'PERSON' in [e.label_ for e in q_doc.ents]):
        person_l = str([t.text for t in q_doc.ents if (t.label_=='PERSON' or t.label_=='ORG')][0])
        like_l = []
        for person in persons:
            if person_l==person.name:
                for p in person.likes:
                    like_l.append(p)
        if len(like_l)>0:
            print(person_l+' is liked by the following people:')
            for p in like_l:
                print(p)
        elif len(like_l)==1:
            print(person_l+' is liked by '+like_l[0])
        else:
            print('Nobody likes '+person_l)

    # Q.6> Who does <person> like?
    elif q_sentence.subject.lower() == 'who' and 'does' in q_sentence.lower():
        person_lb = str()
def main():
    sents = get_data_from_file()

    cl = ClausIE.get_instance()

    triples = cl.extract_triples(sents)

    for t in triples:
        r = process_relation_triplet(t)

    #   print(r)


    #print(persons)

    #print(pets)

    traveldic = ['travel', 'fly', 'drive', 'go', 'visit']
    #traveldic = {'travel to': 'take a trip',  # verbs here are lemmatized
     #'fly to': 'take a trip',
     #'drive to': 'take a trip',
     #'go to': 'take a trip',
     #}



    question  = ' '

    while question[-1] != '?':
        question = raw_input("Please enter your question: ")

        if question[-1] != '?':
            print('This is not a question... please try again')
    question2 = question
    question = question.lower()
    q_trip = cl.extract_triples([preprocess_question(question)])[0]
    # q_doc = nlp(unicode((question)))
    doc = nlp(unicode(question2))

    # (WHO, has, PET)
    # here's one just for dogs


    if q_trip.subject.lower() == 'who' and q_trip.object == 'dog':
        answer = '{} has a {}.'

        for person in persons:
            pet = person.has
            if 'dog' in pet:
                print(answer.format(person.name, 'dog'))

    elif q_trip.subject.lower() == 'who' and q_trip.object == 'cat':
        answer = '{} has a {}.'

        for person in persons:
            pet = person.has
            if 'cat' in pet:
                print(answer.format(person.name, 'cat'))


    # here's one for cats
    elif q_trip.subject.lower() == 'who' and q_trip.object == 'cat':
        answer = '{} has a {}.'

        for person in persons:
            pet = get_persons_pet(person.name)
            if pet and pet.type == 'cat':
                print (answer.format(person.name, 'cat'))





    # Does someone like someone else?
    elif (q_trip.object.startswith('like')) and (q_trip.subject == 'does'):
        answer = '{}, {} {} {}.'

        doc = nlp(unicode(question2))
        personnames = [entity.text for entity in doc.ents if entity.label_=='PERSON']
        p1 = str(personnames[0])
        p2 = str(personnames[1])
        host_person = [str(person.likes) for person in persons if person.name == p1]

        if p2 in host_person:
            print (answer.format('Yes',p1,'likes', p2))
        else:
            print (answer.format('No', p1,'does not like', p2))


    # Who likes <person>?
    elif (q_trip.subject == 'who') and (q_trip.predicate == 'likes'):
        answer = '{} {} {}'

        doc = nlp(unicode(question2))
        personnames = [entity.text for entity in doc.ents if entity.label_ == 'PERSON']
        p1 = str(personnames[0])
        personlikep1 = []
        for person in persons:
            personlike = person.likes
            for p in set(personlike):
                if p1 == p.name:
                    personlikep1.append(person.name)
        ref = []

        if personlikep1 != ref:
            print (answer.format(' and '.join(personlikep1), 'likes',p1))
        else:
            print ('Sorry, we don\'t know.')



    # Who does <person> like?
    elif (q_trip.object == 'who') and (q_trip.predicate.endswith('like')):
        answer = '{} {} {}'

        # doc = nlp(unicode(question2))
        personnames = [entity.text for entity in doc.ents if entity.label_ == 'PERSON']
        p1 = str(personnames[0])
        likes = [person.likes for person in persons if person.name == p1][0]
        p1likes = [p.name for p in set(likes) ]
        ref = []
        if p1likes != ref:
            print(answer.format(p1, 'likes', ' and '.join(p1likes)))
        else:
            print ('Sorry, we don\'t know.')



    # Who is [going to | flying to | traveling to | visiting] < place >?
    elif (str(t.lemma_) for t in doc if str(t.lemma) in traveldic ) and (q_trip.subject == 'who'):
        answer = '{} {} {}'
        persontrip = []
        ref = []
        qplace = [str(entity.text) for entity in doc.ents if entity.label_ == 'GPE']
        for person in persons:
            qtrip = get_persons_trip(person.name)
            #print (person.travels.date)
            if qtrip and ( qplace[0] in qtrip.place ):
                persontrip.append(person.name)
                # print (person.name)


        if persontrip != ref:
            print (answer.format(' and '.join(persontrip), 'is traveling to', qplace[0]))

        else:
            print ('Sorry, we don\'t know.')



    # Does <person> likes <Pet>?
    elif q_trip.subject.startswith('does') and ( q_trip.object == ('cat') or q_trip.object == ('dog')):
        answer = '{}, {} does not have a {}.'
        new_subj_doc = nlp(unicode(q_trip.subject))
        #people = new_subj_doc[5:].capitalize()
        # ents = list(new_subj_doc.ents)
        # people = [ents[i].text for i in range(len(ents)) if ents[i].label_=='PERSON']
        new_subj_doc_childern = new_subj_doc[0].children
        people = str([t.text.capitalize() for t in new_subj_doc_childern])


        if people in persons:
            pet = get_persons_pet(people)
            if q_trip.object == pet:
                print(answer.format('Yes', people, q_trip.object))
            else:
                print(answer.format('No', people, q_trip.object))

        else:
            print (answer.format('No', people, q_trip.object))


    else:
        print('We don\'t have the material to answer the question.')
示例#19
0
 def __init__(self):
     try:
         self.cl = ClausIE.get_instance(
             jar_filename='/home/guest/git/DeFactoNLP/clausie/clausie.jar')
     except Exception as error:
         raise error
from __future__ import print_function
import re
import en_core_web_sm

from pyclausie import ClausIE

nlp = en_core_web_sm.load()
CL_OBJ = ClausIE.get_instance()

re_spaces = re.compile(r'\s+')


class Person(object):
    def __init__(self, name, likes=None, has=None, travels=None):
        """
        :param name: the person's name
        :type name: basestring
        :param likes: (Optional) an initial list of likes
        :type likes: list
        :param dislikes: (Optional) an initial list of likes
        :type dislikes: list
        :param has: (Optional) an initial list of things the person has
        :type has: list
        :param travels: (Optional) an initial list of the person's travels
        :type travels: list
        """
        self.name = name
        self.likes = [] if likes is None else likes
        self.has = [] if has is None else has
        self.travels = [] if travels is None else travels
def main():
 sents = get_data_from_file()

    cl = ClausIE.get_instance()

    triples = cl.extract_triples(sents)
示例#22
0
def main(question):
    sents = process_data_from_input_file()

    cl = ClausIE.get_instance()

    triples = cl.extract_triples(sents)

    for t in triples:
        r = process_relation_triplet(t)

    answers = []

    #question = '?'

    #question = raw_input("Please enter your question: [type exit to finish the query] \n")

    if question[-1] != '?':
        answers.append('This is not a question... please try again')
    else:

        ques = nlp(unicode(question))
        if "What" not in [str(word) for word in ques]:
            q_trip = cl.extract_triples([preprocess_question(question)])[0]
        else:
            q_trip = cl.extract_triples(["Hamid likes Ali"])[0]
        """
        1. Who has a <pet_type>?
        Checking the wh-word
        Checking the pet type mentioned in the question
        """
        if q_trip.subject.lower() == 'who' and q_trip.object.lower() == 'dog':
            for person in persons:
                pet = get_persons_pet(person.name)
                if pet and pet.type == 'dog':
                    answers.append('{} has a {} named {}.'.format(
                        person.name, 'dog', pet.name))

        # here's one just for cats
        if q_trip.subject.lower() == 'who' and q_trip.object == 'cat':

            for person in persons:
                pet = get_persons_pet(person.name)
                if pet and pet.type == 'cat':
                    answers.append('{} has a {} named {}.'.format(
                        person.name, 'cat', pet.name))
        """
        2. Who is [going to/flying to/traveling to/visiting] <place>?
        Checking the wh-word
        Checking the verb which is used in the question
        
        """
        if q_trip.subject.lower() == 'who':
            check_list = (('going to' in question), ('flying to' in question),
                          ('traveling to' in question), ('visiting'
                                                         in question))
            if any(check_list):
                accepted_trips = [
                    trip for trip in trips if trip.location in q_trip.object
                ]

                if len(accepted_trips) > 0:
                    for asked_trip in accepted_trips:
                        passenger = asked_trip.person
                        location = asked_trip.location
                        answers.append('{} is going to {}.'.format(
                            passenger, asked_trip.location))

                else:
                    answers.append("I don't know")
        """ 
        3. (does <person> like <person>?)
        Check if the quesion yes/no or not
        Check if the second person in the quesion is in the "like-list" of the first person 
        """
        if str(ques[0]).lower() == 'does':
            accepted_subject = [
                person for person in persons if person.name in q_trip.subject
            ]
            if len(accepted_subject) > 0:
                first = accepted_subject[0]
                accepted_object = [
                    person for person in persons
                    if person.name in q_trip.object
                ]
                if len(accepted_object) > 0:
                    second = accepted_object[0]
                    if second in first.likes:
                        answers.append('Yes! {} likes {}.'.format(
                            first, second))
                    else:
                        answers.append('No! {} does not like {}.'.format(
                            first, second))
                else:
                    answers.append("I don't know")
            else:
                answers.append("I don't know".format(ques.ents[0]))
        """
        4. (What's the name of <person>'s <pet_type>?)
        Check if the wh-quesion is what
        Check if the person has pet or not
        Report the name of the pet
        """
        #This part is for dogs
        if 'What' in [str(word) for word in ques
                      ] and 'dog' in [str(word) for word in ques]:
            name = str(ques.ents[0])
            accepted_names = [
                person for person in persons if person.name == name
            ]
            if len(accepted_names) > 0:
                owner = accepted_names[0]
                if len(owner.has) > 0 and owner.has[0].type == "dog":
                    answers.append("{}'s {}'s name is {}.".format(
                        owner.name, "dog", owner.has[0].name))

                else:
                    answers.append("{} does not have a pet".format(owner))

            else:
                answers.append("We have no information about this person")
        #This part is for cats
        if 'What' in [str(word) for word in ques
                      ] and 'cat' in [str(word) for word in ques]:
            name = str(ques.ents[0])
            accepted_names = [
                person for person in persons if person.name == name
            ]

            if len(accepted_names) > 0:
                owner = accepted_names[0]
                #print(owner.name)
                if len(owner.has) > 0 and owner.has[0].type == "cat":
                    answers.append("{}'s {}'s  name is {}.".format(
                        owner.name, "cat", owner.has[0].name))

                else:
                    #answers.append("{} does not have a pet".format(owner))
                    answers.append("I don't know")
            else:
                answers.append("I don't know")
        """
        5. When is <person> [going to/flying to/traveling to/visiting] <place>?
        Check if the wh quesion is when
        Checking the lemma_ of the root
        Inquiring the trip list and "location"; I have defined a class for any trip
        """
        if 'When' in list_maker(ques):
            check_list = (('going to' in question), ('flying to' in question),
                          ('traveling to' in question), ('visiting'
                                                         in question))
            if any(check_list):
                accepted_trips = [
                    trip for trip in trips if trip.location in q_trip.object
                ]

                if len(accepted_trips) > 0:
                    for asked_trip in accepted_trips:
                        if asked_trip.person in question.split(' '):
                            time = asked_trip.time
                            answers.append('In {}.'.format(time))
                else:
                    answers.append("I don't know")
        """
        6. (Who likes <person>?)
        Check the wh word; also check that the quesion contain like
        Check the "like list" of the other player
        """
        if q_trip.subject.lower() == 'who' and str(
                q_trip.predicate) == 'likes':
            accepted_names = [
                person for person in persons if person.name == q_trip.object
            ]

            if len(accepted_names) > 0:
                loved = accepted_names[0]
                lovers = []

                for person in persons:
                    if loved in person.likes:
                        lovers.append(person)
                if len(lovers) > 1:
                    answers.append('{},and {} like {}.'.format(
                        ', '.join([str(name) for name in lovers[0:-1]]),
                        lovers[-1], loved))
                elif len(lovers) == 1:
                    answers.append('{} likes {}.'.format(lovers[0], loved))
                else:
                    answers.append('{} likes {}.'.format("no one", loved))
            else:
                answers.append("I don't know")
        """
        7. (Who does <person> like?)
        Check the wh word; also check that the quesion contain like
        Check the "like list" of the mentioned person
        """
        if q_trip.object.lower() == 'who' and q_trip.predicate == 'does like':
            accepted_names = [
                person for person in persons if person.name == q_trip.subject
            ]
            if len(accepted_names) > 0:
                lover = accepted_names[0]

                if len(lover.likes) > 1:
                    answers.append('{} likes {},and {}.'.format(
                        lover,
                        ', '.join([str(name) for name in lover.likes[0:-1]]),
                        lover.likes[-1]))
                elif len(lover.likes) == 1:
                    answers.append('{} likes {}.'.format(
                        lover, lover.likes[0]))
                else:
                    answers.append('{} likes {}.'.format("no one", loved))
            else:
                lover = None
                answers.append("I don't know")

    return answers
def answer_question(question_string):
    cl = ClausIE.get_instance()

    # Q1: Who has a <pet_type>? (e.g. Who has a dog?)
    q_trip = cl.extract_triples([preprocess_question(question_string)])[0]
    sentence = q_trip.subject + ' ' + q_trip.predicate + ' ' + q_trip.object

    doc = nlp(unicode(sentence))
    for t in doc:
        if t.pos_ == 'VERB' and t.head == t:
            root = t

    if q_trip.subject.lower() == 'who' and ('dog' in q_trip.object
                                            or 'cat' in q_trip.object):

        answer = '{} has a {}.'

        pet_type = 'dog' if 'dog' in q_trip.object else 'cat'
        for person in persons:
            pet = get_persons_pet(person.name)
            if pet:
                if pet.type == pet_type:
                    print(answer.format(person.name, pet_type, pet.name))

    # Q2: Who is [going to|flying to|traveling to] <place>? (e.g. Who is flying to Japan?)
    if q_trip.subject.lower() == 'who' and (root.lemma_ == 'go'
                                            or root.lemma_ == 'fly'
                                            or root.lemma_ == 'travel'):
        answer = '{} is going to {}'

        place_doc = nlp(unicode(q_trip.object))
        for e in place_doc.ents:
            if e.label_ == 'GPE':
                place = e.text
                # print(place)
                for where in trips:
                    # print(where.place)
                    if where.place[0] == place:
                        # a = get_place(where)
                        print(answer.format(where.traveller, where.place[0]))

    # Q3: Does <person> like <person>? (e.g. Does Bob like Sally?)
    if 'does' in q_trip.subject.lower() and 'like' in sentence:
        list = [e.text for e in doc.ents if e.label_ == 'PERSON']
        person_sub = list[0]
        person_obj = list[1]
        x = 0
        for person in persons:
            if person.name == person_sub:
                for person1 in person.likes:
                    if person1.name == person_obj:
                        x = 1
        if x:
            print("Yes.")
        else:
            print("No")

    # Q4: When is <person> [going to|flying to|traveling to] <place>?
    if 'when' in sentence.lower() and (root.lemma_ == 'go' or root.lemma_
                                       == 'fly' or root.lemma_ == 'travel'):
        answer = "{} is going to {} in {}."
        personD = [
            e.text for e in doc.ents
            if e.label_ == 'PERSON' or (e.label_ == 'ORG')
        ][0]
        for e in doc.ents:
            if e.label_ == 'GPE':
                place = e.text
                # print(place)
        for where in trips:
            # print(where.place[0])
            # print(where.traveller)
            if where.place[0] == place and where.traveller == personD:
                print(
                    answer.format(where.traveller, where.place[0],
                                  where.time[0]))

    # 5) Who likes <person>?
    if 'who' in q_trip.subject.lower() and root.lemma_ == 'like':
        qdoc = nlp(unicode(sentence))
        personB = [e.text for e in qdoc.ents if e.label_ == 'PERSON'][0]
        for person in persons:
            for personC in person.likes:
                if personC.name == personB:
                    print(person)

    # 6) Who does <person> like?
    if 'does' in q_trip.predicate.lower() and root.lemma_ == 'like':
        qdoc = nlp(unicode(sentence))
        personD = [
            e.text for e in qdoc.ents
            if e.label_ == 'PERSON' or (e.label_ == 'ORG')
        ][0]
        m = add_person(personD)
        for person in m.likes:
            print(person)
示例#24
0
def answer_question(question_string):
        answered = 1
        if question_string.startswith('what') or question_string.startswith('What'):
        # For Bonus (5 pts): What's the name of <person>'s <pet_type>? (e.g. What's the name of Mike's dog?)
            doc=nlp(unicode(question_string))
            answer="{}'s {}'s name is {}"
            a=1
            for e in doc.ents:
                if e.label_=='PERSON':
                    person=add_person(e.text)
                    pet_type='dog' if 'dog' in question_string else 'cat'
                    for x in person.has:
                        if x.type==pet_type:
                            # if x.name:
                                print(answer.format(person.name,pet_type,x.name))
                                a=0
                                answered=0
                            # else:

                        #     print()
            if a:
                print(""+person.name+" has not a "+pet_type)
                answered = 0
        else:
            IE = ClausIE.get_instance()

            q_trip = IE.extract_triples([preprocess_question(question_string)])[0]

            # (WHO, has, PET)
            # here's one just for dogs
            sentence = q_trip.subject + ' ' + q_trip.predicate + ' ' + q_trip.object
            # print("triplet.subject: " + q_trip.subject)
            # print("triplet.predicate: " + q_trip.predicate)
            # print("triplet.object: " + q_trip.object)

            doc = nlp(unicode(sentence))

            for t in doc:
                if t.pos_ == 'VERB' and t.head == t:
                    root = t
                    # print(root)
                # elif t.pos_ == 'NOUN'

            # also, if only one sentence
            # root = doc[:].root

            # For q1:Who has a <pet_type>? (e.g. Who has a dog?)
            if q_trip.subject.lower() == 'who' and ('dog' in q_trip.object or 'cat' in q_trip.object):

                answer = '{} has a {} named {}.'

                pet_type = 'dog' if 'dog' in q_trip.object else 'cat'
                x = 1
                for person in persons:
                    pet = get_persons_pet(person.name)
                    if pet:
                        if pet.type == pet_type:
                            print(answer.format(person.name, pet_type, pet.name))
                            x = 0
                            answered = 0
                if x:
                    print('Nobody has a' + pet_type)
                    answered = 0
            # for answer in answers:
            #     print answer

            # For q2:  Who is [going to|flying to|traveling to] <place>? (e.g. Who is flying to Japan?)
            if q_trip.subject.lower() == 'who' and (
                    root.lemma_ == 'go' or root.lemma_ == 'fly' or root.lemma_ == 'travel'):
                answer = '{} is going to {}, time:{}.'
                # place=t.text for t in doc.ents if t.label_=='GPE'
                place_doc = nlp(unicode(q_trip.object))
                a = 1

                for x in place_doc.ents:
                    if x.label_ == 'GPE':
                        place = x.text
                        for trip in trips:
                            if trip.to == place:
                                print(answer.format(trip.name, trip.to, trip.time))
                                a = 0
                                answered = 0
                if a:
                    print('Nobody is going to ' + x.text)
                    answered = 0

            # For q3:  Does <person> like <person>? (e.g. Does Bob like Sally?)
            if 'does' in sentence.lower() and 'like' in sentence and 'who' not in sentence.lower():
                list = [e.text for e in doc.ents if e.label_ == 'PERSON' or e.label_ == 'ORG']
                person_sub = list[0]
                person_obj = list[1]

                x = 0
                for person in persons:
                    if person.name == person_sub:
                        for person1 in person.likes:
                            if person1.name == person_obj:
                                # print(x)

                                x = 1
                #                 print(x)
                # print(x)
                if x:
                    print("Yes.")
                    answered = 0
                else:
                    print("No.")
                    answered = 0

            # For q4:When is <person> [going to|flying to|traveling to] <place>?
            if 'when' in q_trip.object.lower() and (root.lemma_ == 'go' or root.lemma_ == 'fly' or root.lemma_ == 'travel'):


                answer = '{} is going to {} in {}.'
                # answer1 = '{} is not going to {} in {}.'
                # if q_trip.subject in [e.text for e in doc.ents if e.label_ == 'PERSON' or e.label_ == 'ORG']:
                #      print(0)
                when_who = [str(e) for e in doc.ents if e.label_ == 'PERSON' or e.label_ == 'ORG']
                when_all = ' '.join(when_who)
                when_final = when_all.split(' ')
                # if len(q4_persosn)<1:
                #     q4_persosn[0]='Mary'
                # print(trips[0].name)
                place1_doc = nlp(unicode(q_trip.object))
                for e in place1_doc.ents:
                    if e.label_ == 'GPE':
                        q4_place = e.text
                        break

                for x in when_final:
                    # print(x)
                    # print(q4_place)
                    if q_trip.subject=='Mary':
                        x='Mary'
                        # print(x)
                        for trip in trips:
                            if trip.to == q4_place:
                                if trip.name.name == x:
                                    print(answer.format(trip.name, trip.to, trip.time))
                                    answered = 0
                    # if x==trips[0].name.name:
                    #     # print(1)
                    else:
                        for trip in trips:
                            if trip.to == q4_place:
                                if trip.name.name == x:
                                    print(answer.format(trip.name, trip.to, trip.time))
                                    answered = 0

                        # elif trip.name.name == x and trip.to != q4_place:
                        #     print(answer1.format(trip.name, trip.to, trip.time))
                        #     answered = 0

            # For q5:Who likes <person>?
            if  'like' in sentence and 'who' in q_trip.subject.lower():
                if q_trip.object in [e.text for e in doc.ents if e.label_ == 'PERSON'or e.label_ == 'ORG']:
                    x = 1
                    person_like = q_trip.object
                    for person in persons:
                        for x in person.likes:
                            if x.name == person_like:
                                print(person.name)
                                x = 0
                                answered = 0
                    # if x:
                    #     print('Nobody likes ' + person_like+'.')
                    #     answered = 0

            # For q6:Who does <person> like?
            if 'like' in sentence and 'who' in q_trip.object.lower() and 'does' in sentence:
                # print(1)
                if 'Mary'in sentence:
                    # print(0)
                    person_like = add_person('Mary')
                    for a in person_like.likes:
                            print(a.name)
                            answered = 0
                elif q_trip.subject in [e.text for e in doc.ents if e.label_ == 'PERSON'or e.label_ == 'ORG']:
                    person_like = add_person(q_trip.subject)
                    if len(person_like.likes) == 0:
                        print('' + person_like.name + ' likes nobody.')
                        answered = 0
                    else:

                        for a in person_like.likes:
                            print(a.name)
                            answered = 0

        if answered:
            print("Sorry, I don't know.")
def answer_question(question_string):
    if 'What' in question_string:
        e_doc = nlp(unicode(question_string))
        for e in e_doc.ents:
            #print(9)
            if e.label_ == 'PERSON':
                person = e.text
                #print(person)
                for x in persons:
                    if x.name == person:
                        for e in x.has:
                            #print(e.name)
                            if e.name != None:
                                print(e.name)

    else:
        cl = ClausIE.get_instance()
        # q1
        q_trip = cl.extract_triples([preprocess_question(question_string)])[0]
        sentence = q_trip.subject + ' ' + q_trip.predicate + ' ' + q_trip.object

        doc = nlp(unicode(sentence))
        # print(q_trip.object)
        # print(q_trip.predicate)
        # print(q_trip.subject)
        if q_trip.subject.lower() == 'who' and ('dog' in q_trip.object
                                                or 'cat' in q_trip.object):

            answer = '{} has a {}.'

            pet_type = 'dog' if 'dog' in q_trip.object else 'cat'
            for person in persons:
                pet = get_persons_pet(person.name)
                if pet:
                    if pet.type == pet_type:
                        print(answer.format(person.name, pet_type, pet.name))
        # q2
        if q_trip.subject.lower() == 'who' and (root.lemma_ == 'go' or 'fly'
                                                or 'travel'):
            answer = '{} is going to {}, time:{}'

            place_doc = nlp(unicode(q_trip.object))
            for e in place_doc.ents:
                if e.label_ == 'GPE':

                    place = e.text
                    # print(place)
                    for trip in trips:
                        if trip.to == place and len(trip.time) > 0:
                            print(answer.format(trip.name, trip.to, trip.time))
        # q3
        if 'does' in q_trip.subject.lower():
            # answer = '{} likes {}'
            sent = q_trip.subject + ' ' + q_trip.predicate + ' ' + q_trip.object

            e_doc = nlp(unicode(sent))
            list = [
                str(e) for e in e_doc.ents
                if e.label_ == 'PERSON' or e.label_ == 'ORG'
            ]
            x = 0
            # print(list)
            subject = list[0]
            object = q_trip.object

            for person in persons:
                # print(9)
                if person.name == subject:
                    for personA in person.likes:
                        # print(personA.name)
                        if personA.name == object:
                            x = 1
                        # print(x)
            if x:
                print("y")
            else:
                print("N")
        # q4
        if 'when' in q_trip.object and (root.lemma_ == 'go' or root.lemma_
                                        == 'fly' or root.lemma_ == 'travel'):
            answer = '{} is going to {}, time:{}'

            e_doc = nlp(unicode(q_trip.object))
            for e in e_doc.ents:
                if e.label_ == 'GPE':

                    place = e.text
                    # print(place)
                    for trip in trips:
                        if trip.to == place and len(trip.time) > 0:
                            print(answer.format(trip.name, trip.to, trip.time))

        # q5
        if 'who' in q_trip.subject.lower():
            answer = '{} likes {}'
            # e_doc = nlp(unicode(q_trip.object))

            for e in doc.ents:

                if e.label_ == 'PERSON':

                    person1 = e.text
                    #print(person1)
                    for person in persons:

                        for e in person.likes:
                            if e.name == q_trip.object:
                                print(person.name)

        # q6
        if root.lemma_ == 'like':
            x = 1
            for x in doc:
                if x.text == "n't":
                    x = 0
            if x:
                fw_doc = nlp(unicode(q_trip.object))

                if 'who' in q_trip.object.lower():
                    answer = '{} likes {}'

                e_doc = nlp(unicode(question_string))
                for e in e_doc.ents:

                    if e.label_ == 'PERSON':
                        person = e.text

                        for person in persons:
                            #print(person)
                            if person.name == q_trip.subject:
                                for e in person.likes:
                                    print(e.name)
def process_what_petname_question(string):
    cl = ClausIE.get_instance()
    str = string.replace('What\'s', '').strip()
    triples = cl.extract_triples([str])[0]
    w_quetsion = triples.subject + ' ' + triples.predicate + ' ' + triples.object + ' ' + 'name'
    return w_quetsion
示例#27
0
def answer_question(question=' '):
    while question[-1] != '?':
        question = raw_input("Please enter your question: ")

        if question[-1] != '?':
            print('This is not a question... please try again')

    cl = ClausIE.get_instance()
    q_trip = cl.extract_triples([preprocess_question(question)])[0]

    triplet_sentence = q_trip.subject + ' ' + q_trip.predicate + ' ' + q_trip.object
    doc = nlp(unicode(triplet_sentence))
    root = doc[:].root

    # retrieve answers for questions like (WHO, has, PET)
    # here's one just for dogs
    # you should really check the verb... this is just an example not the best way to do things
    if q_trip.subject.lower() == 'who' and root.lemma_ == 'have':

        answer = '{} has a {} named {}.'
        if q_trip.object == 'dog':
            for person in persons:
                pet = get_persons_pet(person.name)
                if pet and pet.type == 'dog':
                    print(answer.format(person.name, pet.type, pet.name))
        elif q_trip.object == 'cat':
            for person in persons:
                pet = get_persons_pet(person.name)
                if pet and pet.type == 'cat':
                    print(answer.format(person.name, pet.type, pet.name))

    # retrieve answers for questions like (WHO, like, PERSON)
    # again this is just an example, NOT the best way to do things. That's for you to figure out.
    elif q_trip.subject.lower(
    ) == 'who' and root.lemma_ == 'like' and q_trip.object in [
            e.text
            for e in doc.ents if e.label_ == 'PERSON' or e.label_ == 'ORG'
    ]:
        answer = '{} likes {}'

        liked_person = select_person(q_trip.object)

        for person in persons:
            if person.name != q_trip.object and liked_person in person.likes:
                print(answer.format(person.name, liked_person.name))

    elif q_trip.subject.lower(
    ) == 'does' and q_trip.predicate == 'like' and q_trip.object in [
            e.text for e in doc.ents if e.label_ == 'PERSON'
    ]:
        person = select_trip(q_trip.subject)
        liked_person = select_person(q_trip.object)
        if liked_person in person.likes:
            print(person.name, 'likes ', liked_person.name)
        else:
            print(person.name, 'does not like ', liked_person.name)
    elif q_trip.subject.lower(
    ) == 'who' and q_trip.predicate == 'likes' and q_trip.object in [
            e.text for e in doc.ents if e.label_ == 'PERSON'
    ]:
        like_person = select_person(q_trip.object)
        for person in persons:
            if like_person in person.likes:
                print(person.name, 'likes ', like_person.name)

    else:
        print("I don't know.")
示例#28
0
def answer_questions(string):
    sents = get_data_from_file()
    cl = ClausIE.get_instance()
    triples = cl.extract_triples(sents)
    q_trip = cl.extract_triples([preprocess_question(string)])[0]
    answers = []
    # (WHO, has, PET)
    # here's one just for dogs
    print(q_trip)

    if q_trip.subject.lower() == 'who' and q_trip.object == 'dog':
        answer = '{} has a dog.'

        for person in persons:
            pet = get_persons_pet(person.name)
            if pet and pet.type == 'dog':
                answer = (answer.format(person.name))
                answers.append(answer)

    if q_trip.subject.lower() == 'who' and q_trip.object == 'cat':
        answer = '{} has a cat.'

        for person in persons:
            pet = get_persons_pet(person.name)
            if pet and pet.type == 'cat':
                answer = (answer.format(person.name))
                answers.append(answer)

    if q_trip.subject.lower() == 'what' and 'name' in q_trip.object:
        answer = '{} has a {} name {}'
        for t in q_trip.object:
            if t.pos_ == 'PERSON':
                person = t.text
                pet = get_persons_pet(person)
                answer = answer.format(person, pet, pet.name)

    if q_trip.subject.lower == 'who' and q_trip.predicate == 'likes':
        answer = '{} likes {}'
        for i in q_trip.object:
            if i.pos == 'PERSON':
                target = i
            for people in persons:
                if people.likes == target:
                    answer = answer.format(people.name, target)
                    answers.append(answer)

    if q_trip.subject.lower() == 'does' and q_trip.predicate == 'like':
        answer = '{} likes {}'
        for t in q_trip.subect:
            if t.pos_ == 'PERSON':
                Sender = t
                Receiver = q_trip.object
                if Sender.likes == Receiver:
                    answer = answer.format(Sender, Receiver)
                    answers.append(answer)

    if q_trip.subject.lower() == 'who' and (q_trip.predicate == 'going'
                                            or q_trip.predicate == 'flying'
                                            or q_trip.predicate == 'traveling'
                                            or q_trip.predicate == 'visiting'):
        destination = q_trip.object
        answer = '{} is going on a trip to {}'
        for person in persons:
            if person.travels == destination:
                answer = (answer.format(person.name), destination)
                answers.append(answer)

    if q_trip.subject.lower() == 'when' and (
            q_trip.predicate == 'going' or q_trip.predicate == 'flying' or
            q_trip.predicate == 'traveling' or q_trip.predicate == 'visiting'):
        answer = '{} is going on a trip in {}'
        for t in q_trip.object:
            if t.pos_ == 'GPE':
                destination = q_trip.object
            for trips in Trip:
                if Trip.departs_to == destination:
                    answer = answer.format(Trip.departs_on)
                    answers.append(answer)

    for answer in answers:
        if answers != []:
            print(answer)
        else:
            print('I do not know')
示例#29
0
from __future__ import print_function
import re
import spacy

from pyclausie import ClausIE

nlp = spacy.load('en_core_web_sm')
re_spaces = re.compile(r'\s+')
cl = ClausIE.get_instance()


class Person(object):
    def __init__(self, name, likes=None, has=None, travels=None):
        """
        :param name: the person's name
        :type name: basestring
        :param likes: (Optional) an initial list of likes
        :type likes: list
        :param dislikes: (Optional) an initial list of likes
        :type dislikes: list
        :param has: (Optional) an initial list of things the person has
        :type has: list
        :param travels: (Optional) an initial list of the person's travels
        :type travels: list
        """
        self.name = name
        self.likes = [] if likes is None else likes
        self.has = [] if has is None else has
        self.travels = [] if travels is None else travels

    def __repr__(self):
'''
Created on 2017.8.27
@author: ZhongqiLi
'''
from pyclausie import ClausIE
cl = ClausIE.get_instance(jar_filename='./clausie/clausie.jar')
for i in range(1):
    S = ['I learned that the 2012 Sasquatch music festival is scheduled for May 25th until May 28.']
    triples = cl.extract_triples(S)
    for t in triples:
        print t