Example #1
0
    def _parse_features(self, query):
        features = query.get_required_list('features')

        for value in features:
            Query.check_float('features_value', value)

        return features
Example #2
0
    def _get_signs(self, query):
        signs = query.get_required_list('signs')

        for sign_id in signs:
            Query.check_float('sign_id', sign_id)

        return signs
Example #3
0
    def _parse_features(self, query):
        features = query.get_optional_list('features')
        if features == None:
            return list()

        for value in features:
            Query.check_float('features_value', value)

        return features
Example #4
0
    def _get_properties(self, query):
        properties = query.get_optional_list('properties')
        if properties == None:
            return list()

        for value in properties:
            Query.check_str('properties_value', value)

        return properties
Example #5
0
    def remove_discipline(self):
        """
        Remove the discipline triples from triple store.
        """

        discipline = self.get_object()

        query = """
            PREFIX pp: <http://www.semanticweb.org/ontologies/2018/Pedagogical_Project/>
            PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
            PREFIX dc: <http://purl.org/dc/elements/1.1/>

            DELETE {
                <%s> rdfs:subClassOf pp:Discipline ;
                pp:belongsTo pp:Software_Engineering ;
                pp:hasType pp:%s ;
                pp:isInTheFlowOf pp:%s ;
                pp:isPartOf pp:%s ;
                dc:title "%s" ;
                pp:code "%s" ;
                dc:description "%s"
            } WHERE {}
        """ % (discipline.uri, create_uri(
            discipline.classification), create_uri(
                discipline.semester), create_uri(discipline.core_content),
               discipline.title, discipline.code, discipline.description)

        response = Query.update(query)

        return response
Example #6
0
 def test_top(self):
     book_url = path.join(path.join(TEST_DIR, "texts"), "query-2.txt")
     with Query(self.text_dir, self.db_url, book_url) as query:
         t = query.top(2)
         print " top ------ "
         for u in t:
             print u
Example #7
0
    def action(self, request, *args, **kwargs):
        """
        Remove the triple from triple store.
        """

        discipline = self.get_object()
        required = self.get_required()

        query = """
            PREFIX pp: <http://www.semanticweb.org/ontologies/2018/Pedagogical_Project/>
            DELETE {
                <%s> pp:hasRequired <%s> .
                <%s> pp:isRequiredOf <%s>
            } WHERE {}
        """ % (discipline.uri, required.uri, required.uri, discipline.uri)

        response = Query.update(query)

        if response == 204:
            messages.success(self.request,
                             "Required discipline removed successfully")
        else:
            messages.success(self.request, "There was a server error")

        return super(RemoveRequiredDisciplineView,
                     self).action(request, *args, **kwargs)
Example #8
0
    def remove_subtopics(self):
        """
        Remove all subtopics from discipline.
        """

        discipline = self.get_object()

        subtopics = Disciplines.get_contents(discipline.uri)

        if subtopics:
            for subtopic in subtopics:
                query = """
                    PREFIX pp: <http://www.semanticweb.org/ontologies/2018/Pedagogical_Project/>
                    DELETE {<%s> pp:hasContent <%s>} WHERE {}
                """ % (discipline.uri, subtopic.uri)

                Query.update(query)
Example #9
0
def _test_book(filename, period):
    filepath = path.join(training_text_dir, filename)
    assert path.isfile(filepath), filepath
    with Query(text_dir, db_url, filepath, should_download=False) as q:
        e, r = q.results()
        if period == "Elizabethan":
            assert e > r, filename
        else:
            assert r > e, filename
Example #10
0
    def remove_requireds(self):
        """
        Remove all requireds discipline from discipline.
        """

        discipline = self.get_object()

        requireds = Disciplines.get_requireds(discipline.uri)

        if requireds:
            for required in requireds:
                query = """
                    PREFIX pp: <http://www.semanticweb.org/ontologies/2018/Pedagogical_Project/>
                    DELETE {
                        <%s> pp:hasRequired <%s> .
                        <%s> pp:isRequiredOf <%s>
                    } WHERE {}
                """ % (discipline.uri, required.uri, required.uri,
                       discipline.uri)

                Query.update(query)
Example #11
0
    def form_valid(self, form):
        """
        Receive the form already validated to create a discipline.
        """

        title = form.cleaned_data['title']
        uri = create_uri(title)
        code = form.cleaned_data['code']
        description = form.cleaned_data['description']
        classification = create_uri(form.cleaned_data['classification'])
        flow = create_uri(form.cleaned_data['flow'])
        core = create_uri(form.cleaned_data['core'])

        query = """
            PREFIX pp: <http://www.semanticweb.org/ontologies/2018/Pedagogical_Project/>
            PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
            PREFIX dc: <http://purl.org/dc/elements/1.1/>

            INSERT {
                pp:%s rdfs:subClassOf pp:Discipline ;
                pp:belongsTo pp:Software_Engineering ;
                pp:hasType pp:%s ;
                pp:isInTheFlowOf pp:%s ;
                pp:isPartOf pp:%s ;
                dc:title "%s" ;
                pp:code "%s" ;
                dc:description "%s"
            } WHERE {}
        """ % (uri, classification, flow, core, title, code, description)

        if self.discipline_exists(title):
            messages.success(self.request, "This discipline already exists")
        else:
            response = Query.update(query)

            if response == 204:
                messages.success(self.request,
                                 "Discipline created successfully")
            else:
                messages.success(self.request, "There was a server error")

        return super(DisciplineCreateView, self).form_valid(form)
Example #12
0
    def action(self, request, *args, **kwargs):
        """
        Insert content into discipline.
        """

        discipline = self.get_object()
        subtopic = self.get_subtopic()

        query = """
            PREFIX pp: <http://www.semanticweb.org/ontologies/2018/Pedagogical_Project/>
            INSERT {<%s> pp:hasContent <%s>} WHERE {}
        """ % (discipline.uri, subtopic.uri)

        response = Query.update(query)

        if response == 204:
            messages.success(self.request, "Subtopic inserted successfully")
        else:
            messages.success(self.request, "There was a server error")

        return super(InsertContentView, self).action(request, *args, **kwargs)
Example #13
0
    def action(self, request, *args, **kwargs):
        """
        Remove the discipline triples from triple store.
        """

        discipline = self.get_object()

        self.remove_subtopics()
        self.remove_requireds()

        query = """
            PREFIX pp: <http://www.semanticweb.org/ontologies/2018/Pedagogical_Project/>
            PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
            PREFIX dc: <http://purl.org/dc/elements/1.1/>

            DELETE {
                <%s> rdfs:subClassOf pp:Discipline ;
                pp:belongsTo pp:Software_Engineering ;
                pp:hasType pp:%s ;
                pp:isInTheFlowOf pp:%s ;
                pp:isPartOf pp:%s ;
                dc:title "%s" ;
                pp:code "%s" ;
                dc:description "%s"
            } WHERE {}
        """ % (discipline.uri, create_uri(
            discipline.classification), create_uri(
                discipline.semester), create_uri(discipline.core_content),
               discipline.title, discipline.code, discipline.description)

        response = Query.update(query)

        if response == 204:
            messages.success(self.request, "Discipline removed successfully")
        else:
            messages.success(self.request, "There was a server error")

        return super(DisciplineRemoveView,
                     self).action(request, *args, **kwargs)
Example #14
0
def query():
    """Returns the query results."""
    reload(sys)
    sys.setdefaultencoding("utf-8")
    template_variables = {}
    db_url = path.join(BASE_DIR, "berrynet.db")
    text = request.args.get('text', '')
    filename = str(uuid.uuid4())
    book_url = path.join(TEMP_DIR, filename)
    e, r, t = None, None, None

    with open(book_url, 'w') as text_file:
        text_file.write(text)

    with Query(None, db_url, book_url, should_download=False) as query:
        e, r = query.results()
        t = query.top(500)

    template_variables['elizabethan'] = e
    template_variables['romantic'] = r
    template_variables['top'] = t
    print "template variables = %s" % str(template_variables)
    return render_template('result.html', **template_variables)
    
Example #15
0
 def test_elizabethan_1(self):
     book_url = path.join(path.join(TEST_DIR, "texts"), "query-2.txt")
     with Query(self.text_dir, self.db_url, book_url) as query:
         e, r = query.results()
         self.assertTrue(e > r)
Example #16
0
    "tokenize": True,
    "stem": True,
    "stopw_minimal": False,
    "tfidf": True
})

corpus = Config.loadCorpus(kb_filename, config)

questions = [
    Question(q, {config: process_chain(q.strip(), config)})
    for q in loadtest(test_filename)
]

measure = Metric(config, euclidean)

queries = [Query(q, [measure]) for q in questions]

results = [corpus.queryGroup(query) for query in queries]

with open(output_filename, "w") as ofile:
    for result in results:

        score = result.top().score
        #print(score)
        if score <= 1.1:
            answer = result.top().qa.answer().get()
            print(answer, file=ofile)
        else:
            print("0", file=ofile)

print("Elapsed time: " + str(time() - start_time))
Example #17
0
 def test_romantic_1(self):
     book_url = path.join(path.join(TEST_DIR, "texts"), "query-1.txt")
     with Query(self.text_dir, self.db_url, book_url) as query:
         e, r = query.results()
         self.assertTrue(r > e)
Example #18
0
    def _evaluate(self, query_func, metrics, decide=None):

        queries = []
        for ans_nr, qtext in self.dev.items():
            qformat = {}

            for m in metrics:

                qformat[m.format] = process_chain(qtext, m.format)

            queries.append((Query(question=Question(qtext, qformat),
                                  metrics=metrics,
                                  n=1), ans_nr))

        answ = []

        mscores = {}
        s = {}
        for m in metrics:
            mscores[m] = []
            s[m] = 0

        c = 0

        #print(len(queries))
        #print(len(self.corpus.qa_corpus))

        b = True
        for qq, ans in queries:
            rs = query_func(qq)

            #print(rs.rankings)

            if b:
                max = {}
                min = {}
                for m, hp in rs.rankings:
                    max[m] = 0
                    min[m] = 1
                b = False

            for m, hp in rs.rankings:
                mscores[m].append(hp[0].qa.ans.nr)

                if hp[0].qa.ans.nr == ans:
                    if hp[0].score <= 1:
                        s[m] += 1
                    #print(hp[0].score)
                    max[m] = max[m] if hp[0].score < max[m] else hp[0].score
                else:
                    min[m] = min[m] if hp[0].score > min[m] else hp[0].score
                #print("############")
                #print(hp[0].score)
                #print("query:" + qq.question.text)
                #print("processed" + str(qq.question.format[m.format]))
                #print("obtained: " + hp[0].q.text)
                #print("processed" + str(hp[0].q.format[m.format]))

            answ.append(ans)

        #for m in max.keys():
        #    print("max: " + str(max[m]) + " " + m.name)
        #    print("min: " + str(min[m]) + " " + m.name)
        #    print(s[m]/len(queries))
        return mscores, answ
Example #19
0
 def parse_query(self, data):
     logging.info(data[:512])
     ''' Parse json '''
     query = Query.from_json(data)
     ''' Initialize session specific params '''
     self._init_session_params(query)