コード例 #1
0
 def getSortID(self):
     if self.type == 'superlative':
         for token in self.nlp.tokens:
             if token.tag_ == 'JJS':
                 possible_sort_words = [token.lemma_] + nounify(token.lemma_)
                 for word in possible_sort_words:
                     ID = IDfinder(word, "property", self.specs).findIdentifier()
                     if ID != '':
                         print("ID found for word " + word + ", ID is " + str(ID))
                         return ID
     return None
コード例 #2
0
    def getAnswer(self):
        print("question is ")
        print(self.question.question)
        print(" with type " + self.question.type)
        ###this defines what to print, based on the type. Currently there are only list type (simple answer ~ list one), so the others are not implemented
        #print(data)

        ##in case there were initally no possible triples, and the list is also not extendable,  we extend with induced properties from the question word (like, 'Where is Singapore?')
        if not self.possibleTriplesRemaining() and not self.extendable():
            print(
                colored(
                    'No triples originally, Inducing properties from question words',
                    'green'))
            self.question.induceWordsFromQuestionWord()
        i = 0
        #print(num)
        #self.question.constructQuery()

        print(self.question.type)
        if self.question.type == "true_false":  ##checks if any of the results from the query is the same as some word in the question (like 'is it true, that the capital of the Netherlands is Amsterdam?' --> Netherlands, capital --> query gives Amsterdam --> chacks if in the question --> print yes
            print(self.question.question)
            while self.possibleTriplesRemaining(
            ) and not self.triedAllExtensions:
                self.runNLP()
                for answer in self.data["results"]["bindings"]:
                    for word in self.question.nlp.tokens:
                        if (word.text == answer[(
                                self.question.targetVariable)[1:]]['value']
                            ) or (word.lemma_ == "be" and answer[
                                (self.question.targetVariable)[1:]]['value']
                                  == "http://www.wikidata.org/prop/direct/P31"
                                  ):  #bad hack
                            print("yes")
                            return
                print(
                    "match not found, restarting the queries, number of possible triples remainig:"
                )
            print("no")
            return

        if self.question.type == "comparative_list":
            print("type is list comparative")
            ##first we have to check what is the type of objects we have to list: What countries are bigger then Franc --> France is instance of Country...
            instance = ""
            for object in self.question.possible_words["Object"]:
                try:
                    instance = "wd:" + requests.get(
                        self.url,
                        params={
                            'query':
                            self.question.constructQuery(
                                self.question.queryStatementFromTriple(
                                    self.question.getTripleFromWordsAndFormat(
                                        [object, "instance of", ""],
                                        self.question.specs.
                                        basic_question_formats["Result"]))),
                            'format':
                            'json'
                        }).json()["results"]["bindings"][0][
                            ("?var")[1:]]['value'].split("/")[-1:][0]
                    print("instance found " + str(instance))
                    break
                except:
                    print(
                        "could not get answer for query, when looking for the instance in superlative"
                    )
                    continue
            if instance != "":
                property_ID = "wdt:" + IDfinder(
                    self.question.possible_words["Property"][0], 'property',
                    self.question.specs).findIdentifier()
                self.question.sort = property_ID
                self.runNLP()
                print(self.data)
                limit = int(self.data['results']['bindings'][0][(
                    self.question.targetVariable)[1:]]['value'])
                print("limit is " + str(limit))
                self.question.possible_words["Object"] = [
                ]  ##we remove the objects and the results, since we want to run queries for objects that are the instances of the same category as the initial object
                self.question.possible_words["Result"] = [instance]

                print("listing valid answers, property is " + property_ID)

                queryBody = "?var    wdt:P31    " + instance + ";\n        " + property_ID + "    ?sort"
                self.question.sort = 1
                self.question.variable += " ?sort "
                try:
                    results = requests.get(
                        self.url,
                        params={
                            'query': self.question.constructQuery(queryBody),
                            'format': 'json'
                        }).json()["results"]["bindings"]
                except:
                    print("could not get the list of objects of type " +
                          str(instance) + " with property " + property_ID +
                          " (in comparative listing)")
                    return
                current = 0
                index = 0
                currentLabel = ""
                while True:
                    try:
                        currentLabel = results[index][(
                            "?varLabel")[1:]]['value']
                        current = int(results[index][("?sort")[1:]]['value'])
                        if current <= limit:
                            break
                        print(currentLabel)
                        index += 1
                    except:
                        print(currentLabel)
                        index += 1
                        pass

        if self.question.type == "comparative_objects":
            print("type is object comparative")
            self.runNLP()  ##gettinh result for the first object
            data1 = self.data
            firstWord = self.popped[0]
            self.runNLP()  ##getting result for the second

            if data1['results']['bindings'][0][(
                    self.question.targetVariable
            )[1:]]['value'] > self.data['results']['bindings'][0][(
                    self.question.targetVariable)[1:]]['value']:
                print(firstWord)
            else:
                print(self.popped[0])

        if self.question.type == "count":
            if self.runNLP():
                c = 0
                #########################################

                for answer in self.data['results']['bindings']:
                    if answer == '':
                        print('no answer found')
                    else:
                        c += 1
                print(c)
            pass

        if self.question.type == 'superlative':
            numberOfAnswers = self.question.getNumberOfAnswers() - 1
            count = 0
            # Running the trials
            if self.runNLP():
                #########################################

                for answer in self.data['results']['bindings']:
                    if answer == '':
                        print('no answer found')
                    else:
                        print(answer[(
                            self.question.targetVariable)[1:]]['value'])
                    count += 1
                    if count > numberOfAnswers:
                        break

        if self.question.type == "list":
            #print(self.question.question)
            #print('target is: ')
            #print(self.question.targetVariable)
            #print('data is ')
            #print(self.data)

            # Running the trials
            if self.runNLP():
                #########################################

                for answer in self.data['results']['bindings']:
                    if answer == '':
                        print('no answer found')
                    else:
                        print(answer[(
                            self.question.targetVariable)[1:]]['value'])
コード例 #3
0
ファイル: Triples.py プロジェクト: BalintHompot/LangTech_v2
 def findSQL(self):
     return 'wdt:' + IDfinder(
         self.word, 'property',
         self.triple.specs).findIdentifier()  ##requests a P ID
コード例 #4
0
ファイル: Triples.py プロジェクト: BalintHompot/LangTech_v2
 def findSQL(self):
     return 'wd:' + IDfinder(self.word, 'result',
                             self.triple.specs).findIdentifier()
コード例 #5
0
ファイル: Triples.py プロジェクト: BalintHompot/LangTech_v2
 def findSQL(self):
     return 'wd:' + IDfinder(
         self.word, 'object',
         self.triple.specs).findIdentifier()  ##requests a Q ID