plint

French poetry validator (local mirror of https://gitlab.com/a3nm/plint)
git clone https://a3nm.net/git/plint/
Log | Files | Refs | README

commit 851516e4d8356ff002fa8ca4f4859aa5ee99c6a2
parent 3f9d9be40d8b538554d00b46dc3a00bcd6ece05e
Author: Antoine Amarilli <a3nm@a3nm.net>
Date:   Sun, 10 Jul 2011 21:53:33 -0400

refactoring

Diffstat:
error.py | 109+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
poetlint.py | 116+++----------------------------------------------------------------------------
2 files changed, 113 insertions(+), 112 deletions(-)

diff --git a/error.py b/error.py @@ -0,0 +1,109 @@ + +class Error: + def __init__(self): + self.line = None + self.line_no = None + self.pattern = None + self.prefix = None + + def pos(self, line, line_no, pattern): + self.line = line + self.line_no = line_no + self.pattern = pattern + self.prefix = "stdin:%d: " % self.line_no + + def say(self, l): + print(self.prefix + l) + + def report(self, s, t = []): + self.say("error: %s" % (s)) + self.say("Line is: %s" % (self.line)) + for l in t: + self.say(l) + +class ErrorBadRhyme(Error): + def __init__(self, expected, inferred): + Error.__init__(self) + self.expected = expected + self.inferred = inferred + + def report(self): + Error.report(self, "Bad rhyme %s for type %s (expected %s, inferred %s)" + % (self.kind, self.pattern.myid, self.fmt(self.expected), + self.fmt(self.inferred))) + +class ErrorBadRhymeGenre(ErrorBadRhyme): + def fmt(self, l): + return ' or '.join(list(l)) + + @property + def kind(self): + return "genre" + +class ErrorBadRhymeSound(ErrorBadRhyme): + def fmt(self, l): + pron, spel, constraint = l + ok = [] + if len(pron) > 0: + ok.append("") + + + def report(self): + Error.report(self, "Bad rhyme %s for type %s (expected %s)" + % (self.kind, self.pattern.myid, self.fmt(self.expected))) + + @property + def kind(self): + return "value" + +class ErrorBadMetric(Error): + def __init__(self, possible): + Error.__init__(self) + self.possible = possible + + def align(self, align): + score, align = align + align, feminine, hemis = align + line = self.line + l2 = [] + count = 0 + ccount = 0 + summary = [] + done = False + for x in [''] + align: + if isinstance(x, tuple): + orig = "" + while len(line) > 0 and is_vowels(line[0]): + orig += line[0] + line = line[1:] + l2 += ('{:^'+str(len(orig))+'}').format(str(x[1])) + count += x[1] + ccount += x[1] + done = False + else: + orig = "" + while len(line) > 0 and not is_vowels(line[0]): + orig += line[0] + line = line[1:] + if count in hemis.keys() and not done: + done = True + summary.append(str(ccount)) + ccount = 0 + summary.append(hemis_types[hemis[count]]) + l2 += ('{:^'+str(len(orig))+'}' + ).format(hemis_types[hemis[count]]) + else: + l2 += ' ' * len(orig) + summary.append(str(ccount)) + result = ''.join(l2) + summary = ('{:^9}').format(''.join(summary)) + return summary + result + + def report(self): + num = min(len(self.possible), 4) + Error.report( + self, + ("Bad metric (expected %s, inferred %d options)" % + (self.pattern.metric, num)), + list(map(self.align, self.possible[:num]))) + diff --git a/poetlint.py b/poetlint.py @@ -5,6 +5,7 @@ import sys import unicodedata import haspirater import rhyme +import error from pprint import pprint from vowels import possible_weights from common import strip_accents, normalize, is_vowels, consonants, \ @@ -20,7 +21,6 @@ def annotate_aspirated(word): else: return word - def fit(chunks, pos, left): if pos >= len(chunks): return [[]] @@ -132,114 +132,6 @@ def parse(text, bound): return list(map((lambda x : (x, feminine(x, original_text))), fit(chunks, 0, bound))) -class Error: - def __init__(self): - self.line = None - self.line_no = None - self.pattern = None - self.prefix = None - - def pos(self, line, line_no, pattern): - self.line = line - self.line_no = line_no - self.pattern = pattern - self.prefix = "stdin:%d: " % self.line_no - - def say(self, l): - print(self.prefix + l) - - def report(self, s, t = []): - self.say("error: %s" % (s)) - self.say("Line is: %s" % (self.line)) - for l in t: - self.say(l) - -class ErrorBadRhyme(Error): - def __init__(self, expected, inferred): - Error.__init__(self) - self.expected = expected - self.inferred = inferred - - def report(self): - Error.report(self, "Bad rhyme %s for type %s (expected %s, inferred %s)" - % (self.kind, self.pattern.myid, self.fmt(self.expected), - self.fmt(self.inferred))) - -class ErrorBadRhymeGenre(ErrorBadRhyme): - def fmt(self, l): - return ' or '.join(list(l)) - - @property - def kind(self): - return "genre" - -class ErrorBadRhymeSound(ErrorBadRhyme): - def fmt(self, l): - pron, spel, constraint = l - ok = [] - if len(pron) > 0: - ok.append("") - - - def report(self): - Error.report(self, "Bad rhyme %s for type %s (expected %s)" - % (self.kind, self.pattern.myid, self.fmt(self.expected))) - - @property - def kind(self): - return "value" - -class ErrorBadMetric(Error): - def __init__(self, possible): - Error.__init__(self) - self.possible = possible - - def align(self, align): - score, align = align - align, feminine, hemis = align - line = self.line - l2 = [] - count = 0 - ccount = 0 - summary = [] - done = False - for x in [''] + align: - if isinstance(x, tuple): - orig = "" - while len(line) > 0 and is_vowels(line[0]): - orig += line[0] - line = line[1:] - l2 += ('{:^'+str(len(orig))+'}').format(str(x[1])) - count += x[1] - ccount += x[1] - done = False - else: - orig = "" - while len(line) > 0 and not is_vowels(line[0]): - orig += line[0] - line = line[1:] - if count in hemis.keys() and not done: - done = True - summary.append(str(ccount)) - ccount = 0 - summary.append(hemis_types[hemis[count]]) - l2 += ('{:^'+str(len(orig))+'}' - ).format(hemis_types[hemis[count]]) - else: - l2 += ' ' * len(orig) - summary.append(str(ccount)) - result = ''.join(l2) - summary = ('{:^9}').format(''.join(summary)) - return summary + result - - def report(self): - num = min(len(self.possible), 4) - Error.report( - self, - ("Bad metric (expected %s, inferred %d options)" % - (self.pattern.metric, num)), - list(map(self.align, self.possible[:num]))) - class Pattern: def __init__(self, metric, myid, femid, rhyme): self.metric = metric @@ -305,7 +197,7 @@ class Template: errors = [] if len(possible) == 0 or possible[0][0] != 0: - errors.append(ErrorBadMetric(possible)) + errors.append(error.ErrorBadMetric(possible)) if len(possible) == 0: return errors, pattern possible2 = [] @@ -330,7 +222,7 @@ class Template: #pprint(self.env[pattern.myid]) if (self.env[pattern.myid][1] == None and len(self.env[pattern.myid][0]) == 0): - errors.append(ErrorBadRhymeSound(old, None)) + errors.append(error.ErrorBadRhymeSound(old, None)) if pattern.femid not in self.femenv.keys(): if pattern.femid == 'M': x = set(['M']) @@ -351,7 +243,7 @@ class Template: #print(old) #print(new) if len(self.femenv[pattern.femid]) == 0: - errors.append(ErrorBadRhymeGenre(old, new)) + errors.append(error.ErrorBadRhymeGenre(old, new)) return errors, pattern