diff --git a/tests.py b/tests.py index 5da101324..598762737 100644 --- a/tests.py +++ b/tests.py @@ -56,6 +56,7 @@ from follow import clear_followers from follow import send_follow_request_via_server from follow import send_unfollow_request_via_server from siteactive import site_is_active +from utils import uninvert_text from utils import get_url_from_post from utils import date_from_string_format from utils import date_utcnow @@ -8584,6 +8585,37 @@ def _test_book_link(base_dir: str): shutil.rmtree(base_dir, ignore_errors=False, onerror=None) +def _test_uninvert(): + print('uninvert') + inverted_text = 'abcdefghijklmnopqrstuvwxyz' + uninverted_text = uninvert_text(inverted_text) + if uninverted_text != inverted_text: + print('inverted: ' + inverted_text) + print('uninverted: ' + uninverted_text) + assert uninverted_text == inverted_text + + inverted_text = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + uninverted_text = uninvert_text(inverted_text) + if uninverted_text != inverted_text: + print('inverted: ' + inverted_text) + print('uninverted: ' + uninverted_text) + assert uninverted_text == inverted_text + + inverted_text = 'ʍǝɹpuɐ' + uninverted_text = uninvert_text(inverted_text) + if uninverted_text != 'andrew': + print('inverted: ' + inverted_text) + print('uninverted: ' + uninverted_text) + assert uninverted_text == 'andrew' + + inverted_text = '˙ʇsǝʇ ɐ sı sıɥ⊥' + uninverted_text = uninvert_text(inverted_text) + if uninverted_text != 'This is a test.': + print('inverted: ' + inverted_text) + print('uninverted: ' + uninverted_text) + assert uninverted_text == 'This is a test.' + + def run_all_tests(): base_dir = os.getcwd() print('Running tests...') @@ -8601,6 +8633,7 @@ def run_all_tests(): _test_checkbox_names() _test_thread_functions() _test_functions() + _test_uninvert() _test_book_link(base_dir) _test_dateformat() _test_is_right_to_left() diff --git a/utils.py b/utils.py index 2c9e241b4..452381744 100644 --- a/utils.py +++ b/utils.py @@ -154,6 +154,83 @@ def get_attributed_to(field) -> str: return None +def uninvert_text(text: str) -> str: + """uninverts inverted text + """ + if len(text) < 4: + return text + + flip_table = { + '\u0021': '\u00A1', + '\u0022': '\u201E', + '\u0026': '\u214B', + '\u002E': '\u02D9', + '\u0033': '\u0190', + '\u0034': '\u152D', + '\u0037': '\u2C62', + '\u003B': '\u061B', + '\u003F': '\u00BF', + '\u0041': '\u2200', + '\u0042': '\u10412', + '\u0043': '\u2183', + '\u0044': '\u25D6', + '\u0045': '\u018E', + '\u0046': '\u2132', + '\u0047': '\u2141', + '\u004A': '\u017F', + '\u004B': '\u22CA', + '\u004C': '\u2142', + '\u004D': '\u0057', + '\u004E': '\u1D0E', + '\u0050': '\u0500', + '\u0051': '\u038C', + '\u0052': '\u1D1A', + '\u0054': '\u22A5', + '\u0055': '\u2229', + '\u0056': '\u1D27', + '\u0059': '\u2144', + '\u005B': '\u005D', + '\u005F': '\u203E', + '\u0061': '\u0250', + '\u0062': '\u0071', + '\u0063': '\u0254', + '\u0064': '\u0070', + '\u0065': '\u01DD', + '\u0066': '\u025F', + '\u0067': '\u0183', + '\u0068': '\u0265', + '\u0069': '\u0131', + '\u006A': '\u027E', + '\u006B': '\u029E', + '\u006C': '\u0283', + '\u006D': '\u026F', + '\u006E': '\u0075', + '\u0072': '\u0279', + '\u0074': '\u0287', + '\u0076': '\u028C', + '\u0077': '\u028D', + '\u0079': '\u028E', + '\u203F': '\u2040', + '\u2234': '\u2235' + } + + matches = 0 + possible_result = '' + for ch_test in text: + ch_result = ch_test + for ch1, ch_inv in flip_table.items(): + if ch_test == ch_inv: + matches += 1 + ch_result = ch1 + break + possible_result = ch_result + possible_result + + result = text + if matches > len(text)/2: + result = possible_result + return result + + def _standardize_text_range(text: str, range_start: int, range_end: int, offset: str) -> str: @@ -209,7 +286,7 @@ def standardize_text(text: str) -> str: offset = char_range[1] text = _standardize_text_range(text, range_start, range_end, offset) - return text + return uninvert_text(text) def remove_eol(line: str):