X-Git-Url: https://git.kaliko.me/?a=blobdiff_plain;f=sima%2Flib%2Fsimastr.py;h=033a994e23f7bea76b99db648570426c8a62e4ad;hb=37dd60538984a3917354b794a5c96b0a025f8e95;hp=9dacc45d1ea3af6302a1f56b08c9e6049b117a9b;hpb=e73aeec41c2065c852294253a8ed85a5d0958f76;p=mpd-sima.git diff --git a/sima/lib/simastr.py b/sima/lib/simastr.py index 9dacc45..033a994 100644 --- a/sima/lib/simastr.py +++ b/sima/lib/simastr.py @@ -1,7 +1,6 @@ # -*- coding: utf-8 -*- - # -# Copyright (c) 2009, 2010, 2013 Jack Kaliko +# Copyright (c) 2009, 2010, 2013 kaliko # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as @@ -18,7 +17,7 @@ # If not, see . # -""" +r""" SimaStr Special unicode() subclass to perform fuzzy match on specific strings with @@ -70,7 +69,7 @@ __version__ = '0.4' # IMPORTS import unicodedata -from re import (compile, U, I) +from re import compile as re_compile, U, I from ..utils.leven import levenshtein_ratio @@ -94,11 +93,11 @@ class SimaStr(str): # Trailing patterns: ! ? live # TODO: add "concert" key word # add "Live at " - regexp_dict.update({'trail': '([- !?\.]|\(? ?[Ll]ive ?\)?)'}) + regexp_dict.update({'trail': r'([- !?\.]|\(? ?[Ll]ive ?\)?)'}) - reg_lead = compile('^(?P%(lead)s )(?P.*)$' % regexp_dict, I | U) - reg_midl = compile('^(?P.*)(?P %(mid)s )(?P.*)' % regexp_dict, U) - reg_trail = compile('^(?P.*?)(?P%(trail)s+$)' % regexp_dict, U) + reg_lead = re_compile('^(?P%(lead)s )(?P.*)$' % regexp_dict, I | U) + reg_midl = re_compile('^(?P.*)(?P %(mid)s )(?P.*)' % regexp_dict, U) + reg_trail = re_compile('^(?P.*?)(?P%(trail)s+$)' % regexp_dict, U) def __init__(self, fuzzstr): """ @@ -108,7 +107,7 @@ class SimaStr(str): # fuzzy computation self._get_root() if self.__class__.diafilter: - self.remove_diacritics() + self.remove_diacritics() def __new__(cls, fuzzstr): return super(SimaStr, cls).__new__(cls, fuzzstr) @@ -119,21 +118,19 @@ class SimaStr(str): """ sea = SimaStr.reg_lead.search(self.stripped) if sea: - #print sea.groupdict() self.stripped = sea.group('root0') sea = SimaStr.reg_midl.search(self.stripped) if sea: - #print sea.groupdict() self.stripped = str().join([sea.group('root0'), ' ', sea.group('root1')]) sea = SimaStr.reg_trail.search(self.stripped) if sea: - #print sea.groupdict() self.stripped = sea.group('root0') def remove_diacritics(self): + """converting diacritics""" self.stripped = ''.join(x for x in unicodedata.normalize('NFKD', self.stripped) if unicodedata.category(x) != 'Mn') @@ -156,40 +153,5 @@ class SimaStr(str): return hash(self) != hash(other) -# Script starts here -if __name__ == "__main__": - import time - print(SimaStr('Kétanoue')) - #from leven import levenshtein_ratio - CASES_LIST = list([ - dict({ - 'got': 'Guns N\' Roses (live)!! !', - 'look for': 'Guns And Roses'}), - dict({ - 'got': 'Jesus & Mary Chains', - 'look for': 'The Jesus and Mary Chains - live'}), - dict({ - 'got': 'Desert sessions', - 'look for': 'The Desert Sessions'}), - dict({ - 'got': 'Têtes Raides', - 'look for': 'Les Têtes Raides'}), - dict({ - 'got': 'Noir Désir', - 'look for': 'Noir Désir'}), - dict({ - 'got': 'No Future', - 'look for': 'Future'})]) - - for case in CASES_LIST[:]: - str0 = case.get('got') - str1 = case.get('look for') - fz_str0 = SimaStr(str0) - fz_str1 = SimaStr(str1) - print(fz_str0, '\n', fz_str1) - print(fz_str0.stripped == fz_str1.stripped) - #print levenshtein_ratio(fz_str0.lower(), fz_str1.lower()) - time.sleep(1) - # VIM MODLINE # vim: ai ts=4 sw=4 sts=4 expandtab