commit 9cd5bdc3d03b05d128272e0ce323e1460eec8878
parent f726bfffe714b25118092cb16b9e7360d7c12232
Author: Antoine Amarilli <a3nm@a3nm.net>
Date: Tue, 10 Jul 2012 22:07:12 +0200
make ErrorBadCharacters actually work
Diffstat:
3 files changed, 17 insertions(+), 16 deletions(-)
diff --git a/common.py b/common.py
@@ -50,8 +50,7 @@ def rm_punct(text):
#TODO rather: keep only good chars
pattern = re.compile("[^'\w -]", re.UNICODE)
text2 = pattern.sub(' ', text)
- pattern2 = re.compile("[0-9]", re.UNICODE)
- return pattern2.sub('', text2)
+ return text2
def is_vowels(chunk, with_h=False, with_y=True):
"""Test if a chunk is vowels
diff --git a/error.py b/error.py
@@ -39,8 +39,9 @@ class ErrorBadCharacters(Error):
self.characters = characters
def report(self, short=False):
- return Error.report(self, "Illegal character: %s"
- % ', '.join(["'" + a + "'" for a in self.characters]), short)
+ return Error.report(self, "Illegal character%s: %s"
+ % ('' if len(self.characters) == 1 else 's',
+ ', '.join(["'" + a + "'" for a in self.characters])), short)
class ErrorForbiddenPattern(Error):
def __init__(self, pattern):
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, legal, strip_accents_one
+from common import normalize, legal, strip_accents_one, rm_punct
from nature import nature_count
from vowels import possible_weights_ctx, make_query
@@ -115,11 +115,20 @@ class Template:
def match(self, line, ofile=None):
"""Check a line against current pattern, return errors"""
- line_with_case = normalize(line, downcase=False)
- line = normalize(line)
+ errors = []
pattern = self.get()
- errors = []
+ # check characters
+ illegal = set()
+ for x in line:
+ if not rm_punct(strip_accents_one(x)[0].lower()) in legal:
+ illegal.add(x)
+ if len(illegal) > 0:
+ errors.append(error.ErrorBadCharacters(illegal))
+ return errors, pattern
+
+ line_with_case = normalize(line, downcase=False)
+ line = normalize(line)
# rhymes
if pattern.myid not in self.env.keys():
@@ -153,14 +162,6 @@ class Template:
possible = map((lambda x: (self.rate(pattern, x), x)), possible)
possible = sorted(possible, key=(lambda x: x[0]))
- # 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))