コード例 #1
0
 def test_is_exact(self):
     """Test exact search response of needle in haystack"""
     self.assertTrue(validate.is_exact('abcd', 'abcdefgh', 0, 4, False))
     self.assertFalse(validate.is_exact('abcd', 'abcdefgh', 0, 4, True))
     self.assertFalse(validate.is_exact('bcd', 'abcdefgh', 0, 4, False))
     self.assertTrue(validate.is_exact('bcd', 'abcdefgh', 0, 4, True))
     # Test with substring out of string's range
     self.assertFalse(validate.is_exact('a', 'a', 1, 2, False))
     self.assertTrue(validate.is_exact('a', 'a', 1, 2, True))
コード例 #2
0
 def test_is_exact(self):
     """Test exact search response of needle in haystack"""
     self.assertTrue(validate.is_exact('abcd', 'abcdefgh', 0, 4, False))
     self.assertFalse(validate.is_exact('abcd', 'abcdefgh', 0, 4, True))
     self.assertFalse(validate.is_exact('bcd', 'abcdefgh', 0, 4, False))
     self.assertTrue(validate.is_exact('bcd', 'abcdefgh', 0, 4, True))
     # Test with substring out of string's range
     self.assertFalse(validate.is_exact('a', 'a', 1, 2, False))
     self.assertTrue(validate.is_exact('a', 'a', 1, 2, True))
コード例 #3
0
ファイル: avro.py プロジェクト: souravdatta/pyAvroPhonetic
def process_match(match, fixed_text, cur, cur_end):
    """Processes a single match in rules"""
    # Set our tools
    # -- Initial/default value for replace
    replace = True
    # -- Set check cursor depending on match['type']
    if match['type'] == 'prefix':
        chk = cur - 1
    else:
        # suffix
        chk = cur_end
    # -- Set scope based on whether scope is negative
    if match['scope'].startswith('!'):
        scope = match['scope'][1:]
        negative = True
    else:
        scope = match['scope']
        negative = False

    # Let the matching begin
    # -- Punctuations
    if scope == 'punctuation':
        # Conditions: XORd with negative
        if (not ((chk < 0 and match['type'] == 'prefix') or
                 (chk >= len(fixed_text) and match['type'] == 'suffix') or
                 validate.is_punctuation(fixed_text[chk]))
            ^ negative):
            replace = False
    # -- Vowels -- Checks: 1. Cursor should not be at first character
    # -- if prefix or last character if suffix, 2. Character at chk
    # -- should be a vowel. 3. 'negative' will invert the value of 1
    # -- AND 2
    elif scope == 'vowel':
        if (not (((chk >= 0 and match['type'] == 'prefix') or
                  (chk < len(fixed_text) and match['type'] == 'suffix'))
                 and validate.is_vowel(fixed_text[chk]))
            ^ negative):
            replace =  False
    # -- Consonants -- Checks: 1. Cursor should not be at first
    # -- character if prefix or last character if suffix, 2. Character
    # -- at chk should be a consonant. 3. 'negative' will invert the
    # -- value of 1 AND 2
    elif scope == 'consonant':
        if (not (((chk >= 0 and match['type'] == 'prefix') or
                  (chk < len(fixed_text) and match['type'] == 'suffix'))
                 and validate.is_consonant(fixed_text[chk]))
            ^ negative):
            replace = False
    # -- Exacts
    elif scope == 'exact':
        # Prepare cursor for exact search
        if match['type'] == 'prefix':
            exact_start = cur - len(match['value'])
            exact_end = cur
        else:
            # suffix
            exact_start = cur_end
            exact_end = cur_end + len(match['value'])
        # Validate exact find.
        if not validate.is_exact(match['value'], fixed_text, exact_start,
                                 exact_end, negative):
            replace = False
    # Return replace, which will be true if none of the checks above match
    return replace