英語を啄ばむ4-記号は特別。
Private symbols As Char() = New Char() _
{","c, "."c, "?"c, "'"c, "@"c, ":"c, ";"c, "\"c}
次にコンストラクタを作ってソートをしたんだ。何故ソートしたのかはすぐに分かるよ。
Public Sub New()
Array.Sort(Me.symbols)
End Sub
最後は記号判定用メソッドを改良するだけだぁ。
Private Function IsSymbol(ByVal str As Char) As Boolean
'サーチしてあったら記号だよね♪
If Array.BinarySearch(Of Char)(Me.symbols, str) >= 0 Then
Return True
End If
Return False
End Function
これで嫌らしいドリィちゃんのテストはパスするはずだ・・・
・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・ ピよし!成功ぅー♪
次はJapan.を何とかしないとね。それはGetTokensメソッドを改良すればいいはずさ。
Public Function GetTokens(ByVal text As String) As Token()
'文字列を単語に分ける
Dim str As Char = text(0)
Dim tmp As Queue(Of Token) = New Queue(Of Token)
Dim word As StringBuilder = New StringBuilder(text.Length)
Dim readIndex As Integer = 0
For readIndex = 0 To text.Length
str = text(readIndex)
'空白もしくは記号が来たらそこまでが文字列
If str = " " OrElse Me.IsSymbol(str) Then
If word.Length <> 0 Then
tmp.Enqueue(New Token(word.ToString()))
word = New StringBuilder(text.Length - readIndex)
End If
'シンボルは単体
If Me.IsSymbol(str) Then
tmp.Enqueue(New Token(str.ToString(), TokenCategory.Symbol))
End If
'空白を無視する
While str = " "
readIndex += 1
If readIndex = text.Length Then
Exit While
End If
str = text(readIndex)
End While
End If
word.Append(str)
Next
'結果を返す
Dim result As Token()
Dim max As Integer = tmp.Count - 1
ReDim result(max)
For i As Integer = 0 To max
result(i) = tmp.Dequeue()
Next
Return result
End Function
これでどうだぁー。ピョッシ成功♪これで次に移れるぞ気分爽快ー♪
ドリィちゃん「ふふ、それが快感になるわぁ。」
ゾゾォツ!鳥肌が立ったよ。あれ?ボクは鳥だから元からだね。
なんだか危ないけど今回はおしまい。