Translator by C++

How do I do this?

"ARP ARP Talk is a fun variation of the English language. The conversion to ARP ARP is carried out as follows:
1. When a vowel or vowel sound (a, e, i, o, u, or y as in why) is found, arp is placed in front of it.
2. Two or more vowels together are treated as one.
3. If a vowel or vowel sound occurs as the final letter of a word, it is only given an arp if it is the only vowel or vowel sound in the word.
Examples
fish becomes farpish
Harry becomes Harparry
Condition becomes carpondarpitarpion
Problem
Write a program which will convert an English sentence into Arp Arp language.
Input
A string or sentence terminated by the end-of-line character.
Output
The string or sentence converted to arp arp talk.
Input Validation
None. The input will always be a valid English sentence/phrase."

I got stuck in C++ class because of this. Any kind of help would do.

start by maintaining a collection of all of the possible letters or letter-combinations you will be checking for.
iterate over the characters in your test string and check against this collection.
Last edited on
See if you can get your hands on a readable dictionary file, you're going to need it, and one hell of a computer if you plan to translate to lots of languages. But if you find a dictionary file Mutexe's answer is a very good start point.

EDIT:

http://www.cplusplus.com/doc/tutorial/control/ Would be nice to look into to see how to check if else.

Last edited on
does that mean the program has to access an external library file?
no?
I'm not really sure that drakon is trying to say.

try what i suggested. assume it's just the 5 vowels to translate ("vowel sound" is very hazy so i'm ignoring that).

do something like this:

1
2
3
4
5
6
	std::vector<char> compareList;
	compareList.push_back('a');
	compareList.push_back('e');
	compareList.push_back('i');
	compareList.push_back('o');
	compareList.push_back('u');


You now have your list containing stuff you want to look for in your string.

now iterate over the characters in your input string to see if it's there or not (maybe use std::find for this?).
it it's there you know the index of the string where to insert your translation.
Last edited on
You could also use

1
2
3
4
bool isVowel(char ch) {
    static const std::string vowels = "aeiou";
    return (vowels.npos != vowels.find(ch)); // could add tolower here? 
}


Andy
Last edited on
that's a lot nicer :)
mutexe wrote:
"vowel sound" is very hazy so i'm ignoring that
->
SCK15 wrote:
When a vowel or vowel sound (a, e, i, o, u, or y as in why)


[EDIT]
For me that sounds as if 'y' is always treated as a vowel
Last edited on
And there's this

Introduction to American English Semi-vowels
http://www.pronuncian.com/Lessons/default.aspx?Lesson=36

which explains how the two semivowels w and y work.

Andy
Last edited on
mutexe, sorry for my late response, but I meant to save you typing out most of the words you'd have a dictionary file to copy them from with description
@Drakon - The problem description doesn't say anything about needing a dictionary. It says convert the words in a sentence using the algorithin given. The task at hand is to write the needed algorithim.

how do you insert the 'arp'?
If your compiler has a working std::regex implementation or you can use boost.regex, the following might be of interest:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <string>
#include <regex>

std::string arpify(const std::string& str)
{
    std::regex vowels("[aeiou]+");                              
    std::sregex_iterator it(str.begin(), str.end(), vowels);    
    const std::sregex_iterator end;

    // specification requires that we treat y as a vowel if there are no other vowels
    // present in the string.
    std::regex y("y");
    if (it == end)
        it = std::sregex_iterator(str.begin(), str.end(), y);

    std::string result;

    if (it == end)
        result = str;
    else
    {
        while (it != end)
        {
            std::smatch match = *it;

            result += match.prefix().str() + "arp" + match.str();

            if (++it == end)
                result += match.suffix();
        }
    }

    return result ;
}


Last edited on
My interpretation of "vowel sound ... or y as in why", with the other rules, suggests that:

geyser  -> garpeysarper   as y part of ey
prepay  -> prarpepay      as y part of ay at end but not only vowel sound
gray    -> grarpay        as y part of ay at end and only vowel sound
gapyear -> garpapyarpear  as y is distinct
year    -> yarpear        as y is distinct at front

plus a few annoying cases

stylus  -> starpylarpus   as y is acting as a vowel on its own
yttrium -> arpyttrarpium  as y acts as a vowel here even though at front

Andy

PS I've decided this is quite a good little exercise from a testing perspective.
Last edited on
Test app (without actual arpify implementation)

Not sure how accurate my interpretations of the rules are, but I got a hacked together algorithm to pass all 50 test cases. It checks the preceeding and following characters to decide what to do with the semi-vowels, inc. a belatedly added special case for leading 'y's (to handle yttrium, etc.)

cire's current algorithm scores 40/50 (the failure cases are: borrow, prepay, styrofoam, stylus, taboo, slurpee, bureau, ewe, yellow, yttrium.)

Andy

Edit my alg needed to be rejigged to handle punctation and spaces, and re-capitalization. And now again in Arp Arp...

Tarpest arpapp (warpitharpout arpactarpual arparparpify arpimplarpemarpentarpatarpion)

Narpot sarpure harpow arpaccarpurarpate marpy arpintarperprarpetarpatarpions arpof tharpe rarpularpes arpare, barput Arpi garpot arpa harpackarped tarpogarpetharper arpalgarporarpithm tarpo parpass arpall 50 tarpest carpasarpes. Arpit charpecks tharpe prarpecarpeedarping arpand farpollarpowarping charpararpactarpers tarpo darpecarpide wharpat tarpo darpo warpith tharpe sarpemi-varpowarpels, arpinc. arpa barpelarpatarpedly arpaddarped sparpecarpial carpase farpor larpeadarping 'y's (tarpo harpandle arpyttrarpium, arpetc.)

carpire's carpurrarpent arpalgarporarpithm scarporarpes 40/50 (tharpe farpailarpure carpasarpes arpare: barporrow, prarpepay, starpyrarpofarpoam, starpylarpus, tarpaboo, slarpurpee, barpureau, arpewe, yarpellow, arpyttrarpium.)

Arpandy

Arpedarpit marpy arpalg narpeedarped tarpo barpe rarpejarpiggarped tarpo harpandle parpunctarpatarpion arpand sparpacarpes, Arpand rarpe-carpaparpitarpalarpizarpatarpion. arpand narpow arpagarpain arpin Arparp Arparp...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <iostream>
#include <iomanip>
#include <string>
#include <regex> // if required for arpify function

std::string arpify(const std::string& str);

bool run_all_tests(bool showFailuresOnly);
bool run_test(const std::string& str, const std::string& expectedStr, bool showFailuresOnly);

int main()
{
    std::cout << std::boolalpha;

    bool showFailuresOnly = true;

    bool pass = run_all_tests(showFailuresOnly);

    return (pass ? EXIT_SUCCESS : EXIT_FAILURE);
}

bool run_all_tests(bool showFailuresOnly)
{
    struct TESTCASE
    {
        const char* str;
        const char* expectedStr;
    };

    static const TESTCASE testCases[] = {
        // original
        {"fish", "farpish"},
        {"Harry", "Harparry"},
        {"condition", "carpondarpitarpion"},
        // 1. When a vowel or vowel sound (a, e, i, o, u, or y as in why)
        //    is found, arp is placed in front of it.
        // vowel in the middle start
        {"can", "carpan"},
        {"pen", "parpen"},
        {"spin", "sparpin"},
        {"cloth", "clarpoth"},
        {"burn", "barpurn"},
        {"why", "wharpy"},
        // vowel in front
        {"app", "arpapp"},
        {"elk", "arpelk"},
        {"ink", "arpink"},
        {"ork", "arpork"},
        {"urn", "arpurn"},
        // see yttrium below
        // semi-vowel detail
        // w
        {"growth", "grarpowth"},           // w part of ow
        {"borrow", "barporrow"},           // w part of ow at end but not only vowel sound
        {"brow", "brarpow"},               // w part of ow at end and only vowel sound
        {"homework", "harpomarpewarpork"}, // w distinct
        {"worker", "warporkarper"},        // w distinct at front
        // w does not act on its own
        // y
        {"geyser", "garpeysarper"},   // y part of ey
        {"prepay", "prarpepay"},      // y part of ay at end but not only vowel sound
        {"gray", "grarpay"},          // y part of ay at end and only vowel sound
        {"gapyear", "garpapyarpear"}, // y distinct
        {"year", "yarpear"},          // y distinct at front
        // y acting on its own
        {"styrofoam", "starpyrarpofarpoam"}, // y on its own
        {"stylus", "starpylarpus"},          // y on its own
        // see yttrium below
        // 2. Two or more vowels together are treated as one.
        {"mean", "marpean"},
        {"loan", "larpoan"},
        {"sleet", "slarpeet"},
        {"aesthetic", "arpaestharpetarpic"},
        {"ook", "arpook"},
        {"harpoon", "harparparpoon"},
        // 3. If a vowel or vowel sound occurs as the final letter of a word,
        //    it is only given an arp if it is the only vowel or vowel sound
        //    in the word.
        {"taboo", "tarpaboo"},
        {"woo", "warpoo"},
        {"slurpee", "slarpurpee"},
        // more
        {"beau", "barpeau"},
        {"beauty", "barpeauty"},
        {"bureau", "barpureau"},
        {"cowpat", "carpowparpat"},
        {"ewe", "arpewe"},
        {"hardwood", "harpardwarpood"},
        {"meow", "marpeow"},
        {"miaow", "marpiaow"},
        {"ow", "arpow"},
        {"rhythm", "rharpythm"},
        {"towpath", "tarpowparpath"},
        {"upward", "arpupwarpard"},
        {"wooed", "warpooed"},
        {"yellow", "yarpellow"},
        {"yttrium", "arpyttrarpium"},
    };
    // considered, but apparently not valid spellings:
    // twittwoo (twit twoo/twit-twoo)
    // nappyrash (nappy rash)

    static const size_t testCaseCount = sizeof(testCases)/sizeof(testCases[0]);

    size_t passCount = 0;

    for(size_t i = 0; i < testCaseCount; ++i)
    {
        const TESTCASE& tc  = testCases[i];
        bool pass = run_test(tc.str, tc.expectedStr, showFailuresOnly);
        if(pass)
            ++passCount;
    }

    std::cout << "ran    = " << testCaseCount << std::endl
              << "passed = " << passCount     << std::endl
              << "failed = " << (testCaseCount - passCount) << std::endl
              << std::endl;

    return (passCount == testCaseCount);
}

bool run_test(const std::string& str, const std::string& expectedStr, bool showFailuresOnly)
{
    std::string arpedStr = arpify(str);
    bool pass = (arpedStr == expectedStr);
    if(!pass || !showFailuresOnly)
    {
        std::cout << str << " -> " << arpedStr << std::endl
                  << "pass = " << pass << std::endl;
        if(!pass)
            std::cout << "(expected " << expectedStr << ")" << std::endl;
        std::cout << std::endl;
    }
    return pass;
}

std::string arpify(const std::string& str)
{
    return str; // TODO : implement arpification algorithm
}
Last edited on
cire's current algorithm scores 40/50


I didn't copy the original requirements. I just went off what I remembered in my head (which never seems to be a good idea) and I obviously didn't bother with any special handling for 'w'. I'll have to retool it.

You certainly outdid yourself on the test cases. I only used 8! =P
(Although, you could maybe add one with no vowels in it: psst, shhh, tsk, nth)

[Edit: Hippopotamus is my favorite.]
Last edited on
Topic archived. No new replies allowed.