class LCSstrTestCases(unittest.TestCase): """Test LCSstr functions. abydos.distance.LCSstr """ cmp = LCSstr() def test_lcsstr(self): """Test abydos.distance.LCSstr.lcsstr.""" self.assertEqual(self.cmp.lcsstr('', ''), '') self.assertEqual(self.cmp.lcsstr('A', ''), '') self.assertEqual(self.cmp.lcsstr('', 'A'), '') self.assertEqual(self.cmp.lcsstr('A', 'A'), 'A') self.assertEqual(self.cmp.lcsstr('ABCD', ''), '') self.assertEqual(self.cmp.lcsstr('', 'ABCD'), '') self.assertEqual(self.cmp.lcsstr('ABCD', 'ABCD'), 'ABCD') self.assertEqual(self.cmp.lcsstr('ABCD', 'BC'), 'BC') self.assertEqual(self.cmp.lcsstr('ABCD', 'AD'), 'A') self.assertEqual(self.cmp.lcsstr('ABCD', 'AC'), 'A') self.assertEqual(self.cmp.lcsstr('AB', 'CD'), '') self.assertEqual(self.cmp.lcsstr('ABC', 'BCD'), 'BC') self.assertEqual(self.cmp.lcsstr('DIXON', 'DICKSONX'), 'DI') # https://en.wikipedia.org/wiki/Longest_common_subsequence_problem self.assertEqual(self.cmp.lcsstr('AGCAT', 'GAC'), 'A') self.assertEqual(self.cmp.lcsstr('XMJYAUZ', 'MZJAWXU'), 'X') # https://github.com/jwmerrill/factor/blob/master/basis/lcs/lcs-tests.factor self.assertEqual(self.cmp.lcsstr('hell', 'hello'), 'hell') self.assertEqual(self.cmp.lcsstr('hello', 'hell'), 'hell') self.assertEqual(self.cmp.lcsstr('ell', 'hell'), 'ell') self.assertEqual(self.cmp.lcsstr('hell', 'ell'), 'ell') self.assertEqual(self.cmp.lcsstr('faxbcd', 'abdef'), 'f') # http://www.unesco.org/culture/languages-atlas/assets/_core/php/qcubed_unit_tests.php self.assertEqual(self.cmp.lcsstr('hello world', 'world war 2'), 'world') self.assertEqual(self.cmp.lcsstr('foo bar', 'bar foo'), 'foo') self.assertEqual(self.cmp.lcsstr('aaa', 'aa'), 'aa') self.assertEqual(self.cmp.lcsstr('cc', 'bbbbcccccc'), 'cc') self.assertEqual(self.cmp.lcsstr('ccc', 'bcbb'), 'c') # http://www.maplesoft.com/support/help/Maple/view.aspx?path=StringTools/LongestCommonSubString self.assertEqual(self.cmp.lcsstr('abax', 'bax'), 'bax') self.assertEqual(self.cmp.lcsstr('tsaxbaxyz', 'axcaxy'), 'axy') self.assertEqual(self.cmp.lcsstr('abcde', 'uvabxycde'), 'cde') self.assertEqual(self.cmp.lcsstr('abc', 'xyz'), '') self.assertEqual( self.cmp.lcsstr( 'TAAGGTCGGCGCGCACGCTGGCGAGTATGGTGCGGAGGCCCTGGA\ GAGGTGAGGCTCCCTCCCCTGCTCCGACCCGGGCTCCTCGCCCGCCCGGACCCAC', 'AAGCGCCGCGCAGTCTGGG\ CTCCGCACACTTCTGGTCCAGTCCGACTGAGAAGGAACCACCATGGTGCTGTCTCCCGCTGACAAGACCAACATCAAG\ ACTGCCTGGGAAAAGATCGGCAGCCACGGTGGCGAGTATGGCGCCGAGGCCGT', ), 'TGGCGAGTATGG', ) def test_lcsstr_sim(self): """Test abydos.distance.LCSstr.sim.""" self.assertEqual(self.cmp.sim('', ''), 1) self.assertEqual(self.cmp.sim('A', ''), 0) self.assertEqual(self.cmp.sim('', 'A'), 0) self.assertEqual(self.cmp.sim('A', 'A'), 1) self.assertEqual(self.cmp.sim('ABCD', ''), 0) self.assertEqual(self.cmp.sim('', 'ABCD'), 0) self.assertEqual(self.cmp.sim('ABCD', 'ABCD'), 1) self.assertAlmostEqual(self.cmp.sim('ABCD', 'BC'), 2 / 4) self.assertAlmostEqual(self.cmp.sim('ABCD', 'AD'), 1 / 4) self.assertAlmostEqual(self.cmp.sim('ABCD', 'AC'), 1 / 4) self.assertAlmostEqual(self.cmp.sim('AB', 'CD'), 0) self.assertAlmostEqual(self.cmp.sim('ABC', 'BCD'), 2 / 3) self.assertAlmostEqual(self.cmp.sim('DIXON', 'DICKSONX'), 2 / 8) # https://en.wikipedia.org/wiki/Longest_common_subsequence_problem self.assertAlmostEqual(self.cmp.sim('AGCAT', 'GAC'), 1 / 5) self.assertAlmostEqual(self.cmp.sim('XMJYAUZ', 'MZJAWXU'), 1 / 7) # https://github.com/jwmerrill/factor/blob/master/basis/lcs/lcs-tests.factor self.assertAlmostEqual(self.cmp.sim('hell', 'hello'), 4 / 5) self.assertAlmostEqual(self.cmp.sim('hello', 'hell'), 4 / 5) self.assertAlmostEqual(self.cmp.sim('ell', 'hell'), 3 / 4) self.assertAlmostEqual(self.cmp.sim('hell', 'ell'), 3 / 4) self.assertAlmostEqual(self.cmp.sim('faxbcd', 'abdef'), 1 / 6) # http://www.unesco.org/culture/languages-atlas/assets/_core/php/qcubed_unit_tests.php self.assertAlmostEqual(self.cmp.sim('hello world', 'world war 2'), 5 / 11) self.assertAlmostEqual(self.cmp.sim('foo bar', 'bar foo'), 3 / 7) self.assertAlmostEqual(self.cmp.sim('aaa', 'aa'), 2 / 3) self.assertAlmostEqual(self.cmp.sim('cc', 'bbbbcccccc'), 2 / 10) self.assertAlmostEqual(self.cmp.sim('ccc', 'bcbb'), 1 / 4) def test_lcsstr_dist(self): """Test abydos.distance.LCSstr.dist.""" self.assertEqual(self.cmp.dist('', ''), 0) self.assertEqual(self.cmp.dist('A', ''), 1) self.assertEqual(self.cmp.dist('', 'A'), 1) self.assertEqual(self.cmp.dist('A', 'A'), 0) self.assertEqual(self.cmp.dist('ABCD', ''), 1) self.assertEqual(self.cmp.dist('', 'ABCD'), 1) self.assertEqual(self.cmp.dist('ABCD', 'ABCD'), 0) self.assertAlmostEqual(self.cmp.dist('ABCD', 'BC'), 2 / 4) self.assertAlmostEqual(self.cmp.dist('ABCD', 'AD'), 3 / 4) self.assertAlmostEqual(self.cmp.dist('ABCD', 'AC'), 3 / 4) self.assertAlmostEqual(self.cmp.dist('AB', 'CD'), 1) self.assertAlmostEqual(self.cmp.dist('ABC', 'BCD'), 1 / 3) self.assertAlmostEqual(self.cmp.dist('DIXON', 'DICKSONX'), 6 / 8) # https://en.wikipedia.org/wiki/Longest_common_subsequence_problem self.assertAlmostEqual(self.cmp.dist('AGCAT', 'GAC'), 4 / 5) self.assertAlmostEqual(self.cmp.dist('XMJYAUZ', 'MZJAWXU'), 6 / 7) # https://github.com/jwmerrill/factor/blob/master/basis/lcs/lcs-tests.factor self.assertAlmostEqual(self.cmp.dist('hell', 'hello'), 1 / 5) self.assertAlmostEqual(self.cmp.dist('hello', 'hell'), 1 / 5) self.assertAlmostEqual(self.cmp.dist('ell', 'hell'), 1 / 4) self.assertAlmostEqual(self.cmp.dist('hell', 'ell'), 1 / 4) self.assertAlmostEqual(self.cmp.dist('faxbcd', 'abdef'), 5 / 6) # http://www.unesco.org/culture/languages-atlas/assets/_core/php/qcubed_unit_tests.php self.assertAlmostEqual(self.cmp.dist('hello world', 'world war 2'), 6 / 11) self.assertAlmostEqual(self.cmp.dist('foo bar', 'bar foo'), 4 / 7) self.assertAlmostEqual(self.cmp.dist('aaa', 'aa'), 1 / 3) self.assertAlmostEqual(self.cmp.dist('cc', 'bbbbcccccc'), 8 / 10) self.assertAlmostEqual(self.cmp.dist('ccc', 'bcbb'), 3 / 4)
from abydos.distance import (IterativeSubString, BISIM, DiscountedLevenshtein, Prefix, LCSstr, MLIPNS, Strcmp95, MRA, Editex, SAPS, FlexMetric, JaroWinkler, HigueraMico, Sift4, Eudex, ALINE, PhoneticEditDistance) from abydos.phonetic import PSHPSoundexFirst, Ainsworth from abydos.phones import * import re from sklearn.preprocessing import MinMaxScaler # Featurizer pshp_soundex_first = PSHPSoundexFirst() pe = Ainsworth() iss = IterativeSubString() bisim = BISIM() dlev = DiscountedLevenshtein() prefix = Prefix() lcs = LCSstr() mlipns = MLIPNS() strcmp95 = Strcmp95() mra = MRA() editex = Editex() saps = SAPS() flexmetric = FlexMetric() jaro = JaroWinkler(mode='Jaro') higuera_mico = HigueraMico() sift4 = Sift4() eudex = Eudex() aline = ALINE() phonetic_edit = PhoneticEditDistance() algos = [iss, bisim, dlev, prefix, lcs, mlipns, strcmp95, mra, editex, saps, flexmetric, jaro, higuera_mico, sift4, eudex, aline, phonetic_edit]
def __init__(self, model='latin'): self.model = model self.impH = input_helpers.InputHelper() self.ST = syllable_tokenizer.SyllableTokenizer() # Phonetic Encoder self.pe = Ainsworth() # Soundex Firstname Algorithm self.pshp_soundex_first = PSHPSoundexFirst() # String Distance algorithms self.algos = [ IterativeSubString(), BISIM(), DiscountedLevenshtein(), Prefix(), LCSstr(), MLIPNS(), Strcmp95(), MRA(), Editex(), SAPS(), FlexMetric(), JaroWinkler(mode='Jaro'), HigueraMico(), Sift4(), Eudex(), ALINE(), Covington(), PhoneticEditDistance() ] self.algo_names = [ 'iterativesubstring', 'bisim', 'discountedlevenshtein', 'prefix', 'lcsstr', 'mlipns', 'strcmp95', 'mra', 'editex', 'saps', 'flexmetric', 'jaro', 'higueramico', 'sift4', 'eudex', 'aline', 'covington', 'phoneticeditdistance' ] # extract model tarball into directory if doesnt exist model_dir = os.path.join(os.path.dirname(__file__), "models", self.model) if not os.path.exists(model_dir): os.makedirs(model_dir) tar = tarfile.open( os.path.join(os.path.dirname(__file__), "models", self.model + ".tar.gz"), "r:gz") tar.extractall(model_dir) tar.close() # String Distance Pipeline (Level 0/Base Model) self.baseModel = joblib.load(os.path.join(model_dir, 'base.pkl')) # Character Embedding Network (Level 0/Base Model) self.vocab = preprocess.VocabularyProcessor( max_document_length=15, min_frequency=0).restore(os.path.join(model_dir, 'vocab')) siamese_model = os.path.join(model_dir, 'siamese') graph = tf.Graph() with graph.as_default() as graph: self.sess = tf.Session() with self.sess.as_default(): # Load the saved meta graph and restore variables saver = tf.train.import_meta_graph( '{}.meta'.format(siamese_model)) self.sess.run(tf.global_variables_initializer()) saver.restore(self.sess, siamese_model) # Get the placeholders from the graph by name self.input_x1 = graph.get_operation_by_name('input_x1').outputs[0] self.input_x2 = graph.get_operation_by_name('input_x2').outputs[0] self.dropout_keep_prob = graph.get_operation_by_name( 'dropout_keep_prob').outputs[0] self.prediction = graph.get_operation_by_name( 'output/distance').outputs[0] self.sim = graph.get_operation_by_name( 'accuracy/temp_sim').outputs[0] # Logreg (Level 1/Meta Model) self.metaModel = joblib.load(os.path.join(model_dir, 'meta.pkl')) # seen names (mapping dict from raw name to processed name) self.seen_names = {} # seen pairs (mapping dict from name pair tuple to similarity) self.seen_pairs = {}
def __init__(self, model='latin', prefilter=True, allow_alt_surname=True, allow_initials=True, allow_missing_components=True): # user-provided parameters self.model = model self.allow_alt_surname = allow_alt_surname self.allow_initials = allow_initials self.allow_missing_components = allow_missing_components self.prefilter = prefilter if self.prefilter: self.refined_soundex = { 'b': 1, 'p': 1, 'f': 2, 'v': 2, 'c': 3, 'k': 3, 's': 3, 'g': 4, 'j': 4, 'q': 5, 'x': 5, 'z': 5, 'd': 6, 't': 6, 'l': 7, 'm': 8, 'n': 8, 'r': 9 } # verify user-supplied class arguments model_dir = self.validate_parameters() self.impH = input_helpers.InputHelper() # Phonetic Encoder self.pe = Ainsworth() # Soundex Firstname Algorithm self.pshp_soundex_first = PSHPSoundexFirst() # Soundex Lastname Algorithm self.pshp_soundex_last = PSHPSoundexLast() # String Distance algorithms self.algos = [IterativeSubString(), BISIM(), DiscountedLevenshtein(), Prefix(), LCSstr(), MLIPNS(), Strcmp95(), MRA(), Editex(), SAPS(), FlexMetric(), JaroWinkler(mode='Jaro'), HigueraMico(), Sift4(), Eudex(), ALINE(), CovingtonGuard(), PhoneticEditDistance()] self.algo_names = ['iterativesubstring', 'bisim', 'discountedlevenshtein', 'prefix', 'lcsstr', 'mlipns', 'strcmp95', 'mra', 'editex', 'saps', 'flexmetric', 'jaro', 'higueramico', 'sift4', 'eudex', 'aline', 'covington', 'phoneticeditdistance'] # String Distance Pipeline (Level 0/Base Model) self.baseModel = joblib.load(os.path.join(model_dir, 'base.pkl')) # Character Embedding Network (Level 0/Base Model) self.vocab = preprocess.VocabularyProcessor(max_document_length=15, min_frequency=0).restore( os.path.join(model_dir, 'vocab')) siamese_model = os.path.join(model_dir, 'siamese') # start tensorflow session graph = tf.Graph() with graph.as_default() as graph: self.sess = tf.Session() if tf.__version__[0] == '1' else tf.compat.v1.Session() with self.sess.as_default(): # Load the saved meta graph and restore variables if tf.__version__[0] == '1': saver = tf.train.import_meta_graph('{}.meta'.format(siamese_model)) self.sess.run(tf.global_variables_initializer()) else: saver = tf.compat.v1.train.import_meta_graph('{}.meta'.format(siamese_model)) self.sess.run(tf.compat.v1.global_variables_initializer()) saver.restore(self.sess, siamese_model) # Get the placeholders from the graph by name self.input_x1 = graph.get_operation_by_name('input_x1').outputs[0] self.input_x2 = graph.get_operation_by_name('input_x2').outputs[0] self.dropout_keep_prob = graph.get_operation_by_name('dropout_keep_prob').outputs[0] self.prediction = graph.get_operation_by_name('output/distance').outputs[0] self.sim = graph.get_operation_by_name('accuracy/temp_sim').outputs[0] # Logreg (Level 1/Meta Model) self.metaModel = joblib.load(os.path.join(model_dir, 'meta.pkl')) # seen names (mapping dict from raw name to processed name) self.seen_names = {} # seen pairs (mapping dict from name pair tuple to similarity) self.seen_pairs = {} # user scores (mapping dict from name pair tuple to similarity) self.user_scores = {}