|
| 1 | +import logging |
| 2 | +import re |
| 3 | +from typing import Tuple, Optional |
| 4 | + |
| 5 | +from atcodertools.models.constpred.problem_constant_set import ProblemConstantSet |
| 6 | +from bs4 import BeautifulSoup |
| 7 | + |
| 8 | +from atcodertools.models.problem_content import ProblemContent, InputFormatDetectionError, SampleDetectionError |
| 9 | + |
| 10 | + |
| 11 | +class YesNoPredictionFailedError(Exception): |
| 12 | + pass |
| 13 | + |
| 14 | + |
| 15 | +class MultipleModCandidatesError(Exception): |
| 16 | + |
| 17 | + def __init__(self, cands): |
| 18 | + self.cands = cands |
| 19 | + |
| 20 | + |
| 21 | +MOD_ANCHORS = ["余り", "あまり", "mod", "割っ", "modulo"] |
| 22 | + |
| 23 | +MOD_STRATEGY_RE_LIST = [ |
| 24 | + re.compile("([0-9]+).?.?.?で割った"), |
| 25 | + re.compile("modu?l?o?[^0-9]?[^0-9]?[^0-9]?([0-9]+)") |
| 26 | +] |
| 27 | + |
| 28 | + |
| 29 | +def is_mod_context(sentence): |
| 30 | + for kw in MOD_ANCHORS: |
| 31 | + if kw in sentence: |
| 32 | + return True |
| 33 | + return False |
| 34 | + |
| 35 | + |
| 36 | +def predict_modulo(html: str) -> Optional[int]: |
| 37 | + def normalize(sentence): |
| 38 | + return sentence.replace('\\', '').replace("{", "").replace("}", "").replace(",", "").replace(" ", "").replace( |
| 39 | + "10^9+7", "1000000007").lower().strip() |
| 40 | + |
| 41 | + soup = BeautifulSoup(html, "html.parser") |
| 42 | + sentences = soup.get_text().split("\n") |
| 43 | + sentences = [normalize(s) for s in sentences if is_mod_context(s)] |
| 44 | + |
| 45 | + mod_cands = set() |
| 46 | + |
| 47 | + for s in sentences: |
| 48 | + for regexp in MOD_STRATEGY_RE_LIST: |
| 49 | + m = regexp.search(s) |
| 50 | + if m is not None: |
| 51 | + extracted_val = int(m.group(1)) |
| 52 | + mod_cands.add(extracted_val) |
| 53 | + |
| 54 | + if len(mod_cands) == 0: |
| 55 | + return None |
| 56 | + |
| 57 | + if len(mod_cands) == 1: |
| 58 | + return list(mod_cands)[0] |
| 59 | + |
| 60 | + raise MultipleModCandidatesError(mod_cands) |
| 61 | + |
| 62 | + |
| 63 | +def predict_yes_no(html: str) -> Tuple[Optional[str], Optional[str]]: |
| 64 | + try: |
| 65 | + outputs = set() |
| 66 | + for sample in ProblemContent.from_html(html).get_samples(): |
| 67 | + for x in sample.get_output().split("\n"): |
| 68 | + outputs.add(x.strip()) |
| 69 | + except (InputFormatDetectionError, SampleDetectionError) as e: |
| 70 | + raise YesNoPredictionFailedError(e) |
| 71 | + |
| 72 | + yes_kws = ["yes", "possible"] |
| 73 | + no_kws = ["no", "impossible"] |
| 74 | + |
| 75 | + yes_str = None |
| 76 | + no_str = None |
| 77 | + for val in outputs: |
| 78 | + if val.lower() in yes_kws: |
| 79 | + yes_str = val |
| 80 | + if val.lower() in no_kws: |
| 81 | + no_str = val |
| 82 | + |
| 83 | + return yes_str, no_str |
| 84 | + |
| 85 | + |
| 86 | +def predict_constants(html: str) -> ProblemConstantSet: |
| 87 | + try: |
| 88 | + yes_str, no_str = predict_yes_no(html) |
| 89 | + except YesNoPredictionFailedError: |
| 90 | + yes_str = no_str = None |
| 91 | + |
| 92 | + try: |
| 93 | + mod = predict_modulo(html) |
| 94 | + except MultipleModCandidatesError as e: |
| 95 | + logging.warning("Modulo prediction failed -- " |
| 96 | + "two or more candidates {} are detected as modulo values".format(e.cands)) |
| 97 | + mod = None |
| 98 | + |
| 99 | + return ProblemConstantSet(mod=mod, yes_str=yes_str, no_str=no_str) |
0 commit comments