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
def _exec_internal(self, src: (TextList, list, tuple), person_names: list) -> TokenList: LOG.debug(f'-- src: {src}') tmp = [] def _excepted(target: str): return target in ('EOS', '', 't', 'ー') def _is_exists_name(target: str): for name in person_names: if name == target: return True return False _src = src.data if isinstance(src, TextList) else src parsed = self._tagger.parse('\n'.join( assertion.is_listlike(_src))).split('\n') tokens = self._packed_from_parsed(parsed) for token in tokens: if _excepted(token[0]): continue elif len(token) == 1: continue if token[1] == WordClass.NOUN.conv_str(): if _is_exists_name(token[0]): token[3] = '人名' tmp.append(MecabData.conv(*token)) return TokenList(*tmp)
def has_rubi_exclusions(self, src: str, ex_words: (tuple, list)) -> bool: ''' Check whether the string has a exclusion rubi-words. ''' for word in assertion.is_listlike(ex_words): if assertion.is_str(word) in assertion.is_str(src): return True return False
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
def __init__(self, title: str): LOG.info('CONFIG: initialize') LOG.debug(f'-- title: {title}') # for specific self._title = assertion.is_str(title) self._copy = assertion.is_str('__catch_copy__') self._oneline = assertion.is_str('__one_line__') self._outline = assertion.is_str('__story_outline__') self._theme = assertion.is_str('__theme__') self._genre = assertion.is_str('__genre__') self._target = assertion.is_str('__target_age__') self._size = assertion.is_str('__size__') self._desc_size = assertion.is_int(0) self._total_size = assertion.is_int(0) self._desc_papers = assertion.is_int_or_float(0) self._total_papers = assertion.is_int_or_float(0) # for story self._priority = assertion.is_int(__PRIORITY_DEFAULT__) self._start = assertion.is_int(0) self._end = assertion.is_int(-1) self._base_date = datetime.date(2020, 1, 1) self._base_time = datetime.time(12, 00) self._version = assertion.is_tuple((0, 0, 1)) self._columns = assertion.is_int(20) self._rows = assertion.is_int(20) self._contest_info = assertion.is_str('') self._caution = assertion.is_str('') self._note = assertion.is_str('') self._sites = assertion.is_listlike([]) self._taginfos = assertion.is_listlike([]) self._modified = datetime.date.today() self._released = datetime.date(2020, 1, 1) # for compile self._is_data = assertion.is_bool(False) self._is_plot = assertion.is_bool(False) self._is_text = assertion.is_bool(False) self._is_scenario = assertion.is_bool(False) self._is_audiodrama = assertion.is_bool(False) self._is_rubi = assertion.is_bool(False) self._is_comment = assertion.is_bool(False) self._is_console = assertion.is_bool(False) self._format_mode = assertion.is_instance(FormatMode.DEFAULT, FormatMode) self._output_mode = assertion.is_instance(OutputMode.FILE, OutputMode) self._filename = assertion.is_str('story') self._builddir = assertion.is_str('build') self._log_level = assertion.is_str('warn')
def test_is_listlike(self): data = [ # (val, expect) (True, [1,2,3], [1,2,3]), (True, (1,2), (1,2)), (False, 1, 1), ] validate_with_fail(self, "is_listlike", lambda v,expect: self.assertEqual(assertion.is_listlike(v), expect), data)
def build_db(self, persons: ListLike, stages: ListLike, days: ListLike, times: ListLike, items: ListLike, words: ListLike, rubis: ListLike) -> None: # TODO: 間違ったものの設定時はエラー出す if assertion.is_listlike(persons): self.set_persons(persons) if assertion.is_listlike(stages): self.set_stages(stages) if assertion.is_listlike(days): self.set_days(days) if assertion.is_listlike(times): self.set_times(times) if assertion.is_listlike(items): self.set_items(items) if assertion.is_listlike(words): self.set_words(words) if assertion.is_listlike(rubis): self.set_rubis(rubis)
def _packed_from_parsed(self, src: list) -> tuple: return (re.split('[\t,]', v) for v in assertion.is_listlike(src))
def _set_objects(self, data: ListLike, func: Callable) -> None: for line in assertion.is_listlike(data): func(line[0], *line[1:])
def script_relieved_symbols(self, src: (list, tuple)) -> list: return list(val for val in assertion.is_listlike(src) if not ('&' == val or '#' in val))
def script_relieved_strings(self, src: (list, tuple)) -> list: return list(val for val in assertion.is_listlike(src) if isinstance(val, (int, str)))
def to_dialogue(self, src: (list, tuple), brackets: tuple=('「', '」')) -> str: _ = "。".join(assertion.is_listlike(src)) return validate_string_duplicate_chopped(f'{brackets[0]}{_}{brackets[1]}')
def to_description(self, src: (list, tuple)) -> str: _ = "。".join(assertion.is_listlike(src)) return validate_string_duplicate_chopped(f'{_}。')
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)