“ A”到Ä转换器


12

我目前正在扫描一堆手写文档并将其转换为.txt文件。由于我的笔迹很差,所以.jpg-> .txt转换器会将我的变音符号转换为“正常”字母,'

任务

编写程序或函数以:

  • 给一个字符串
    • 您可以选择任何I / O代码页,只要
      • 它支持字符AEIOUaeiouÄËÏÖÜäëïöü'
      • 输入和输出代码页相同。
    • 输入(在空格旁边)将仅包含代码页中的可打印字符。
      • 只有一种解决方案,因此'a'e'不会出现
  • 将以下集合中的所有字符转换AEIOUaeiouÄËÏÖÜäëïöü
    • 当且仅当它们被'字符包围:
      • 范例'a''e' -> äë
    • 如果from字符串是单个字母。
      • 例如,'AE'根本不改变,照原样输出。
    • 如果字符不是该字符之外的AEIOUaeiou字符,则不会更改。

注意:from字符/ from字符串是之间的一个'

测试用例

Input
Output
<empty line>

'A'sthetik
Ästhetik

Meinung ist wichtig!
Meinung ist wichtig!

Ich sagte: "Er sagte: 'Ich habe Hunger'"
Ich sagte: "Er sagte: 'Ich habe Hunger'"

Ich sagte: "Er sagte: ''A'sthetik'"
Ich sagte: "Er sagte: 'Ästhetik'"

Hämisch rieb er sich die H'a'nde
Hämisch rieb er sich die Hände

H'a''a'slich isn't a German word
Hääslich isn't a German word

since it's really called h'a'sslich
since it's really called hässlich

6
您所有测试用例中的有效成分是'A''a'...不是我认为好的测试用例。
Leaky Nun

1
您可以添加一个示例'w'w不是之一AEIOUaeiou)吗?
jimmy23013

8
合并变音器的状态不明,然后被允许,然后被禁止。这使至少4个答案无效。!嘶!我已将我的投票更改为投票:(
Digital Trauma

1
@ DigitalTrauma我对此感到非常抱歉。
罗曼·格拉夫(RomanGräf)

4
添加测试用例:'q'e'd'
显示名称

Answers:


11

JavaScript(ES6),81 70 68字节

s=>s.replace(/'[aeiou]'/gi,c=>"ï   ÖÄöä ËÜëüÏ "[c.charCodeAt(1)%15])

尝试一下

f=
s=>s.replace(/'[aeiou]'/gi,c=>"ï   ÖÄöä ËÜëüÏ "[c.charCodeAt(1)%15])
i.addEventListener("input",_=>o.innerText=f(i.value))
console.log(f("'A'sthetik")) // Ästhetik
console.log(f("Meinung ist wichtig!")) // Meinung ist wichtig!
console.log(f(`Ich sagte: "Er sagte: 'Ich habe Hunger'"`)) // Ich sagte: "Er sagte: 'Ich habe Hunger'"
console.log(f(`Ich sagte: "Er sagte: ''A'sthetik'"`)) // Ich sagte: "Er sagte: 'Ästhetik'"
console.log(f("Hämisch rieb er sich die H'a'nde")) // Hämisch rieb er sich die Hände
console.log(f("H'a''a'slich isn't a German word")) // Hääslich isn't a German word
console.log(f("since it's really called h'a'sslich")) // since it's really called hässlich
<input id=i><pre id=o>


说明

  • s=> 通过参数“ s”将输入字符串作为参数的匿名函数。
  • s.replace(x,y) 返回以“ x”替换为“ y”的字符串。
  • /'[aeiou]'/gi 不区分大小写的正则表达式,与所有出现在单引号中的元音匹配。
  • c=> 通过参数“ c”将正则表达式的每个匹配项传递给匿名函数。
  • "ï ÖÄöä ËÜëüÏ "[n]返回字符串“ïÖÄööäËÜÜüüÏ”中的第n个字符(索引为0),类似于"ï ÖÄöä ËÜëüÏ ".charAt(n)
  • c.charCodeAt(1)%15 当除以15时,获取“ c”中第二个字符(即元音字符)的其余字符代码。

替代方案中,52分之40 36/48字节(四十七分之三十五字符)

以下是我在禁止使用变音符号之前的回答(嘘声!)-在此提琴中可以更好地查看

s=>s.replace(/'([aeiou])'/gi,"$1̈")

但是,ETHproductions建议在添加.normalize()额外的12个字节后这将是有效的。

s=>s.replace(/'([aeiou])'/gi,"$1̈").normalize()


不可以,如果允许组合变音符号。
阿达姆(Adám)'17年

现在禁止合并变音符号。
阿达姆

我相信您可以通过.normalize()在函数的末尾添加内容来使其有效。
ETHproductions's

您确定@ETHproductions吗?如果禁止使用变音符号,是不是根本不禁止它们出现在答案
毛茸茸的

8

Perl 5、25个字节

s/'(\w)'/chr 1+ord$1/age

24个字节,加上1 -pe代替-e

这利用了“您可以选择任何I / O代码页,只要它支持字符AEIOUaeiouÄËÏÖÜäëïöü'”的规则。它还利用了正则/a表达式上的标志,无论使用哪种编码方式,都\w可以精确地引用字符abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789

我的脚本选择的I / O代码页是这样的:

 1  a
 2  ä
 3  e
 4  ë
 5  i
 6  ï
 7  o
 8  ö
 9  u
10  ü
11  A
12  Ä
13  E
14  Ë
15  I
16  Ï
17  O
18  Ö
19  U
20  Ü
21  '

(我无法在问题的测试用例中测试此脚本,因为它们包含一些非常奇怪的字符,例如t。)


感谢Grimy为我节省了三个字节。早先,我有s/'([a-z])'/chr 1+ord$1/gie,它利用了(编码和)有趣的事实,无论编码如何[a-z],Perl 中都特意对此进行了精确匹配abcdefghijklmnopqrstuvwxyz。我较早的回答是,IMO更有趣,但是这个简短一些,所以,我会接受的。


1
在发布此消息之前,我仔细检查了“默认情况下禁止的漏洞”列表,并且没有发明代码页。那,尤其是该问题邀请使用“任何I / O代码页”的事实,似乎都允许这个答案。然后a-z把戏使答案实际上很有趣,而不仅仅是作弊。(无论如何,IMO。)
msh210 '17

3
这种技巧只有一次很有趣,但是我相信您是第一个使用它的技巧,因此它可以工作(=
Grimmy

1
您可以通过使用节省3个字节\w,而不是[a-z],以及/a代替/i。如果“ / a”修饰符有效,则\w与字符[a-zA-Z0-9_]匹配,无论它们如何编码。
Grimmy

@肮脏的,谢谢!我将修改....
msh210


4

Japt,29个字节

r"'%v'"@"ï   ÖÄöä ËÜëüÏ "gXc1

在线尝试!

说明

r"'%v'"@"ï   ÖÄöä ËÜëüÏ "gXc1

r"'%v'"@                       // Replace each match X of /'<vowel>'/ in the input with
        "ï   ÖÄöä ËÜëüÏ "g     //   the character in this string at index
                          Xc1  //     X.charCodeAt(1).
                               //   Values larger than the length of the string wrap around,
                               //   so this is effectively equal to " ... "[n%15].
                               // Implicit: output result of last expression

1
使用组合变音符是有争议的。
Leaky Nun

击败我。不过,您的解决方案比我的解决方案短得多。
路加福音

@LeakyNun这个问题还是有争议的?
Digital Trauma'5

该问题之所以引起争议,是因为您在评论中提出了这个问题,但是从未解决过。
Leaky Nun

@Adám击败您38秒;-)
ETHproductions '17

4

Javascript,67个字节

s=>s.replace(/'.'/g,c=>"äëïöüÄËÏÖÜ"['aeiouAEIOU'.indexOf(c[1])]||c)

在线尝试!

用引号之间的所有字符替换为相应的变音字符,或者如果匹配项不在需要更改的字符组中,则替换为匹配项本身。


3

果冻,36 字节

œṣ⁹Ṫ¤j
“.ạẏụ’D196;+\Ọż⁾''jЀØc¤;@Wç/

在线尝试!

对于果冻来说,这似乎很复杂!

怎么样?

注意:由于字符不在代码页上,而是在Unicode的一个字节范围内,所以我认为它们必须由普通字符创建。

œṣ⁹Ṫ¤j - Link 1, Replace: char list S [...], list R [char T, char list F]
œṣ     - split S at sublists equal to:
    ¤  -   nilad followed by link(s) as a nilad:
  ⁹    -     link's right argument, R
   Ṫ   -     tail - yield char list F and modify R to become [T]
     j - join with R (now [T])
       - all in all split S at Rs and join back up with [T]s.

“.ạẏụ’D196;+\Ọż⁾''jЀØc¤;@Wç/ - Main link: char list S
       196;                   - 196 concatenate with:
“.ạẏụ’                        -   base 250 literal 747687476
      D                       -   to decimal list [7,4,7,6,8,7,4,7,6]
           +\                 - cumulative reduce with addition: [196,203,207,214,220,228,235,239,246,252]
             Ọ                - cast to characters: ÄËÏÖÜäëïöü
                       ¤      - nilad followed by link(s) as a nilad:
               ⁾''            -   literal ["'", "'"]
                     Øc       -   vowel yield: AEIOUaeiou
                  jЀ         -   join mapped:  ["'A'", "'E'", ...]
              ż               - zip together
                          W   - wrap S in a list
                        ;@    - concatenate (swap @rguments)
                           ç/ - reduce with last link (1) as a dyad
                              - implicit print

3

V,24字节

Óã'¨[aeiou]©'/±:
éiD@"

在线尝试!

十六进制转储:

00000000: d3e3 27a8 5b61 6569 6f75 5da9 272f 160b  ..'.[aeiou].'/..
00000010: b13a 0ae9 6944 4022                      .:..iD@"

这只是我vim答案的直接翻译,以便我可以打败所有高尔夫语言。:P



1

///,67字节

/~/'\///`/\/\/~/'A~Ä`E~Ë`I~Ï`O~Ö`U~Ü`a~ä`e~ë`i~ï`o~ö`u~ü/

在线尝试!

这是通过用单点引号('A')代替由单引号()包围的非点缀字母(与点号相同Ä)来实现的。单个可替代的,这看起来像这样(高尔夫前): /'A'/Ä/

高尔夫有两次常见的事件,//'/,并将它们用作替代。


1

迅捷-201字节

import Foundation;func g(s:String){var e=s;var r="aeiouAEIOUäëïöüÄËÏÖÜ".characters.map{String($0)};for i in r[0...9]{e=e.replacingOccurrences(of:"'\(i)'",with:r[r.index(of:i)!+10])};print(e)}

用法: g("'A'sthetik") // => Ästhetik


1
characters.map{blah blah}replacingOccurrences()真的杀死了乐趣:((
Xcoder先生17年

1

APL(Dyalog),53字节

(v'''[AEIOUaeiou]''')⎕R{'  ÄËÏÖÜäëïöü'[v2⊃⍵.Match]}

在线尝试!

使用PCRE R eplace(将RegEx保存为v)将以下功能应用于带引号的元音:

{ 匿名功能

' ÄËÏÖÜäëïöü'[…用以下方式] 索引字符串(注意与对应的两个空格'[):

  ⍵.Match 匹配的字符串

  2⊃ 选择第二个字母(元音)

  v⍳ 在v中找到索引

}


1

AWK,99字节

{split("AEIOUaeiou",p,"")
for(i=1;i<=split("ÄËÏÖÜäëïöü",r,"");i++)gsub("'"p[i]"'",r[i])}1

在线尝试!

我试图在内想出一些聪明的正则表达式,gensub但失败了:(


1

SOGL43 35(UTF-8)字节

L∫:ÆW ':h++;"äëïöü”:U+Wŗ

说明:

L∫                        repeat 10 times, pushing current iteration (0-based)
  :                       duplicate the iteration
   ÆW                     get the index (1-based) in "aeiouAEIOU"
      ':h++               quote it
           ;              put the copy (current iteration) ontop
            "äëïöü”       push "äëïöü"
                   :      duplicate it
                    U     uppercase it
                     +    join together, resulting in "äëïöüÄËÏÖÜ"
                      W   get the index (1-based) in it
                       ŗ  replace [in the input, current char from "aeiouAEIOU" with
                          the corresponding char in "äëïöüÄËÏÖÜ"

3
嘿,有人可能认为这̈+是SOGL中的功能。
2015年

现在禁止合并变音符号。
2015年

1

05AB1E30 29 24字节

-6字节归功于Emigna

žMDu«S''«''ì"äëïöü"Du«S:

05AB1E äëïöü的代码页中方便地包含字符。

在线尝试!

(旧代码)

žMDu«Svy''.ø})"äëïöü"Du«¹ŠS:

说明(已过时):

žM                             Push aeiou                    ['aeiou']
  D                            Duplicate                     ['aeiou', 'aeiou']
   u                           Uppercase                     ['aeiou', 'AEIOU']
    «                          Concatenate                   ['aeiouAEIOU']
     vy                        For each...
       ''                        Push '
         .ø                      Surround a with b (a -> bab)
           }                   End loop
            )                  Wrap stack to array           [["'a'", "'e'", "'i'", "'o'", "'u'", "'A'", "'E'", "'I'", "'O'", "'U'"]]
             "äëïöü"           String literal.               [["'a'", "'e'", "'i'", "'o'", "'u'", "'A'", "'E'", "'I'", "'O'", "'U'"], 'äëïöü']
                    Du«        Duplicate, uppercase, concat  [["'a'", "'e'", "'i'", "'o'", "'u'", "'A'", "'E'", "'I'", "'O'", "'U'"], 'äëïöüÄËÏÖÜ']
                       ¹       Push first input
                        Š      Push c, a, b                  ["'A'sthetik", ["'a'", "'e'", "'i'", "'o'", "'u'", "'A'", "'E'", "'I'", "'O'", "'U'"], 'äëïöüÄËÏÖÜ']
                          S    Convert to char list          ["'A'sthetik", ["'a'", "'e'", "'i'", "'o'", "'u'", "'A'", "'E'", "'I'", "'O'", "'U'"], ['ä', 'ë', 'ï', 'ö', 'ü', 'Ä', 'Ë', 'Ï', 'Ö', 'Ü']]
                           :   Replace all                   ['Ästhetik']
                               Implicit print

在线尝试!


您可以替换Š
艾米娜(Emigna)'17年

您可以使用žMDu«S''«''ì"äëïöü"Du«S:
Emigna '17

@Emigna再次感谢。
Okx

您也不需要一I开始就使用:)
Emigna '17

1

蟒3.6,98 92个字符

import re;a=lambda i,p="'([AEIOUaeiou])'":re.sub(p,lambda x:'ÄËÏÖÜäëïöü'[p.index(x[1])-3],i)

这是一个功能,而不是完整的程序。

格式化以提高可读性:

import re

a = lambda i, p="'([AEIOUaeiou])'":\
    re.sub(p, lambda x: 'ÄËÏÖÜäëïöü'[p.index(x[1]) - 3], i)

感谢@ValueInk提供了进一步打高尔夫球的聪明技巧。


不为我跑。以TypeError停止。
完全人类

@totallyhuman您确定吗?它似乎为我工作。您需要a使用要替换的字符串来调用该函数。
numbermaniac


1
Python文档报告match.__getitem__(g)Python 3.6的新功能,因此应该在标头中指定它。另外,如果将正则表达式'([AEIOUaeiou])'更改x[0][1]x[1],则可以通过更改为并使用-3代替来保存一个字节-2
价值墨水

1
实际上,它走的时间甚至更短,import re;a=lambda i,p="'([AEIOUaeiou])'":re.sub ...因为您不再需要return声明就节省了很多开销!
价值墨水

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.