Exemplo n.º 1
0
 def query(self, input_string):
     # type: (text_type)->text_type
     """* What you can do
     """
     try:
         return self.__query(input_string=input_string)
     except UnicodeDecodeError:
         logger.error(msg=traceback.format_exc())
         raise Exception()
Exemplo n.º 2
0
    def __run_subprocess_mode(self, input_string):
        # type: (text_type)->Tuple[bool,text_type]
        assert isinstance(self.juman, SubprocessHandler)
        assert isinstance(self.knp, SubprocessHandler)

        try:
            juman_result = self.juman.query(input_string, '^EOS', 'juman')
            knp_result = self.knp.query(juman_result, '^EOS', 'knp')
            return (True, knp_result)
        except UnicodeDecodeError:
            traceback_message = traceback.format_exc()
            logger.error("Error with command={}".format(
                traceback.format_exc()))
            return (False, 'error with UnicodeDecodeError traceback={}'.format(
                traceback_message))
        except TimeoutError:
            traceback_message = traceback.format_exc()
            logger.error("Error with command={}".format(
                traceback.format_exc()))
            return (False, 'error with TimeoutErro traceback={}'.format(
                traceback_message))
        except Exception:
            traceback_message = traceback.format_exc()
            logger.error("Error with command={}".format(
                traceback.format_exc()))
            return (False, 'error traceback={}'.format(traceback_message))
Exemplo n.º 3
0
    def __run_pexpect_mode(self, input_string):
        # type: (text_type)->Tuple[bool,text_type]
        """* What you can do
        - It calls Juman in UNIX process.
        - It calls KNP
            - with keep process running
            - with launching KNP command everytime
        """
        assert isinstance(self.juman, UnixProcessHandler)
        try:
            juman_result = self.juman.query(input_string=input_string)
        except:
            return (False, 'error traceback={}'.format(traceback.format_exc()))

        if isinstance(self.knp, SubprocessHandler):
            try:
                parsed_result = self.knp.query(sentence=juman_result.strip(),
                                               eos_pattern='^EOS')
                return (True, parsed_result)
            except:
                traceback_message = traceback.format_exc()
                return (False, 'error traceback={}'.format(traceback_message))
        else:
            # Delete final \n of Juman result document. This \n causes error at KNP #
            echo_process = ["echo", juman_result.strip()]
            try:
                echo_ps = subprocess.Popen(echo_process,
                                           stdout=subprocess.PIPE)
                echo_ps.wait()
                parsed_result = subprocess.check_output(self.knp,
                                                        stdin=echo_ps.stdout)
                if six.PY2:
                    return (True, parsed_result)
                else:
                    return (True, parsed_result.decode('utf-8'))
            except subprocess.CalledProcessError:
                traceback_message = traceback.format_exc()
                logger.error("Error with command={}".format(
                    traceback.format_exc()))
                return (False,
                        'error with CalledProcessError. traceback={}'.format(
                            traceback_message))
            except UnicodeDecodeError:
                traceback_message = traceback.format_exc()
                logger.error("Error with command={}".format(
                    traceback.format_exc()))
                return (False,
                        'error with UnicodeDecodeError traceback={}'.format(
                            traceback_message))
            except Exception:
                traceback_message = traceback.format_exc()
                logger.error("Error with command={}".format(
                    traceback.format_exc()))
                return (False, 'error traceback={}'.format(traceback_message))
Exemplo n.º 4
0
    def __run_server_model(self, input_string):
        # type: (text_type)->Tuple[bool,text_type]
        assert isinstance(self.juman, list)
        assert isinstance(self.knp, list)
        echo_process = ["echo", input_string]
        try:
            echo_ps = subprocess.Popen(echo_process, stdout=subprocess.PIPE)
            echo_ps.wait()
            juman_ps = subprocess.Popen(self.juman,
                                        stdin=echo_ps.stdout,
                                        stdout=subprocess.PIPE)
            juman_ps.wait()
            parsed_result = subprocess.check_output(self.knp,
                                                    stdin=juman_ps.stdout)

            if six.PY2:
                return (True, parsed_result)
            else:
                return (True, parsed_result.decode('utf-8'))
        except subprocess.CalledProcessError:
            traceback_message = traceback.format_exc()
            logger.error("Error with command={}".format(
                traceback.format_exc()))
            return (False,
                    'error with CalledProcessError. traceback={}'.format(
                        traceback_message))
        except UnicodeDecodeError:
            traceback_message = traceback.format_exc()
            logger.error("Error with command={}".format(
                traceback.format_exc()))
            return (False, 'error with UnicodeDecodeError traceback={}'.format(
                traceback_message))
        except Exception:
            traceback_message = traceback.format_exc()
            logger.error("Error with command={}".format(
                traceback.format_exc()))
            return (False, 'error traceback={}'.format(traceback_message))
Exemplo n.º 5
0
    def __init__(self,
                 record_id,
                 text,
                 status,
                 parsed_result=None,
                 is_success=None,
                 sub_id=None,
                 sentence_index=None,
                 timestamp=datetime.now(),
                 updated_at=datetime.now(),
                 document_args=None):
        # type: (int,text_type,bool,Optional[text_type],bool,str,int,datetime,datetime,Dict[str, Any]) -> None
        """

        :param record_id: unique id in backend DB
        :param text: input text to be parsed.
        :param status: boolean flag to describe status of knp parsing.
        :param parsed_result: parsing result text.
        :param is_success: boolean flag to describe status of knp parsing.
        :param sub_id: id in the original given text.
         This is used when the original input text is too long and the original text is separated.
        :param sentence_index: sentence index when the original input text is separated.
        :param timestamp:
        :param updated_at:
        :param document_args: dict object which is attribute information for input document.
        """
        self.record_id = record_id
        self.status = status
        self.timestamp = timestamp
        self.updated_at = updated_at
        self.is_success = is_success
        self.sentence_index = sentence_index
        self.document_args = document_args
        if six.PY2:
            try:
                if isinstance(text, str):
                    self.text = text.decode('utf-8')
                else:
                    self.text = text
            except UnicodeDecodeError:
                logger.error(traceback.format_exc())

            try:
                if isinstance(sub_id, str):
                    self.sub_id = sub_id.decode('utf-8')
                else:
                    self.sub_id = sub_id
            except UnicodeDecodeError:
                logger.error(traceback.format_exc())

            try:
                if isinstance(parsed_result, str):
                    self.parsed_result = parsed_result.decode('utf-8')
                else:
                    self.parsed_result = parsed_result
            except UnicodeDecodeError:
                logger.error(traceback.format_exc())
        else:
            self.text = text
            self.sub_id = sub_id
            self.parsed_result = parsed_result