plint

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

commit e6479d57ec77be0354af7ac9f4aae250f8a17785
parent b86304dba549bdfad13930b6a64192f0632e409a
Author: Antoine Amarilli <a3nm@a3nm.net>
Date:   Sun, 10 Jul 2011 22:40:19 -0400

change rhyme specification format

Diffstat:
common.py | 2+-
error.py | 7++++++-
template.py | 37+++++++++++++++++++------------------
test/boileau.tpl | 8++++----
test/genre.tpl | 8++++----
5 files changed, 34 insertions(+), 28 deletions(-)

diff --git a/common.py b/common.py @@ -9,7 +9,7 @@ consonants = "[bcçdfghjklmnpqrstvwxz*-]" # Forbidden at the end of a hemistiche. "-ent" would also be forbidden # in some cases but not others... -sure_end_fem = ['es', 'e'] +sure_end_fem = ['es', 'e', 'ë'] # http://stackoverflow.com/questions/517923/what-is-the-best-way-to-remove-accents-in-a-python-unicode-string def strip_accents_one(s, with_except): diff --git a/error.py b/error.py @@ -29,13 +29,16 @@ class ErrorBadRhyme(Error): 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.kind, self.get_id(), self.fmt(self.expected), self.fmt(self.inferred))) class ErrorBadRhymeGenre(ErrorBadRhyme): def fmt(self, l): return ' or '.join(list(l)) + def get_id(self): + return self.pattern.femid + @property def kind(self): return "genre" @@ -47,6 +50,8 @@ class ErrorBadRhymeSound(ErrorBadRhyme): if len(pron) > 0: ok.append("") + def get_id(self): + return self.pattern.myid def report(self): Error.report(self, "Bad rhyme %s for type %s (expected %s)" diff --git a/template.py b/template.py @@ -38,7 +38,7 @@ class Template: line = line.strip() self.pattern_line_no += 1 if line != '' and line[0] != '#': - self.template.append(self.parse_template(line.lstrip().rstrip())) + self.template.append(self.parse_line(line.lstrip().rstrip())) def count(self, align): """total weight of an align""" @@ -94,7 +94,12 @@ class Template: errors.append(error.ErrorBadRhymeSound(old, None)) # rhyme genres - # TODO refactor this + # inequality constraint + # TODO this is simplistic and order-dependent + if pattern.femid.swapcase() in self.femenv.keys(): + new = set(['M', 'F']) - self.femenv[pattern.femid.swapcase()] + if len(new) > 0: + self.femenv[pattern.femid] = new if pattern.femid not in self.femenv.keys(): if pattern.femid == 'M': x = set(['M']) @@ -104,12 +109,6 @@ class Template: x = set(['M', 'F']) self.femenv[pattern.femid] = x else: - # TODO this is simplistic and order-dependent - if pattern.femid.swapcase() in self.femenv.keys(): - new = set(['M', 'F']) - self.femenv[pattern.femid.swapcase()] - if len(new) > 0: - self.femenv[pattern.femid] = new - old = list(self.femenv[pattern.femid]) new = list(set(['F' if x[1] else 'M' for (score, x) in possible])) self.femenv[pattern.femid] &= set(new) @@ -118,20 +117,21 @@ class Template: return errors, pattern - def parse_template(self, l): - """Parse template from a line""" + def parse_line(self, l): + """Parse template line from a line""" split = l.split(' ') metric = split[0] if len(split) >= 2: - myid = split[1] + femid = split[1] else: - myid = str(self.pattern_line_no) + femid = str(self.pattern_line_no) # unique if len(split) >= 3: - femid = split[2] + myid = split[2] else: - femid = str(self.pattern_line_no) - if len(split) >= 4: - rhyme = [int(x) for x in split[3].split('|')] + myid = str(self.pattern_line_no) # unique + idsplit = myid.split(':') + if len(idsplit) >= 2: + rhyme = [int(x) for x in idsplit[-1].split('|')] else: rhyme = [] if len(rhyme) == 0: @@ -141,10 +141,10 @@ class Template: return Pattern(metric, myid, femid, rhyme) def reset_conditional(self, d): - return dict((k, v) for x, v in d.items() if x[-1] == '!') + return dict((k, v) for k, v in d.items() if k[0] == '!') def reset_state(self, with_femenv=False): - """Reset our state, except ids ending with '!'""" + """Reset our state, except ids starting with '!'""" self.position = 0 self.env = self.reset_conditional(self.env) self.femenv = self.reset_conditional(self.femenv) @@ -167,6 +167,7 @@ class Template: #possible = sorted(possible, key=rate) errors, pattern = self.match(line) for error in errors: + # update errors with line position and pattern error.pos(line, self.line_no, pattern) return errors diff --git a/test/boileau.tpl b/test/boileau.tpl @@ -1,4 +1,4 @@ -6/6 A X 1|2 -6/6 A X 1|2 -6/6 B Y 1|2 -6/6 B Y 1|2 +6/6 !X A:1|2 +6/6 !X A:1|2 +6/6 !x B:1|2 +6/6 !x B:1|2 diff --git a/test/genre.tpl b/test/genre.tpl @@ -1,4 +1,4 @@ -1 A X -1 A x -1 A x -1 A X +1 X A +1 x A +1 x A +1 X A