]> kaliko git repositories - mpd-sima.git/blobdiff - sima/lib/simastr.py
Removed timestamp for console logging
[mpd-sima.git] / sima / lib / simastr.py
index 56cd2423a0474b540df2c4f010ee89f017585b0e..56edbb4d7e68b82a816ad2d30cb6a99698d78864 100644 (file)
@@ -1,5 +1,4 @@
 # -*- coding: utf-8 -*-
 # -*- coding: utf-8 -*-
-
 #
 # Copyright (c) 2009, 2010, 2013 Jack Kaliko <kaliko@azylum.org>
 #
 #
 # Copyright (c) 2009, 2010, 2013 Jack Kaliko <kaliko@azylum.org>
 #
@@ -18,7 +17,7 @@
 #  If not, see <http://www.gnu.org/licenses/>.
 #
 
 #  If not, see <http://www.gnu.org/licenses/>.
 #
 
-"""
+r"""
 SimaStr
 
 Special unicode() subclass to perform fuzzy match on specific strings with
 SimaStr
 
 Special unicode() subclass to perform fuzzy match on specific strings with
@@ -70,7 +69,7 @@ __version__ = '0.4'
 
 # IMPORTS
 import unicodedata
 
 # IMPORTS
 import unicodedata
-from re import (compile, U, I)
+from re import compile as re_compile, U, I
 
 from ..utils.leven import levenshtein_ratio
 
 
 from ..utils.leven import levenshtein_ratio
 
@@ -80,6 +79,8 @@ class SimaStr(str):
     Specific string object for artist names and song titles.
     Here follows some class variables for regex to run on strings.
     """
     Specific string object for artist names and song titles.
     Here follows some class variables for regex to run on strings.
     """
+    diafilter = True
+    leven_ratio = 0.82
     regexp_dict = dict()
 
     # Leading patterns: The Le Les
     regexp_dict = dict()
 
     # Leading patterns: The Le Les
@@ -92,21 +93,24 @@ class SimaStr(str):
     # Trailing patterns: ! ? live
     # TODO: add "concert" key word
     #       add "Live at <somewhere>"
     # Trailing patterns: ! ? live
     # TODO: add "concert" key word
     #       add "Live at <somewhere>"
-    regexp_dict.update({'trail': '([- !?\.]|\(? ?[Ll]ive ?\)?)'})
+    regexp_dict.update({'trail': r'([- !?\.]|\(? ?[Ll]ive ?\)?)'})
 
 
-    reg_lead = compile('^(?P<lead>%(lead)s )(?P<root0>.*)$' % regexp_dict, I | U)
-    reg_midl = compile('^(?P<root0>.*)(?P<mid> %(mid)s )(?P<root1>.*)' % regexp_dict, U)
-    reg_trail = compile('^(?P<root0>.*?)(?P<trail>%(trail)s+$)' % regexp_dict, U)
+    reg_lead = re_compile('^(?P<lead>%(lead)s )(?P<root0>.*)$' % regexp_dict, I | U)
+    reg_midl = re_compile('^(?P<root0>.*)(?P<mid> %(mid)s )(?P<root1>.*)' % regexp_dict, U)
+    reg_trail = re_compile('^(?P<root0>.*?)(?P<trail>%(trail)s+$)' % regexp_dict, U)
 
     def __init__(self, fuzzstr):
         """
         """
 
     def __init__(self, fuzzstr):
         """
         """
-        super().__init__(fuzzstr)
         self.orig = str(fuzzstr)
         self.stripped = str(fuzzstr.strip())
         # fuzzy computation
         self._get_root()
         self.orig = str(fuzzstr)
         self.stripped = str(fuzzstr.strip())
         # fuzzy computation
         self._get_root()
-        self.remove_diacritics()
+        if self.__class__.diafilter:
+            self.remove_diacritics()
+
+    def __new__(cls, fuzzstr):
+        return super(SimaStr, cls).__new__(cls, fuzzstr)
 
     def _get_root(self):
         """
 
     def _get_root(self):
         """
@@ -129,6 +133,7 @@ class SimaStr(str):
             self.stripped = sea.group('root0')
 
     def remove_diacritics(self):
             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')
         self.stripped = ''.join(x for x in
                                 unicodedata.normalize('NFKD', self.stripped)
                                 if unicodedata.category(x) != 'Mn')
@@ -143,7 +148,7 @@ class SimaStr(str):
                                    other.stripped.lower())
         if hash(self) == hash(other):
             return True
                                    other.stripped.lower())
         if hash(self) == hash(other):
             return True
-        return levenr >= 0.82
+        return levenr >= self.__class__.leven_ratio
 
     def __ne__(self, other):
         if not isinstance(other, SimaStr):
 
     def __ne__(self, other):
         if not isinstance(other, SimaStr):