Comment detail

ケブンッリジ関数 (Nested Flatten)

Squeak Smalltalk で。

 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
| source convert stream separators buffer next |

source := 'こんにちは みなさん おげんき ですか? わたしは げんき です。
この ぶんしょう は イギリス の ケンブリッジ だいがく の けんきゅう の けっか
にんげん は もじ を にんしき する とき その さいしょ と さいご の もじさえ あっていれば
じゅんばん は めちゃくちゃ でも ちゃんと よめる という けんきゅう に もとづいて
わざと もじの じゅんばん を いれかえて あります。
どうです? ちゃんと よめちゃう でしょ?
ちゃんと よめたら はんのう よろしく。'.

convert := [:word |
    | specialChars chars |
    specialChars := 'ゃゅょっャュョッー'.
    chars := word inject: OrderedCollection new into: [:out :char |
        (specialChars includes: char)
            ifFalse: [out add: (OrderedCollection with: char)]
            ifTrue: [out last add: char].
        out].
    chars size <= 3 ifTrue: [chars concatenation as: String] ifFalse: [
        | shuffled middle |
        shuffled := (middle := chars copyFrom: 2 to: chars size - 1) shuffled.
        [shuffled = middle] whileTrue: [shuffled := middle shuffled].
        ((chars first: 1), shuffled, (chars last: 1)) concatenation as: String]].

World findATranscript: nil. Transcript clear.
stream := source readStream.
separators := Character separators, '。?'.
buffer := WriteStream on: String new.

[(next := stream next) notNil] whileTrue: [
    (separators includes: next) ifFalse: [buffer nextPut: next] ifTrue: [
        Transcript show: (convert value: buffer contents); nextPut: next.
        buffer reset]].
Transcript show: (convert value: buffer contents)

"=> こにちんは みさなん おんげき ですか? わしたは げんき です。
この ぶしょんう は イリギス の ケブンリッジ だがいく の けきゅんう の けっか
にげんん は もじ を にしんき する とき その さいしょ と さいご の もさじえ あっいてれば
じゅばんん は めくちゃちゃ でも ちゃんと よめる という けきゅんう に もづいとて
わざと もじの じゅばんん を いかれえて あまりす。
どでうす? ちゃんと よちゃめう でしょ?
ちゃんと よためら はのんう よしろく。 "

Index

Feed

Other

Link

Pathtraq

loading...