12
\$\begingroup\$

Characters

Let’s call these Unicode characters English IPA consonants:

bdfhjklmnprstvwzðŋɡʃʒθ

And let’s call these Unicode characters English IPA vowels:

aeiouæɑɔəɛɜɪʊʌː

(Yes, ː is just the long vowel mark, but treat it as a vowel for the purpose of this challenge.)

Finally, these are primary and secondary stress marks:

ˈˌ

Note that ɡ (U+0261) is not a lowercase g, and the primary stress marker ˈ (U+02C8) is not an apostrophe, and ː (U+02D0) is not a colon.

Your task

Given a word, stack the vowels on top of the consonants they follow, and place the stress markers beneath the consonants they precede. (As the question title hints, such a writing system, where consonant-vowel sequences are packed together as a unit, is called an abugida.) Given the input ˈbætəlʃɪp, produce the output:

æə ɪ
btlʃp
ˈ

A word is guaranteed to be a string of consonants, vowels, and stress marks, as defined above. There will never be consecutive stress marks, and they will always be placed at the start of the word and/or before a consonant.

Test cases

There may be consecutive vowels. For example, kənˌɡrætjʊˈleɪʃən becomes

      ɪ
ə  æ ʊeə
knɡrtjlʃn
  ˌ   ˈ

If a word starts with a vowel, print it on the “baseline” with the consonants: əˈpiːl becomes

 ː
 i
əpl
 ˈ

A test case with an initial, stressed vowel: ˈælbəˌtrɔs becomes

  ə ɔ 
ælbtrs
ˈ  ˌ  

A long word: ˌsuːpərˌkaləˌfrædʒəˌlɪstɪˌkɛkspiːæləˈdoʊʃəs becomes

               æ    
ː              ː ʊ  
uə aə æ əɪ ɪɛ  iəoə 
sprklfrdʒlstkkspldʃs
ˌ  ˌ ˌ   ˌ  ˌ    ˈ  

A nonsense example with an initial diphthong, lots of vowel stacking, and no stress markers: eɪbaeioubaabaaa becomes

 u
 o
 i a
 eaa
ɪaaa
ebbb

Reference implementation

Your program should produce the same output as this Python script:

consonants = 'bdfhjklmnprstvwzðŋɡʃʒθ'
vowels = 'aeiouæɑɔəɛɜɪʊʌː'
stress_marks = 'ˈˌ'

def abugidafy(word):
    tiles = dict()
    x = y = 0

    is_first = True
    for c in word:
        if c in stress_marks:
            tiles[x + 1, 1] = c
        elif c in consonants or is_first:
            y = 0
            x += 1
            tiles[x, y] = c
            is_first = False
        elif c in vowels:
            y -= 1
            tiles[x, y] = c
            is_first = False
        else:
            raise ValueError('Not an IPA character: ' + c)

    xs = [x for (x, y) in tiles.keys()]
    ys = [y for (x, y) in tiles.keys()]
    xmin, xmax = min(xs), max(xs)
    ymin, ymax = min(ys), max(ys)

    lines = []
    for y in range(ymin, ymax + 1):
        line = [tiles.get((x, y), ' ') for x in range(xmin, xmax + 1)]
        lines.append(''.join(line))
    return '\n'.join(lines)

print(abugidafy(input()))

Try it on Ideone.

Rules

  • You may write a function or a full program.

  • If your program has a Unicode character/string type, you can assume inputs and outputs use those. If not, or you read/write from STDIN, use the UTF-8 encoding.

  • You may produce a string containing newlines, or a list of strings representing rows, or an array of Unicode characters.

  • Each row of output may contain any amount of trailing spaces. If you produce a string, it may have a single trailing newline.

  • Your program should produce the correct output for arbitrarily long words with arbitrarily long vowel chains, but may assume that the input word is always valid.

  • If there are no stress markers, your output may optionally include a final empty row (containing nothing, or spaces).

  • The shortest answer (in bytes) wins.

\$\endgroup\$
  • \$\begingroup\$ Poor ɜ, you left it out :-) And British will complain about their ɒ \$\endgroup\$ – Luis Mendo Sep 10 '16 at 14:34
  • \$\begingroup\$ Oops, I did! I added ɜ, so this should be a full General American vowel set now. \$\endgroup\$ – Lynn Sep 10 '16 at 14:42
  • \$\begingroup\$ Are occurrences of any of these characters to only count as one byte in whichever language is used regardless of their code base in order to strike balance between competing golfing languages or is part of the challenge, in your opinion, to find which language may actually perform it in least bytes, period? \$\endgroup\$ – Jonathan Allan Sep 10 '16 at 16:17
  • \$\begingroup\$ Is there a maximum number of vowels after a consonant that our program should recognize? If not add a test case like biiiiiiiiiiiʒ (As in "not the bees") \$\endgroup\$ – DanTheMan Sep 10 '16 at 16:59
  • 1
    \$\begingroup\$ @JonathanAllan The latter; Unicode I/O is part of the challenge. I'll add a note about that. \$\endgroup\$ – Lynn Sep 10 '16 at 17:49
2
\$\begingroup\$

NARS2000 APL, 138 bytes

⍉⌽⊃E,⍨¨↓∘' '¨∨/¨∊∘M¨E←(1+(W∊M←'ˌˈ')++\W∊'bdfhjklmnprstvwzðŋɡʃʒθ')⊂W←⍞
\$\endgroup\$
  • \$\begingroup\$ You can remove the initial ⍞← as output is implied. Also, byte count should be exactly twice the character count, as per this. So this should be 138 bytes. \$\endgroup\$ – Adám Sep 12 '16 at 7:34
1
\$\begingroup\$

Python, 222 bytes

(202 characters)

import re
def f(s):y=[w[0]in'ˈˌ'and w or' '+w for w in re.split('([ˈˌ]?[bdfhjklmnprstvwzðŋɡʃʒθ]?[aeiouæɑɔəɛɜɪʊʌː]*)',s)[1::2]];return[[x[i-1:i]or' 'for x in y]for i in range(max(len(w)for w in y),0,-1)]

Returns an array of unicode characters with an array for each row (containing single spaces for each space required)

Not sure where one can get decent output online yet (and I haven't even got the tools to test it properly here either).
I have loaded a version to ideone that just uses English consonants and vowels with , and . as stress marks, where I have fudged the test cases to conform.

\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES6), 181 bytes

f=
s=>(a=s.match(/[ˈˌ]?.[aeiouæɑɔəɛɜɪʊʌː]*/g).map(s=>/[ˈˌ]/.test(s)?s:` `+s)).map(s=>(l=s.length)>m&&(t=s,m=l),m=0)&&[...t].map(_=>a.map(s=>s[m]||` `,--m).join``).join`
`
;
<input oninput=o.textContent=f(this.value)><pre id=o>

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.