Ejemplo n.º 1
0
    def execute(self, src: (str, list, TextList),
            person_names: list,
            is_debug: bool=False) -> ResultData: # pragma: no cover
        LOG.info('ANALYZER: start exec')
        is_succeeded = True
        error = None
        basesrc = None
        result = ResultData([], is_succeeded, error)

        if isinstance(src, str):
            basesrc = TextList(*get_content_from_text_file(src))
        elif isinstance(src, TextList):
            basesrc = src
        elif isinstance(src, (list, tuple)):
            basesrc = TextList(*src)
        else:
            msg = f'Invalid analyze source!: {src}'
            LOG.critical(msg)
            return ResultData(result, False, AnalyzerError(msg))

        tmp = self._rid_tag(basesrc)

        LOG.info('TOKENIZER: call')
        result = assertion.is_instance(Tokenizer().execute(tmp, person_names), ResultData)
        if not result.is_succeeded:
            return result
        tokens = assertion.is_instance(result.data, TokenList)

        LOG.info('WORD_ANALYZER: call')
        result = assertion.is_instance(WordAnalyzer().execute(tokens), ResultData)
        if not result.is_succeeded:
            return result
        word_data = assertion.is_listlike(result.data)

        LOG.info('PERCENT_ANALYZER: call')
        result = assertion.is_instance(PercentAnalyzer().execute(tmp), ResultData)
        if not result.is_succeeded:
            return result
        percent_data = assertion.is_listlike(result.data)

        LOG.info('FREQUENCY_ANALYZER: call')
        result = assertion.is_instance(FrequencyAnalyzer().execute(tokens), ResultData)
        if not result.is_succeeded:
            return result
        freq_data = assertion.is_listlike(result.data)

        LOG.info('Analyzer result output')
        result_data = percent_data + ['\n---\n'] \
                + word_data + ['\n---\n'] \
                + freq_data
        fname = 'result'
        suffix = ''
        extention = 'md'
        builddir = 'build/results'
        mode = OutputMode.CONSOLE if is_debug else OutputMode.FILE
        data = TextList(*[f'{line}\n' for line in result_data])
        Outputter().execute(data, mode, fname, suffix, extention, builddir)
        return result
Ejemplo n.º 2
0
 def execute(self, src: Story, config: StoryConfig) -> ResultData:
     LOG.info('REDUCER: start exec')
     is_succeeded = True
     error = None
     tmp = assertion.is_instance(
         self._exec_internal(src, config.start, config.end), Story)
     return ResultData(tmp, is_succeeded, error)
Ejemplo n.º 3
0
    def _analyze_and_output(self, src: Story, person_names: list,
                            is_debug: bool) -> ResultData:
        # serialize and compile as text
        mode = CompileMode.NOVEL_TEXT
        fmode = FormatMode.DEFAULT
        LOG.info('Serialize for Analyzer')
        result = assertion.is_instance(Serializer().execute(src, mode),
                                       ResultData)
        if not result.is_succeeded:
            return result
        tmp = assertion.is_instance(result.data, CodeList)
        LOG.info('Validate for Analyzer')
        result = assertion.is_instance(Validater().execute(tmp), ResultData)
        if not result.is_succeeded:
            return result
        tmp = assertion.is_instance(result.data, CodeList)
        LOG.info('Compile for Analyzer')
        result = assertion.is_instance(
            Compiler().execute(tmp, mode, {}, False, False), ResultData)
        if not result.is_succeeded:
            return result
        tmp = assertion.is_instance(result.data, RawData)
        LOG.info('Format for Analyzer')
        result = assertion.is_instance(Formatter().execute(tmp, fmode),
                                       ResultData)
        if not result.is_succeeded:
            return result
        tmp = assertion.is_instance(result.data, TextList)

        LOG.info('RUN: call Analyzer')
        result = Analyzer().execute(tmp, person_names, is_debug)
        return ResultData([], True, None)
Ejemplo n.º 4
0
    def _output(self, src: list, config: StoryConfig) -> ResultData:
        LOG.info('RUN: OUTPUT: start')
        assertion.is_instance(config, StoryConfig)

        result = ResultData(src, True, None)
        prefixs = assertion.is_valid_length(
            ['', '_p', '', '_data', '_sc', '_ad'], len(CompileMode.get_all()))
        extentions = assertion.is_valid_length(
            ['md', 'md', 'txt', 'md', 'md', 'md'], len(CompileMode.get_all()))
        fmt_idx = 0
        outputter = Outputter()

        for fmt_data in assertion.is_listlike(src):
            if fmt_data:
                LOG.info(f'RUN: START: Outputter [{fmt_idx}]')
                result = assertion.is_instance(
                    outputter.execute(fmt_data, config.output_mode,
                                      config.filename, prefixs[fmt_idx],
                                      extentions[fmt_idx], config.builddir),
                    ResultData)
                if not result.is_succeeded:
                    LOG.error(f'Failure in Outputter [{fmt_idx}]!!')
                    return result
                LOG.info(f'... SUCCESS: Outputter [{fmt_idx}]')
            fmt_idx += 1

        return result
Ejemplo n.º 5
0
 def execute(self, src: Story) -> ResultData:
     LOG.info('PLOT_UPDATER: start exec')
     tmp, is_succeeded, error = assertion.is_tuple(self._exec_internal(src))
     return ResultData(
             assertion.is_instance(tmp, Story),
             is_succeeded,
             error)
Ejemplo n.º 6
0
    def execute(self, src: RawData, mode: FormatMode) -> ResultData:
        LOG.info(f'FORMATTER: start exec on [{mode}] mode')
        LOG.debug(f'-- src: {src}')

        is_succeeded = True
        tmp = []
        error = None

        if mode is FormatMode.DEFAULT:
            tmp = assertion.is_instance(self._to_default(src), TextList)
        elif mode is FormatMode.PLAIN:
            tmp = src
        elif mode is FormatMode.WEB:
            tmp = assertion.is_instance(self._to_web(src), TextList)
        elif mode is FormatMode.SMARTPHONE:
            tmp = src
        else:
            msg = f'Invalid format-mode! {mode}'
            LOG.critical(msg)
            is_succeeded = False
            error = FormatModeError(msg)
        return ResultData(
                tmp,
                is_succeeded,
                error)
Ejemplo n.º 7
0
 def execute(self, src: Story, priority: int) -> ResultData:
     LOG.info('FILTER: start exec')
     tmp, is_succeeded = self._exec_internal(src, priority)
     error = None
     return ResultData(
             tmp,
             is_succeeded,
             error)
 def execute(self, src: Story, config: StoryConfig,
             tags: dict) -> ResultData:
     LOG.info('TAG_REPLACER: start exec')
     LOG.debug(f'-- src: {src}')
     LOG.debug(f'-- config: {config}')
     LOG.debug(f'-- tags: {tags}')
     is_succeeded = True
     error = None
     tmp = self._exec_internal(src, dict_sorted(tags, True))
     is_succeeded = self._config_data_replaced(config,
                                               dict_sorted(tags, True))
     return ResultData(tmp, is_succeeded, error)
Ejemplo n.º 9
0
    def execute(self, src: CodeList) -> ResultData:
        LOG.info('VALIDATER: start exec')

        is_succeeded = True
        error = None
        ret, is_succeeded = self._has_invalid_symbol(src)

        if not is_succeeded:
            msg = f'Invalid symbol: {ret}'
            LOG.error(msg)
            error = InvalidSymbolError(msg)
        return ResultData(ret, is_succeeded, error)
Ejemplo n.º 10
0
    def execute(
        self,
        src: Story,
        config: StoryConfig,
        db: Database,
    ) -> ResultData:  # pragma: no cover
        ''' Exec story building, compiling and outputting.

        NOTE: Compile option
            1. normal: output `story.md`
            2. plot: output `plot.md`
            3. text: output `story.txt`
            4. scenario: output `sc_story.md`
        '''
        LOG.info('RUN: == START EXEC ==')
        LOG.info('RUN: START-PHASE: Preparation')

        tmp = assertion.is_instance(src, Story)
        is_succeeded = True
        result = None
        error = None

        is_succeeded = self._build_options(config)
        if not is_succeeded:
            msg = 'Cannot build option arguments!!'
            error = RunnerError(msg)
            LOG.error(msg)
            return ResultData([], is_succeeded, error)
        LOG.info('... SUCCESS: Preparation')

        LOG.info('RUN: START-PHASE: Pre-Compile')
        result = self._pre_compile(src, config, db)
        if not result.is_succeeded:
            return result
        tmp = assertion.is_instance(result.data, Story)
        LOG.info('... SUCCESS: Finish: Pre-Compile')

        LOG.info('RUN: START-PHASE: Compile and Output')
        result = assertion.is_instance(self._compile(tmp, config, db),
                                       ResultData)
        LOG.info('... SUCCESS: Finish: Compile and Output')

        self._output_storyinfo(config)

        LOG.info('RUN: analyzer check')
        if self._is_analyzed:
            result = assertion.is_instance(
                self._analyze_and_output(tmp, db.get_person_names(),
                                         self._is_debug), ResultData)

        LOG.info('RUN: == ALL SUCCEEDED ==')
        return result
Ejemplo n.º 11
0
 def execute(self,
             src: (TextList, list, tuple),
             person_names: list,
             mecab_dir: str = None) -> ResultData:
     LOG.info('TOKENIZER: start exec')
     is_succeeded = True
     error = None
     if not self._tagger:
         mdir = self._get_mecab_dir(mecab_dir)
         mdir = f'-d {mdir}' if mdir else ''
         self._tagger = MeCab.Tagger(mdir)
         self._tagger.parse('')  # 初期化処理
     tmp = assertion.is_instance(self._exec_internal(src, person_names),
                                 TokenList)
     return ResultData(tmp, is_succeeded, error)
Ejemplo n.º 12
0
 def execute(self, src: TextList, mode: OutputMode,
         filename: str, suffix: str, extention: str,
         builddir: str) -> ResultData:
     LOG.info('OUTPUT: start exec')
     is_succeeded = True
     tmp = []
     error = None
     if assertion.is_instance(mode, OutputMode) is OutputMode.CONSOLE:
         is_succeeded = self._out_to_console(src)
     elif mode is OutputMode.FILE:
         is_succeeded = self._out_to_file(src, filename, suffix,
                 extention, builddir)
     else:
         msg = f'Unknown OutputMode!: {mode}'
         LOG.critical(msg)
         error = OutputModeError(msg)
     return ResultData(
             tmp,
             is_succeeded,
             error)
Ejemplo n.º 13
0
    def execute(self,
                src: CodeList,
                mode: CompileMode,
                rubis: dict = None,
                is_rubi: bool = False,
                is_comment: bool = True) -> ResultData:
        LOG.info('COMPILER: start exec')

        is_succeeded = True
        tmp = []
        error = None

        if assertion.is_instance(mode, CompileMode) is CompileMode.NORMAL:
            tmp = assertion.is_instance(self._conv_to_novel(src, is_comment),
                                        RawData)
            if is_rubi:
                tmp = assertion.is_instance(
                    self._add_rubi_on_novel(tmp, rubis), RawData)
        elif mode is CompileMode.PLOT:
            tmp = assertion.is_instance(self._conv_to_plot(src, False),
                                        RawData)
        elif mode is CompileMode.STORY_DATA:
            tmp = assertion.is_instance(self._conv_to_plot(src, True), RawData)
        elif mode is CompileMode.NOVEL_TEXT:
            tmp = assertion.is_instance(self._conv_to_novel(src, is_comment),
                                        RawData)
            tmp = assertion.is_instance(self._conv_to_text(tmp), RawData)
            if is_rubi:
                tmp = assertion.is_instance(
                    self._add_rubi_on_novel(tmp, rubis), RawData)
        elif mode is CompileMode.SCENARIO:
            tmp = src
        elif mode is CompileMode.AUDIODRAMA:
            tmp = src
        else:
            msg = f'Invalid CompileMode!: {mode}'
            LOG.error(msg)
            error = CompileModeError(msg)
        return ResultData(tmp, is_succeeded, error)
Ejemplo n.º 14
0
 def execute(self, src: TokenList) -> ResultData:
     LOG.info('WORD_ANALYZER: start exec')
     is_succeeded = True
     error = None
     tmp = assertion.is_listlike(self._exec_internal(src))
     return ResultData(tmp, is_succeeded, error)
 def execute(self, src: Story) -> ResultData:
     LOG.info('CMT_CONVERTER: start exec')
     is_succeeded = True
     error = None
     tmp = assertion.is_instance(self._exec_internal(src), Story)
     return ResultData(tmp, is_succeeded, error)
Ejemplo n.º 16
0
 def execute(self, src: Story, mode: CompileMode) -> ResultData:
     LOG.info('SERIALIZER: start exec')
     is_succeeded = True
     tmp = CodeList(*self._exec_internal(src, mode))
     error = None
     return ResultData(tmp, is_succeeded, error)