让我们把这些Unicode字符称为英文IPA辅音:
bdfhjklmnprstvwzðŋɡʃʒθ
让我们把这些Unicode字符称为英文IPA元音:
aeiouæɑɔəɛɜɪʊʌː
(是的,ː
只是长元音标记,但为了这一挑战,请把它当作元音。)
最后,这些是主应力和次级应力的标记:
ˈˌ
注意,
ɡ
(U+0261)不是小写g,主应力标记ˈ
(U+02C8)不是撇号,ː
(U+02D0)不是冒号。
给出一个词,将元音叠加在它们所跟随的辅音的顶部,并将重音标记放在它们前面的辅音下面。(正如问题标题所暗示的那样,这种书写系统(辅音元音序列作为一个单元被包装在一起)称为阿布吉达。给定输入ˈbætəlʃɪp
,生成输出:
æə ɪ
btlʃp
ˈ
如上文所定义的,一个单词肯定是一串辅音、元音和重音标记。永远不会有连续的重音标记,它们总是放在单词的开头和/或辅音前面。
可能有连续元音。例如,kənˌɡrætjʊˈleɪʃən
变成
ɪ
ə æ ʊeə
knɡrtjlʃn
ˌ ˈ
如果一个单词以元音开头,在“基线”上用辅音打印它:əˈpiːl
变成
ː
i
əpl
ˈ
带有初始重音元音的测试用例:ˈælbəˌtrɔs
变为
ə ɔ
ælbtrs
ˈ ˌ
一个冗长的词:ˌsuːpərˌkaləˌfrædʒəˌlɪstɪˌkɛkspiːæləˈdoʊʃəs
变成
æ
ː ː ʊ
uə aə æ əɪ ɪɛ iəoə
sprklfrdʒlstkkspldʃs
ˌ ˌ ˌ ˌ ˌ ˈ
一个无意义的例子,有一个初始的diphthong,大量的元音堆叠,没有重音标记:eɪbaeioubaabaaa
变成
u
o
i a
eaa
ɪaaa
ebbb
您的程序应该产生与这个Python脚本相同的输出:
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()))
发布于 2016-09-10 19:51:48
⍉⌽⊃E,⍨¨↓∘' '¨∨/¨∊∘M¨E←(1+(W∊M←'ˌˈ')++\W∊'bdfhjklmnprstvwzðŋɡʃʒθ')⊂W←⍞
发布于 2016-09-10 20:02:56
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>
发布于 2022-11-28 16:00:20
import."strings"
type P struct{x,y int}
func M(s[]int)(m,n int){for _,e:=range s{if e<=m{m=e};if e>=n{n=e}};return}
func f(s string)(L string){T,x,y,F,R:=make(map[P]rune),0,0,1>0,ContainsRune
for _,r:=range s{if R("ˈˌ",r){T[P{x+1,1}]=r}else if R("bdfhjklmnprstvwzðŋɡʃʒθ",r)||F{y=0;x++;T[P{x,y}],F=r,1<0}else if R("aeiouæɑɔəɛɜɪʊʌː",r){y--;T[P{x,y}],F=r,1<0}}
var u,v[]int
for k:=range T{u,v=append(u,k.x),append(v,k.y)}
for y,Y:=M(v);y<Y+1;y++{o:=[]string{}
for x,X:=M(u);x<X+1;x++{if r,ok:=T[P{x,y}];ok{o=append(o,string(r))}else{o=append(o," ")}}
L+=TrimPrefix(Join(o,"")," ")+"\n"}
return}
引用实现的直接端口。
// map of x-y coords to a character
type tile map[P]rune
func (t tile) Xs() (o []int) {
for k := range t {
o = append(o, k.x)
}
return
}
func (t tile) Ys() (o []int) {
for k := range t {
o = append(o, k.y)
}
return
}
// coordinate pair
type P struct{ x, y int }
func min[T int](s []T) T {
var m T
for _, e := range s {
if e <= m {
m = e
}
}
return m
}
func max[T int](s []T) T {
var m T
for _, e := range s {
if e >= m {
m = e
}
}
return m
}
func f(s string) string {
C, V, S := "bdfhjklmnprstvwzðŋɡʃʒθ", "aeiouæɑɔəɛɜɪʊʌː", "ˈˌ" // constant strs
tiles := make(tile) // the actual map
x, y := 0, 0 // (x0,y0) is the leftmost center
isFirst := true // is this character the first of a line?
for _, r := range s { // for each char...
if strings.ContainsRune(S, r) { // if it's stress...
tiles[P{x + 1, 1}] = r // put it underneath the next syllable
} else if strings.ContainsRune(C, r) || isFirst { // if it's a consonant or the first letter...
y = 0
x++
tiles[P{x, y}] = r // place on the baseline
isFirst = false
} else if strings.ContainsRune(V, r) { // if's a vowel...
y--
tiles[P{x, y}] = r // place 1 above the baseline at the current x
isFirst = false
}
}
// get the ranges for outputting into a string
xs, ys := tiles.Xs(), tiles.Ys()
xmin, xmax := min(xs), max(xs)
ymin, ymax := min(ys), max(ys)
lines := []string{}
for y := ymin; y < ymax+1; y++ { // for each vowel at height y...
line := func() (o []string) {
for x := xmin; x < xmax+1; x++ { // for each syllable on that vowel height...
if r, ok := tiles[P{x, y}]; ok { // get the char
o = append(o, string(r))
} else {
o = append(o, " ") // use a space if there is no vowel there
}
}
return
}()
lines = append(lines, strings.TrimPrefix(strings.Join(line, ""), " ")) // add to the output
}
return strings.Join(lines, "\n") // return the string
}
https://codegolf.stackexchange.com/questions/92877
复制相似问题