Python Forum
super simple Ab translation function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
super simple Ab translation function
#1
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.
Reply
#2
May I suggest to convert the tests to unittest ?
Reply
#3
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".)
Reply
#4
(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))
Reply
#5
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.
Reply
#6
(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 ?
Reply
#7
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". :)
Reply
#8
(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 ?
Reply
#9
I'll do proper unit testing on it and get back to you.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020