<commit_before>"""Request handler of the module."""

import ppp_datamodel
from ppp_datamodel import Sentence
from ppp_datamodel.communication import TraceItem, Response
from ppp_nlp_ml_standalone import ExtractTriplet


class RequestHandler:
    def __init__(self, request):
        self.request = request

    def answer(self):
        if not isinstance(self.request.tree, Sentence):
            return []

        sentence = self.request.tree.value
        extract_triplet = ExtractTriplet.ExtractTriplet()
        a, b, c = extract_triplet.extract_from_sentence(sentence)

        if a == '?':
            subject = ppp_datamodel.Missing()
        else:
            subject = ppp_datamodel.Resource(value=a)

        if b == '?':
            predicate = ppp_datamodel.Missing()
        else:
            predicate = ppp_datamodel.Resource(value=b)

        if c == '?':
            object = ppp_datamodel.Missing()
        else:
            object = ppp_datamodel.Resource(value=b)

        triple = ppp_datamodel.Triple(subject=subject,
                                      predicate=predicate,
                                      object=object)

        meas = {'accuracy': 0.5, 'relevance': 0.5}
        trace = self.request.trace + [TraceItem('NLP-ML-standalone', triple, meas)]
        response = Response('en', triple, meas, trace)
        print(repr(response))
        return [response]<commit_msg>Make RequestHandler's code less redundant.<commit_after>"""Request handler of the module."""

import ppp_datamodel
from ppp_datamodel import Sentence, Missing, Resource
from ppp_datamodel.communication import TraceItem, Response
from ppp_nlp_ml_standalone import ExtractTriplet

def missing_or_resource(x):
    return Missing() if x == '?' else Resource(value=x)

class RequestHandler:
    def __init__(self, request):
        self.request = request

    def answer(self):
        if not isinstance(self.request.tree, Sentence):
            return []

        sentence = self.request.tree.value
        extract_triplet = ExtractTriplet.ExtractTriplet()
        triple = extract_triplet.extract_from_sentence(sentence)
        (subject, predicate, object) = map(missing_or_resource, triple)

        triple = ppp_datamodel.Triple(subject=subject,
                                      predicate=predicate,
                                      object=object)

        meas = {'accuracy': 0.5, 'relevance': 0.5}
        trace = self.request.trace + [TraceItem('NLP-ML-standalone', triple, meas)]
        response = Response('en', triple, meas, trace)
        print(repr(response))
        return [response]
{'fraction_non_alphanumeric': 0.0982791586998088, 'fraction_numerical': 0.0030592734225621415, 'mean_word_length': 2.451187335092348, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4997729', 'n_tokens_mistral': 780, 'n_tokens_neox': 728, 'n_words': 165}