Update the German OCR cleanup backend to be a class not a function

This commit is contained in:
Roberto Rosario
2014-07-13 11:32:41 -04:00
parent 7f177c430f
commit 1f3fed2182
+36 -31
View File
@@ -1,43 +1,48 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import absolute_import
import re import re
from . import BackendBase
def check_word(word):
ALL_ALPHANUM = re.compile('([0-9a-zäöüß])', re.I)
NON_ALPHANUM = re.compile('([^0-9a-zäöüß])', re.I)
ALL_ALPHANUM = re.compile('([0-9a-z])', re.I)
NON_ALPHANUM = re.compile('([^0-9a-z])', re.I)
TOO_MANY_VOWELS = re.compile('[aäeioöuü]{4}', re.I) class LanguageBackend(BackendBase):
TOO_MANY_CONSONANTS = re.compile('[bcdfghjklmnpqrstvwxyz]{4}', re.I) def check_word(word):
ALL_ALPHA = re.compile('^[a-z]+$', re.I) ALL_ALPHANUM = re.compile('([0-9a-zäöüß])', re.I)
# SINGLE_LETTER_WORDS = re.compile('^$', re.I) NON_ALPHANUM = re.compile('([^0-9a-zäöüß])', re.I)
ALL_ALPHANUM = re.compile('([0-9a-z])', re.I)
NON_ALPHANUM = re.compile('([^0-9a-z])', re.I)
#(L) If a string is longer than 40 characters, it is considered as garbage TOO_MANY_VOWELS = re.compile('[aäeioöuü]{4}', re.I)
# http://www.duden.de/sprachwissen/sprachratgeber/die-laengsten-woerter-im-dudenkorpus TOO_MANY_CONSONANTS = re.compile('[bcdfghjklmnpqrstvwxyz]{4}', re.I)
# http://www.duden.de/sprachwissen/sprachratgeber/durchschnittliche-laenge-eines-deutschen-wortes ALL_ALPHA = re.compile('^[a-z]+$', re.I)
if len(word) > 40: # SINGLE_LETTER_WORDS = re.compile('^$', re.I)
return None
#(A) If a string's ratio of alphanumeric characters to total #(L) If a string is longer than 40 characters, it is considered as garbage
#characters is less than 50%, the string is garbage # http://www.duden.de/sprachwissen/sprachratgeber/die-laengsten-woerter-im-dudenkorpus
if len(ALL_ALPHANUM.findall(word)) < len(word) / 2: # http://www.duden.de/sprachwissen/sprachratgeber/durchschnittliche-laenge-eines-deutschen-wortes
return None if len(word) > 40:
return None
#Remove word if all the letters in the word are non alphanumeric #(A) If a string's ratio of alphanumeric characters to total
if len(NON_ALPHANUM.findall(word)) == len(word): #characters is less than 50%, the string is garbage
return None if len(ALL_ALPHANUM.findall(word)) < len(word) / 2:
return None
#Removed words with too many consecutie vowels #Remove word if all the letters in the word are non alphanumeric
if TOO_MANY_VOWELS.findall(word): if len(NON_ALPHANUM.findall(word)) == len(word):
return None return None
#Removed words with too many consecutie consonants #Removed words with too many consecutie vowels
if TOO_MANY_CONSONANTS.findall(word): if TOO_MANY_VOWELS.findall(word):
return None return None
# No single letter words in German #Removed words with too many consecutie consonants
if len(word) == 1: if TOO_MANY_CONSONANTS.findall(word):
return None return None
return word # No single letter words in German
if len(word) == 1:
return None
return word