Python Forum
super simple Ab translation function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: Code sharing (https://python-forum.io/forum-5.html)
+--- Thread: super simple Ab translation function (/thread-7528.html)



super simple Ab translation function - league55 - Jan-14-2018

This is just a function that translates text to the Ab language. I've included two tests for it.

def convert_to_ab(text):
    temp = ''
    for char in text:
        if char in ['a', 'e', 'i', 'o', 'u', 'y']:
            temp += ('ab' + char)
        elif char in ['A', 'E', 'I', 'O', 'U', 'Y']:
            temp += ('Ab' + char.lower())
        else:
            temp += char
    return temp

text = "This is a test. Do not adjust your jockstrap. This is only a test."
text2 = "Now is the time for all good men to come to the aid of the Party."
    
print(convert_to_ab(text))
print(convert_to_ab(text2))
The test output:

Thabis abis aba tabest. Dabo nabot abadjabust abyaboabur jabockstrabap. Thabis abis abonlaby aba tabest.
Nabow abis thabe tabimabe fabor aball gaboabod maben tabo cabomabe tabo thabe abaabid abof thabe Pabartaby.



RE: super simple Ab translation function - Gribouillis - Jan-14-2018

May I suggest to convert the tests to unittest ?


RE: super simple Ab translation function - league55 - Jan-14-2018

The real challenge would be developing an algorithm for translating Ab back to English. Any ideas on how I can do that? (The problem I foresee is English words that contain "ab" or start with "Ab".)


RE: super simple Ab translation function - Gribouillis - Jan-14-2018

(Jan-14-2018, 07:34 PM)league55 Wrote: Any ideas on how I can do that?
It doesn't seem very difficult: replace Abx with X and abx with x, from left to right (if x is any vowel!)

Here is a version using module re
import re

def helper(match):
    a, x = match.group(1), match.group(2)
    return x if a == 'a' else x.upper()

def convert_from_ab(text):
    return re.sub(r'([aA])b([aeiouy])', helper, text)

data = [
    "Thabis abis aba tabest. Dabo nabot abadjabust abyaboabur jabockstrabap. Thabis abis abonlaby aba tabest.",
    "Nabow abis thabe tabimabe fabor aball gaboabod maben tabo cabomabe tabo thabe abaabid abof thabe Pabartaby.",
]

for s in data:
    print(convert_from_ab(s))



RE: super simple Ab translation function - league55 - Jan-14-2018

The problem is that some English word contain "ab" or start with "Ab" and your script would remove those parts of the English word as well.


RE: super simple Ab translation function - Gribouillis - Jan-14-2018

(Jan-14-2018, 10:23 PM)league55 Wrote: The problem is that some English word contain "ab" or start with "Ab" and your script would remove those parts of the English word as well.
I don't think so, have you tried it ?


RE: super simple Ab translation function - league55 - Jan-14-2018

For my two test strings your solution works, but what if a string contains the english word "abate"? The translation would change that to "abababatabe" and translating back to English would change it to "ate". :)


RE: super simple Ab translation function - Gribouillis - Jan-14-2018

(Jan-14-2018, 10:31 PM)league55 Wrote: translating back to English would change it to "ate". :)
I don't think so, have you tried it ?


RE: super simple Ab translation function - league55 - Jan-15-2018

I'll do proper unit testing on it and get back to you.