plint

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

commit 9a2696bfebf5df2b3afb376435ff12bc4efb127d
parent 5d9e2db6598a5b2dace0dc57d563def00764e14d
Author: Antoine Amarilli <a3nm@a3nm.net>
Date:   Fri, 27 Apr 2012 19:23:06 +0200

check that all characters are known

Diffstat:
TODO | 2--
common.py | 1+
error.py | 8++++++++
template.py | 17+++++++++++++----
4 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/TODO b/TODO @@ -1,5 +1,3 @@ -standalone numbers - check that there are no more repetitions of the same word for a rhyme than there are different functions for it diff --git a/common.py b/common.py @@ -6,6 +6,7 @@ import re vowels = 'aeiouyœæ' consonants = "bcçdfghjklmnpqrstvwxz" +legal = vowels + consonants + ' -' # a variant of x-sampa such that all french phonemes are one-character SUBSTS = [ diff --git a/error.py b/error.py @@ -34,6 +34,14 @@ class Error: l.append(self.say(x)) return '\n'.join(l) +class ErrorBadCharacters(Error): + def __init__(self, characters): + self.characters = characters + + def report(self): + return Error.report(self, "Illegal character: %s" + % ', '.join(["'" + a + "'" for a in self.characters])) + class ErrorBadRhyme(Error): def __init__(self, expected, inferred): Error.__init__(self) diff --git a/template.py b/template.py @@ -3,7 +3,7 @@ from metric import parse from hemistiches import check_hemistiches import copy import rhyme -from common import normalize +from common import normalize, legal, strip_accents_one class Pattern: def __init__(self, metric, myid, femid, constraint): @@ -63,6 +63,8 @@ class Template: def match(self, line): """Check a line against current pattern, return errors""" + + line = normalize(line) pattern = self.get() # compute alignments, check hemistiches, sort by score possible = parse(line, pattern.length + 2) @@ -73,6 +75,14 @@ class Template: errors = [] + # check characters + illegal = set() + for x in line: + if not strip_accents_one(x)[0] in legal: + illegal.add(x) + if len(illegal) > 0: + errors.append(error.ErrorBadCharacters(illegal)) + # check metric if len(possible) == 0 or possible[0][0] != 0: errors.append(error.ErrorBadMetric(possible)) @@ -85,13 +95,12 @@ class Template: # rhymes if pattern.myid not in self.env.keys(): # initialize the rhyme - self.env[pattern.myid] = rhyme.Rhyme(normalize(line), - pattern.constraint) + self.env[pattern.myid] = rhyme.Rhyme(line, pattern.constraint) else: # update the rhyme old_p = self.env[pattern.myid].phon old_e = self.env[pattern.myid].eye - self.env[pattern.myid].feed(normalize(line), pattern.constraint) + self.env[pattern.myid].feed(line, pattern.constraint) # no more possible rhymes, something went wrong if not self.env[pattern.myid].satisfied(): self.env[pattern.myid].phon = old_p