Project

General

Profile

« Previous | Next » 

Revision 5154

tnrs.py: encode(): Also prepend special padding string to empty and whitespace-only strings because these names are otherwise ignored by TNRS (no response row)

View differences:

lib/tnrs.py
64 64
    return [(r'(?: |(?<=\t)|^)'+re.escape(from_.strip())+r'(?: |(?=\t)|$)',
65 65
        strings.regexp_repl_esc(to)) for from_, to in decode_map]
66 66

  
67
padding = ' !pad ' # prepend to empty and whitespace-only strings
67 68
encode_map = [
68 69
    ('!', ' !exc '), # escape char
69 70
    ('\t', ' !tab '),
......
75 76
    ('\\', ' !bsl '),
76 77
]
77 78
decode_map = strings.flip_map(encode_map)
79
decode_map.append((padding, ''))
78 80
spliced_decode_map = make_spliced_decode_map(decode_map)
79 81

  
80
def encode(str_): return strings.replace_all(encode_map, str_)
82
def encode(str_):
83
    str_ = strings.replace_all(encode_map, str_)
84
    # Empty and whitespace-only strings are ignored by TNRS (no response row)
85
    if str_.strip() == '': str_ = padding+str_
86
    return str_
81 87

  
82 88
def decode(str_): return strings.replace_all_re(spliced_decode_map, str_)
83 89

  

Also available in: Unified diff