"""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]
"""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]

Make RequestHandler's code less redundant.
Make RequestHandler's code less redundant.

  """Request handler of the module."""
  
  import ppp_datamodel
- from ppp_datamodel import Sentence
+ 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()
-         a, b, c = extract_triplet.extract_from_sentence(sentence)
+         triple = extract_triplet.extract_from_sentence(sentence)
+         (subject, predicate, object) = map(missing_or_resource, triple)
- 
-         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]
+ 
Make RequestHandler's code less redundant.
## Code 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]
## Instruction:
Make RequestHandler's code less redundant.
## Code 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]
