diff --git a/pycrfsuite/_dumpparser.py b/pycrfsuite/_dumpparser.py index 7f490fe..232e036 100644 --- a/pycrfsuite/_dumpparser.py +++ b/pycrfsuite/_dumpparser.py @@ -48,6 +48,7 @@ def __init__(self): self.result = ParsedDump() def feed(self, line): + # type: (str) -> None # Strip initial ws and line terminator, but allow for ws at the end of feature names. line = line.lstrip().rstrip('\r\n') if not line: @@ -62,18 +63,22 @@ def feed(self, line): getattr(self, 'parse_%s' % self.state)(line) def parse_FILEHEADER(self, line): + # type: (str) -> None m = re.match(r"(\w+): (.*)", line) self.result.header[m.group(1)] = m.group(2) def parse_LABELS(self, line): + # type: (str) -> None m = re.match(r"(\d+): (.*)", line) self.result.labels[m.group(2)] = m.group(1) def parse_ATTRIBUTES(self, line): + # type: (str) -> None m = re.match(r"(\d+): (.*)", line) self.result.attributes[m.group(2)] = m.group(1) def parse_TRANSITIONS(self, line): + # type: (str) -> None m = re.match(r"\(\d+\) (.+) --> (.+): ([+-]?\d+\.\d+)", line) from_, to_ = m.group(1), m.group(2) assert from_ in self.result.labels @@ -81,6 +86,7 @@ def parse_TRANSITIONS(self, line): self.result.transitions[(from_, to_)] = float(m.group(3)) def parse_STATE_FEATURES(self, line): + # type: (str) -> None m = re.match(r"\(\d+\) (.+) --> (.+): ([+-]?\d+\.\d+)", line) attr, label = m.group(1), m.group(2) assert attr in self.result.attributes diff --git a/pycrfsuite/_logparser.py b/pycrfsuite/_logparser.py index d41baee..7d05cb5 100644 --- a/pycrfsuite/_logparser.py +++ b/pycrfsuite/_logparser.py @@ -23,6 +23,7 @@ def __init__(self): self.events = [] def feed(self, line): + # type: (str) -> None # if line != '\n': self.log.append(line) if self.state is None: @@ -44,10 +45,12 @@ def last_log(self): return ''.join(self.log[start:end]) def handle_STARTING(self, line): + # type: (str) -> None if line.startswith('Feature generation'): self.state = 'FEATGEN' def handle_FEATGEN(self, line): + # type: (str) -> str if line in "0123456789.10": self.featgen_percent += 2 return 'featgen_progress' @@ -63,6 +66,7 @@ def handle_FEATGEN(self, line): return 'featgen_end' def handle_AFTER_FEATGEN(self, line): + # type: (str) -> str if self._iteration_head(line) is not None: self.state = 'ITERATION' self.handle_ITERATION(line) @@ -73,6 +77,7 @@ def handle_AFTER_FEATGEN(self, line): return 'prepare_error' def handle_ITERATION(self, line): + # type: (str) -> None if self._iteration_head(line) is not None: self.last_iteration = { 'num': self._iteration_head(line), @@ -84,6 +89,7 @@ def handle_ITERATION(self, line): return 'iteration' def add_re(key, pattern, typ): + # type: (str,str,str) -> None m = re.match(pattern, line) if m: self.last_iteration[key] = typ(m.group(1)) @@ -137,6 +143,7 @@ def add_re(key, pattern, typ): }) def handle_AFTER_ITERATION(self, line): + # type: (str) -> None if self._iteration_head(line) is not None: self.state = 'ITERATION' return self.handle_ITERATION(line) @@ -150,17 +157,20 @@ def handle_AFTER_ITERATION(self, line): return 'optimization_end' def handle_STORING(self, line): + # type: (str) -> None if line == '\n': return 'end' elif self._seconds(line): self.storing_seconds = self._seconds(line) def _iteration_head(self, line): + # type: (str) -> None m = re.match(r'\*{5} (?:Iteration|Epoch) #(\d+) \*{5}\n', line) if m: return int(m.group(1)) def _seconds(self, line): + # type: (str) -> float m = re.match(r'Seconds required: (\d+\.\d+)', line) if m: return float(m.group(1))