DoboWiki
Top
> .NETプログラミング研究/101 をテンプレートにして作成
.NETプログラミング研究/101 をテンプレートにして作成
開始行:
#title(Microsoft Visual Studio International Feature Pack...
#navi(.NETプログラミング研究)
#contents
*Microsoft Visual Studio International Feature Pack 2.0: ...
**Yomigana Frameworkとは? [#g23255e4]
以前.NETプログラミング研究の[[85>../85]],[[86>../86]],[[8...
Microsoft Visual Studio International Feature Pack 2.0で...
**ルビを振る [#y3e24945]
まずはYomigana Frameworkを使ってルビを振る方法を紹介しま...
なおYomigana Frameworkは、.NET Framework 3.5以上でしか使...
[[Wikipedia>http://ja.wikipedia.org/wiki/%E3%83%AB%E3%83%...
Yomigana Frameworkでルビを振ると言っても、自動的に振られ...
早速ですが、簡単な例を一つ。「笑う門には福来る。」という...
#code(vbnet){{
'基になる文章
Dim srcs As New System.Collections.Generic.List(Of String...
"笑", "う", "門", "には","福", "来", "る。" _
}
'ルビ
Dim anns As New System.Collections.Generic.List(Of String...
"わら", "", "かど", "", "ふく", "きた", "" _
}
'StringAnnotationsオブジェクトを作成する
Dim strAnns As New Microsoft.International.Collections.St...
srcs, anns)
'ルビを括弧で囲んだ文章を取得する
Dim result As String = strAnns.ToString(Nothing, Nothing)
Console.WriteLine(result)
'笑(わら)う門(かど)には福(ふく)来(きた)る。
}}
#code(csharp){{
//基になる文章
System.Collections.Generic.List<string> srcs =
new System.Collections.Generic.List<string>()
{ "笑", "う", "門", "には", "福", "来", "る。" };
//ルビ
System.Collections.Generic.List<string> anns =
new System.Collections.Generic.List<string>()
{ "わら", "", "かど", "", "ふく", "きた", "" };
//StringAnnotationsオブジェクトを作成する
Microsoft.International.Collections.StringAnnotations str...
new Microsoft.International.Collections.StringAnnotat...
//ルビを括弧で囲んだ文章を取得する
string result = strAnns.ToString(null, null);
Console.WriteLine(result);
//笑(わら)う門(かど)には福(ふく)来(きた)る。
}}
このようにまず、基になる文章をルビを振る節で分けたリスト...
このように親文字のリストとルビのリストが別々だと、ルビが...
なお、ルビには文字列以外のオブジェクトを指定することもで...
StringAnnotationsオブジェクトからルビが振られた文字列を取...
***StringAnnotationsクラスのプロパティ [#f400b020]
StringAnnotationsクラスには、幾つかのプロパティがあります...
ところが何故か、親文字をインデックスで取得、設定できるプ...
これらのプロパティを使った例を以下に示します。
#code(vbnet){{
'基になる文章
Dim srcs As New System.Collections.Generic.List(Of String...
"笑", "う", "門", "には", "福", "来", "る。" _
}
'ルビ
Dim anns As New System.Collections.Generic.List(Of String...
"わら", "", "かど", "", "ふく", "きた", "" _
}
'StringAnnotationsオブジェクトを作成する
Dim strAnns As New Microsoft.International.Collections.St...
srcs, anns)
'元の文章を取得する
Console.WriteLine(strAnns.Source)
'笑う門には福来る
'ルビを列挙する
For i As Integer = 0 To strAnns.Count - 1
Console.Write(strAnns(i) + ",")
Next
'わら,,かど,,ふく,きた,,
Console.WriteLine()
'ルビを列挙する
For Each annotation As String In strAnns
Console.Write(annotation & ",")
Next
'わら,,かど,,ふく,きた,,
}}
#code(csharp){{
//基になる文章
System.Collections.Generic.List<string> srcs =
new System.Collections.Generic.List<string>()
{ "笑", "う", "門", "には", "福", "来", "る。" };
//ルビ
System.Collections.Generic.List<string> anns =
new System.Collections.Generic.List<string>()
{ "わら", "", "かど", "", "ふく", "きた", "" };
//StringAnnotationsオブジェクトを作成する
Microsoft.International.Collections.StringAnnotations str...
new Microsoft.International.Collections.StringAnnotat...
//元の文章を取得する
Console.WriteLine(strAnns.Source);
//笑う門には福来る
//ルビを列挙する
for (int i = 0; i < strAnns.Count; i++)
{
Console.Write(strAnns[i] + ",");
}
//わら,,かど,,ふく,きた,,
Console.WriteLine();
//ルビを列挙する
foreach (string annotation in strAnns)
{
Console.Write(annotation + ",");
}
//わら,,かど,,ふく,きた,,
}}
***ユニコード上のルビを振る [#zdaf3a2f]
次は、StringAnnotationsオブジェクトから[[ユニコード上のル...
#code(vbnet){{
'基になる文章
Dim srcs As New System.Collections.Generic.List(Of String...
"笑", "う", "門", "には","福", "来", "る。" _
}
'ルビ
Dim anns As New System.Collections.Generic.List(Of String...
"わら", "", "かど", "", "ふく", "きた", "" _
}
'StringAnnotationsオブジェクトを作成する
Dim strAnns As New Microsoft.International.Collections.St...
srcs, anns)
'StringAnnotationInterLinearAnnotationFormatProviderオブ...
Dim fp As New _
Microsoft.International.StringAnnotationInterLinearAn...
'ユニコード上のルビが振られた文章を取得する
Dim result As String = strAnns.ToString(Nothing, fp)
'ヘルプの「Yomigana Framework Library の How Do I」では次...
'Dim result As String = fp.Format(fp.FormatString, strAnn...
Console.WriteLine(result.Replace(Char.ConvertFromUtf32(&H...
.Replace(Char.ConvertFromUtf32(&H...
.Replace(Char.ConvertFromUtf32(&H...
}}
#code(csharp){{
//基になる文章
System.Collections.Generic.List<string> srcs =
new System.Collections.Generic.List<string>()
{ "笑", "う", "門", "には", "福", "来", "る。" };
//読み仮名
System.Collections.Generic.List<string> anns =
new System.Collections.Generic.List<string>()
{ "わら", "", "かど", "", "ふく", "きた", "" };
//StringAnnotationsオブジェクトを作成する
Microsoft.International.Collections.StringAnnotations str...
new Microsoft.International.Collections.StringAnnotat...
//StringAnnotationInterLinearAnnotationFormatProviderオブ...
Microsoft.International.StringAnnotationInterLinearAnnota...
new Microsoft.International.StringAnnotationInterLine...
//ユニコード上のルビが振られた文章を取得する
string result = strAnns.ToString(null, fp);
//ヘルプの「Yomigana Framework Library の How Do I」では...
//string result = fp.Format(fp.FormatString, strAnns, fp);
Console.WriteLine(result.Replace("\uFFF9", "\\uFFF9")
.Replace("\uFFFA", "\\uFFFA")
.Replace("\uFFFB", "\\uFFFB"));
}}
この結果、以下のような文字列が出力されます。
|\uFFF9笑\uFFFAわら\uFFFBう\uFFF9門\uFFFAかど\uFFFBには\u...
前のコードと違うのは、StringAnnotations.ToStringメソッド...
コード内のコメントにも書きましたが、Yomigana Frameworkの...
***JIS X 4052上のルビを振る [#h3d595f3]
同じように、JIS X 4052上のルビを振るには、StringAnnotatio...
#code(vbnet){{
'基になる文章
Dim srcs As New System.Collections.Generic.List(Of String...
"笑", "う", "門", "には","福", "来", "る。" _
}
'ルビ
Dim anns As New System.Collections.Generic.List(Of String...
"わら", "", "かど", "", "ふく", "きた", "" _
}
'StringAnnotationsオブジェクトを作成する
Dim strAnns As New Microsoft.International.Collections.St...
srcs, anns)
'StringAnnotationJisX4052FormatProviderオブジェクトを作成...
Dim fp As New Microsoft.International.StringAnnotationJis...
'JIS X 4052上のルビが振られた文章を取得する
Dim result As String = strAnns.ToString(Nothing, fp)
Console.WriteLine(result)
}}
#code(csharp){{
//基になる文章
System.Collections.Generic.List<string> srcs =
new System.Collections.Generic.List<string>()
{ "笑", "う", "門", "には", "福", "来", "る。" };
//ルビ
System.Collections.Generic.List<string> anns =
new System.Collections.Generic.List<string>()
{ "わら", "", "かど", "", "ふく", "きた", "" };
//StringAnnotationsオブジェクトを作成する
Microsoft.International.Collections.StringAnnotations str...
new Microsoft.International.Collections.StringAnnotat...
//StringAnnotationJisX4052FormatProviderオブジェクトを作...
Microsoft.International.StringAnnotationJisX4052FormatPro...
new Microsoft.International.StringAnnotationJisX4052F...
//JIS X 4052上のルビが振られた文章を取得する
string result = strAnns.ToString(null, fp);
Console.WriteLine(result);
}}
この結果、以下のような文字列が出力されます。
|_^笑(わら)^_う_^門(かど)^_には_^福(ふく)...
***読み仮名を指定した文字列で囲む [#ncc2072d]
一番初めの例ではルビが丸括弧で囲まれましたが、StringAnnot...
以下の例では、<< >> で囲んでいます。
#code(vbnet){{
'基になる文章
Dim srcs As New System.Collections.Generic.List(Of String...
"笑", "う", "門", "には","福", "来", "る。" _
}
'ルビ
Dim anns As New System.Collections.Generic.List(Of String...
"わら", "", "かど", "", "ふく", "きた", "" _
}
'StringAnnotationsオブジェクトを作成する
Dim strAnns As New Microsoft.International.Collections.St...
srcs, anns)
'StringAnnotationInterOpenCloseBracketsFormatProviderオブ...
Dim fp As New _
Microsoft.International.StringAnnotationInterOpenClos...
'ルビを囲む文字列を指定する
fp.OpenBracket = "<<"
fp.CloseBracket = ">>"
'ルビを << >> で囲んだ文章を取得する
Dim result As String = strAnns.ToString(Nothing, fp)
Console.WriteLine(result)
'笑<<わら>>う門<<かど>>には福<<ふく>>来<<きた>>る。
}}
#code(csharp){{
//基になる文章
System.Collections.Generic.List<string> srcs =
new System.Collections.Generic.List<string>()
{ "笑", "う", "門", "には", "福", "来", "る。" };
//ルビ
System.Collections.Generic.List<string> anns =
new System.Collections.Generic.List<string>()
{ "わら", "", "かど", "", "ふく", "きた", "" };
//StringAnnotationsオブジェクトを作成する
Microsoft.International.Collections.StringAnnotations str...
new Microsoft.International.Collections.StringAnnotat...
//StringAnnotationInterOpenCloseBracketsFormatProviderオ...
Microsoft.International.StringAnnotationInterOpenCloseBra...
new Microsoft.International.StringAnnotationInterOpen...
//ルビを囲む文字列を指定する
fp.OpenBracket = "<<";
fp.CloseBracket = ">>";
//ルビを << >> で囲んだ文章を取得する
string result = strAnns.ToString(null, fp);
Console.WriteLine(result);
//笑<<わら>>う門<<かど>>には福<<ふく>>来<<きた>>る。
}}
***HTML上のルビを振る [#hc0dca0b]
[[HTML上のルビ>http://ja.wikipedia.org/wiki/%E3%83%AB%E3%...
そこで、ユニコード上のルビをHTML上のルビに変換するメソッ...
#code(vbnet){{
''' <summary>
''' 指定された文字列内のUnicodeルビ表示制御文字をHTMLタグ...
''' </summary>
''' <param name="interLinear">
''' Unicodeルビ表示制御文字を含む文字列
''' </param>
''' <param name="rpOpen">
''' RUBYタグに対応していないブラウザでルビの前に表示する...
''' </param>
''' <param name="rpClose">
''' RUBYタグに対応していないブラウザでルビの後ろに表示す...
''' </param>
''' <returns>
''' interLinearのUnicodeルビ表示制御文字をHTMLタグに置換...
''' </returns>
Public Shared Function ReplaceInterlinearCharToHtmlTag(By...
By...
By...
As Str...
If interLinear Is Nothing Then
Return interLinear
End If
Dim sb As New System.Text.StringBuilder(interLinear)
sb.Replace(Char.ConvertFromUtf32(&HFFF9), _
"<ruby><rb>")
If String.IsNullOrEmpty(rpOpen) Then
sb.Replace(Char.ConvertFromUtf32(&HFFFA), _
"</rb><rt>")
Else
sb.Replace(Char.ConvertFromUtf32(&HFFFA), _
"</rb><rp>" & rpOpen & "</rp><rt>")
End If
If String.IsNullOrEmpty(rpClose) Then
sb.Replace(Char.ConvertFromUtf32(&HFFFB), _
"</rt></ruby>")
Else
sb.Replace(Char.ConvertFromUtf32(&HFFFB), _
"</rt><rp>" & rpClose & "</rp></ruby>")
End If
Return sb.ToString()
End Function
''' <summary>
''' 指定された文字列内のUnicodeルビ表示制御文字をHTMLタグ...
''' </summary>
''' <param name="interLinear">
''' Unicodeルビ表示制御文字を含む文字列
''' </param>
''' <returns>
''' interLinearのUnicodeルビ表示制御文字をHTMLタグに置換...
''' </returns>
Public Shared Function ReplaceInterlinearCharToHtmlTag(By...
As String
Return ReplaceInterlinearCharToHtmlTag(interLinear, "...
End Function
End Class
}}
#code(csharp){{
/// <summary>
/// 指定された文字列内のUnicodeルビ表示制御文字をHTMLタグ...
/// </summary>
/// <param name="interLinear">
/// Unicodeルビ表示制御文字を含む文字列
/// </param>
/// <param name="rpOpen">
/// RUBYタグに対応していないブラウザでルビの前に表示する...
/// </param>
/// <param name="rpClose">
/// RUBYタグに対応していないブラウザでルビの後ろに表示す...
/// </param>
/// <returns>
/// interLinearのUnicodeルビ表示制御文字をHTMLタグに置換...
/// </returns>
public static string ReplaceInterlinearCharToHtmlTag(
string interLinear, string rpOpen, string rpClose)
{
if (interLinear == null)
{
return interLinear;
}
System.Text.StringBuilder sb = new System.Text.String...
sb.Replace("\ufff9", "<ruby><rb>");
if (string.IsNullOrEmpty(rpOpen))
{
sb.Replace("\ufffa", "</rb><rt>");
}
else
{
sb.Replace("\ufffa", "</rb><rp>" + rpOpen + "</rp...
}
if (string.IsNullOrEmpty(rpClose))
{
sb.Replace("\ufffb", "</rt></ruby>");
}
else
{
sb.Replace("\ufffb", "</rt><rp>" + rpClose + "</r...
}
return sb.ToString();
}
/// <summary>
/// 指定された文字列内のUnicodeルビ表示制御文字をHTMLタグ...
/// </summary>
/// <param name="interLinear">
/// Unicodeルビ表示制御文字を含む文字列
/// </param>
/// <returns>
/// interLinearのUnicodeルビ表示制御文字をHTMLタグに置換...
/// </returns>
public static string ReplaceInterlinearCharToHtmlTag(stri...
{
return ReplaceInterlinearCharToHtmlTag(interLinear, "...
}
}}
使い方は、以下のような感じです。
#code(vbnet){{
'基になる文章
Dim srcs As New System.Collections.Generic.List(Of String...
"笑", "う", "門", "には","福", "来", "る。" _
}
'ルビ
Dim anns As New System.Collections.Generic.List(Of String...
"わら", "", "かど", "", "ふく", "きた", "" _
}
'StringAnnotationsオブジェクトを作成する
Dim strAnns As New Microsoft.International.Collections.St...
srcs, anns)
'StringAnnotationInterLinearAnnotationFormatProviderオブ...
Dim fp As New _
Microsoft.International.StringAnnotationInterLinearAn...
'ユニコード上のルビが振られた文章を取得する
Dim result As String = strAnns.ToString(Nothing, fp)
'HTML上のルビに変換する
Dim html As String = ReplaceInterlinearCharToHtmlTag(resu...
'結果を表示する
Console.WriteLine(html)
}}
#code(csharp){{
//基になる文章
System.Collections.Generic.List<string> srcs =
new System.Collections.Generic.List<string>()
{ "笑", "う", "門", "には", "福", "来", "る。" };
//ルビ
System.Collections.Generic.List<string> anns =
new System.Collections.Generic.List<string>()
{ "わら", "", "かど", "", "ふく", "きた", "" };
//StringAnnotationsオブジェクトを作成する
Microsoft.International.Collections.StringAnnotations str...
new Microsoft.International.Collections.StringAnnotat...
//StringAnnotationInterLinearAnnotationFormatProviderオブ...
Microsoft.International.StringAnnotationInterLinearAnnota...
new Microsoft.International.StringAnnotationInterLine...
//ユニコード上のルビが振られた文章を取得する
string result = strAnns.ToString(null, fp);
//HTML上のルビに変換する
string html = ReplaceInterlinearCharToHtmlTag(result);
//結果を表示する
Console.WriteLine(html);
}}
この結果、以下のように出力されます。
|<ruby><rb>笑</rb><rp>(</rp><rt>わら</rt><rp>)</rp></ruby...
なおユニコード上のルビからHTML上のルビへの変換は、ASP.NET...
**ルビが振られた文字列からStringAnnotationsオブジェクトを...
ルビが振られた文字列からStringAnnotationsオブジェクトを作...
***ユニコード上のルビが振られた文字列からStringAnnotation...
ユニコード上のルビが振られた文字列から作成する場合は、そ...
#code(vbnet){{
'ユニコード上のルビが振られた文字列
Dim source As String = Char.ConvertFromUtf32(&HFFF9) & "...
Char.ConvertFromUtf32(&HFFFA) & "わら" & Char.Convert...
"う" & Char.ConvertFromUtf32(&HFFF9) & "門" & Char.Co...
"かど" & Char.ConvertFromUtf32(&HFFFB) & "には" & "" ...
Char.ConvertFromUtf32(&HFFF9) & "福" & Char.ConvertFr...
Char.ConvertFromUtf32(&HFFFB) & "" & Char.ConvertFrom...
Char.ConvertFromUtf32(&HFFFA) & "きた" & Char.Convert...
'StringAnnotationsオブジェクトを作成する
Dim strAnns As Microsoft.International.Collections.String...
Microsoft.International.Collections.StringAnnotations...
'括弧で囲んだルビが振られた文章を取得する
Dim result As String = strAnns.ToString(Nothing, Nothing)
'結果を表示する
Console.WriteLine(result)
'笑(わら)う門(かど)には福(ふく)来(きた)る。
}}
#code(csharp){{
//ユニコード上のルビが振られた文字列
string source = "\uFFF9笑\uFFFAわら\uFFFBう\uFFF9門\uFFFA...
"\uFFF9福\uFFFAふく\uFFFB\uFFF9来\uFFFAきた\uFFFBる。";
//StringAnnotationsオブジェクトを作成する
Microsoft.International.Collections.StringAnnotations str...
Microsoft.International.Collections.StringAnnotations...
//括弧で囲んだルビが振られた文章を取得する
string result = strAnns.ToString(null, null);
//結果を表示する
Console.WriteLine(result);
//笑(わら)う門(かど)には福(ふく)来(きた)る。
}}
***JIS X 4052上のルビが振られた文字列からStringAnnotation...
JIS X 4052上のルビが振られた文字列からStringAnnotationsオ...
#code(vbnet){{
'JIS X 4052上のルビが振られた文字列
Dim source As String = "_^笑(わら)^_う_^門(かど...
"_^福(ふく)^__^来(きた)^_る。"
'StringAnnotationsオブジェクトを作成する
Dim strAnns As Microsoft.International.Collections.String...
Microsoft.International.Collections.StringAnnotations...
source, New Microsoft.International.StringAnnotat...
'括弧で囲んだルビが振られた文章を取得する
Dim result As String = strAnns.ToString(Nothing, Nothing)
'結果を表示する
Console.WriteLine(result)
'笑(わら)う門(かど)には福(ふく)来(きた)る。
}}
#code(csharp){{
//JIS X 4052上のルビが振られた文字列
string source = "_^笑(わら)^_う_^門(かど)^_に...
"_^福(ふく)^__^来(きた)^_る。";
//StringAnnotationsオブジェクトを作成する
Microsoft.International.Collections.StringAnnotations str...
Microsoft.International.Collections.StringAnnotations...
new Microsoft.International.StringAnnotationJisX4052F...
//括弧で囲んだルビが振られた文章を取得する
string result = strAnns.ToString(null, null);
//結果を表示する
Console.WriteLine(result);
//笑(わら)う門(かど)には福(ふく)来(きた)る。
}}
***ルビが括弧で囲まれた文字列からStringAnnotationsオブジ...
ルビが括弧で囲まれた文字列からStringAnnotationsオブジェク...
また、ルビが括弧で囲まれている形式では親文字が明確であり...
この方法によってStringAnnotationsオブジェクトを作成する例...
#code(vbnet){{
'ルビが色々な括弧で囲まれた文字列
Dim source As String = "笑(わら)う()門<かど>には()福[ふく...
'ルビを囲んでいる括弧の種類を指定して、StringAnnotations...
Dim strAnns As Microsoft.International.Collections.String...
Microsoft.International.Collections.StringAnnotations...
source, New String() {"()", "<>", "[]", "{}"})
'JIS X 4052上のルビが振られた文章を取得する
Dim result As String = strAnns.ToString( _
Nothing, New Microsoft.International.StringAnnotation...
'結果を表示する
Console.WriteLine(result)
'_^笑(わら)^_う_^門(かど)^_には_^福(ふく)...
}}
#code(csharp){{
//ルビが色々な括弧で囲まれた文字列
string source = "笑(わら)う()門<かど>には()福[ふく]来{き...
//ルビを囲んでいる括弧の種類を指定して、StringAnnotations...
Microsoft.International.Collections.StringAnnotations str...
Microsoft.International.Collections.StringAnnotations...
new string[] { "()", "<>", "[]", "{}" });
//JIS X 4052上のルビが振られた文章を取得する
string result = strAnns.ToString(null,
new Microsoft.International.StringAnnotationJisX4052F...
//結果を表示する
Console.WriteLine(result);
//_^笑(わら)^_う_^門(かど)^_には_^福(ふく...
}}
**ルビが振られた文字列を比較する [#qa4a88c9]
***ユニコード上のルビが振られた2つの文字列が等しいか調べ...
Yomigana Frameworkには、ユニコード上のルビが振られた文字...
等しいと判断されるのは、すべてのルビと、その親文字が等し...
また、ルビの数が違っていても、それ以外が同じならば、同じ...
ルビとその親文字をどのような方法で比較するか(大文字小文...
なお、ルビだけ、あるいは親文字だけを比較する方法は用意さ...
以下の例では、序数の文字列比較を使って比較しています。
#code(vbnet){{
'比較するユニコード上のルビが振られた文字列
Dim s1 As String = Char.ConvertFromUtf32(&HFFF9) & "福" & _
Char.ConvertFromUtf32(&HFFFA) & "ふく" & Char.Convert...
"" & Char.ConvertFromUtf32(&HFFF9) & "来" & Char.Conv...
"きた" & Char.ConvertFromUtf32(&HFFFB) & "る。"
Dim s2 As String = Char.ConvertFromUtf32(&HFFF9) & "福" & _
Char.ConvertFromUtf32(&HFFFA) & "ふく" & Char.Convert...
"" & Char.ConvertFromUtf32(&HFFF9) & "来" & Char.Conv...
"きた" & Char.ConvertFromUtf32(&HFFFB) & "よ。"
'序数の文字列比較を使って比較する
Dim comparer As New Microsoft.International.InterlinearAn...
StringComparer.Ordinal)
'比較を行う
Dim result As Integer = comparer.Compare(s1, s2)
Console.WriteLine(result)
'0
}}
#code(csharp){{
//比較するユニコード上のルビが振られた文字列
string s1 = "\uFFF9福\uFFFAふく\uFFFB\uFFF9来\uFFFAきた\u...
string s2 = "\uFFF9福\uFFFAふく\uFFFB\uFFF9来\uFFFAきた\u...
//序数の文字列比較を使って比較する
Microsoft.International.InterlinearAnnotationComparer com...
new Microsoft.International.InterlinearAnnotationComp...
StringComparer.Ordinal);
//比較を行う
int result = comparer.Compare(s1, s2);
Console.WriteLine(result);
//0
}}
なおInterlinearAnnotationComparerの代わりにStringAnnotati...
***ひらがなとカタカナ、小書きの仮名と普通の仮名、濁音や半...
ひらがなとカタカナを区別しないで比較したり、小書きの仮名...
以下にその例を示します。
#code(vbnet){{
'比較するユニコード上のルビが振られた文字列
Dim s1 As String = Char.ConvertFromUtf32(&HFFF9) & "福" & _
Char.ConvertFromUtf32(&HFFFA) & "ふく" & Char.Convert...
"" & Char.ConvertFromUtf32(&HFFF9) & "来" & Char.Conv...
"きた" & Char.ConvertFromUtf32(&HFFFB) & "る。"
Dim s2 As String = Char.ConvertFromUtf32(&HFFF9) & "福" & _
Char.ConvertFromUtf32(&HFFFA) & "フク" & Char.Convert...
"" & Char.ConvertFromUtf32(&HFFF9) & "来" & Char.Conv...
"ギダ" & Char.ConvertFromUtf32(&HFFFB) & "よ。"
'序数の文字列比較を使って比較する
Dim comp1 As New Microsoft.International.InterlinearAnnot...
StringComparer.Ordinal)
'ひらがなとカタカナを区別しない
Dim comp2 As New Microsoft.International.IgnoreKanaKindCo...
'小書きの仮名と普通の仮名を区別しない
Dim comp3 As New Microsoft.International.IgnoreSmallKanaL...
'濁音や半濁音と清音を区別しない
Dim comp4 As New _
Microsoft.International.IgnoreVoicedSemiVoicedSoundMa...
'比較を行う
Dim result As Integer = comp4.Compare(s1, s2)
Console.WriteLine(result)
'0
}}
#code(csharp){{
//比較するユニコード上のルビが振られた文字列
string s1 = "\uFFF9福\uFFFAふく\uFFFB\uFFF9来\uFFFAきた\u...
string s2 = "\uFFF9福\uFFFAフク\uFFFB\uFFF9来\uFFFAギダ\u...
//序数の文字列比較を使って比較する
Microsoft.International.InterlinearAnnotationComparer com...
new Microsoft.International.InterlinearAnnotationComp...
//ひらがなとカタカナを区別しない
Microsoft.International.IgnoreKanaKindComparison comp2 =
new Microsoft.International.IgnoreKanaKindComparison(...
//小書きの仮名と普通の仮名を区別しない
Microsoft.International.IgnoreSmallKanaLetterComparison c...
new Microsoft.International.IgnoreSmallKanaLetterComp...
//濁音や半濁音と清音を区別しない
Microsoft.International.IgnoreVoicedSemiVoicedSoundMarksC...
new Microsoft.International.IgnoreVoicedSemiVoicedSou...
//比較を行う
int result = comp4.Compare(s1, s2);
Console.WriteLine(result);
//0
}}
***大小の比較について [#se857980]
このようなCompareメソッドによる比較では、2つが等しいかだ...
+インデックスが0のルビをStringComparer.Compareメソッドで...
+インデックスが0の親文字をStringComparer.Compareメソッド...
+節がもうなければ、0を返す。まだあれば、次のインデックス...
つまり、ルビの大小がまず比較され、次に親文字が比較されま...
**次回予告 [#k3a00669]
今回はStringAnnotationsクラスについて色々調べてみましたが...
**コメント [#va111758]
#comment
//これより下は編集しないでください
#pageinfo([[:Category/.NET]] [[:Category/ASP.NET]],2011-0...
終了行:
#title(Microsoft Visual Studio International Feature Pack...
#navi(.NETプログラミング研究)
#contents
*Microsoft Visual Studio International Feature Pack 2.0: ...
**Yomigana Frameworkとは? [#g23255e4]
以前.NETプログラミング研究の[[85>../85]],[[86>../86]],[[8...
Microsoft Visual Studio International Feature Pack 2.0で...
**ルビを振る [#y3e24945]
まずはYomigana Frameworkを使ってルビを振る方法を紹介しま...
なおYomigana Frameworkは、.NET Framework 3.5以上でしか使...
[[Wikipedia>http://ja.wikipedia.org/wiki/%E3%83%AB%E3%83%...
Yomigana Frameworkでルビを振ると言っても、自動的に振られ...
早速ですが、簡単な例を一つ。「笑う門には福来る。」という...
#code(vbnet){{
'基になる文章
Dim srcs As New System.Collections.Generic.List(Of String...
"笑", "う", "門", "には","福", "来", "る。" _
}
'ルビ
Dim anns As New System.Collections.Generic.List(Of String...
"わら", "", "かど", "", "ふく", "きた", "" _
}
'StringAnnotationsオブジェクトを作成する
Dim strAnns As New Microsoft.International.Collections.St...
srcs, anns)
'ルビを括弧で囲んだ文章を取得する
Dim result As String = strAnns.ToString(Nothing, Nothing)
Console.WriteLine(result)
'笑(わら)う門(かど)には福(ふく)来(きた)る。
}}
#code(csharp){{
//基になる文章
System.Collections.Generic.List<string> srcs =
new System.Collections.Generic.List<string>()
{ "笑", "う", "門", "には", "福", "来", "る。" };
//ルビ
System.Collections.Generic.List<string> anns =
new System.Collections.Generic.List<string>()
{ "わら", "", "かど", "", "ふく", "きた", "" };
//StringAnnotationsオブジェクトを作成する
Microsoft.International.Collections.StringAnnotations str...
new Microsoft.International.Collections.StringAnnotat...
//ルビを括弧で囲んだ文章を取得する
string result = strAnns.ToString(null, null);
Console.WriteLine(result);
//笑(わら)う門(かど)には福(ふく)来(きた)る。
}}
このようにまず、基になる文章をルビを振る節で分けたリスト...
このように親文字のリストとルビのリストが別々だと、ルビが...
なお、ルビには文字列以外のオブジェクトを指定することもで...
StringAnnotationsオブジェクトからルビが振られた文字列を取...
***StringAnnotationsクラスのプロパティ [#f400b020]
StringAnnotationsクラスには、幾つかのプロパティがあります...
ところが何故か、親文字をインデックスで取得、設定できるプ...
これらのプロパティを使った例を以下に示します。
#code(vbnet){{
'基になる文章
Dim srcs As New System.Collections.Generic.List(Of String...
"笑", "う", "門", "には", "福", "来", "る。" _
}
'ルビ
Dim anns As New System.Collections.Generic.List(Of String...
"わら", "", "かど", "", "ふく", "きた", "" _
}
'StringAnnotationsオブジェクトを作成する
Dim strAnns As New Microsoft.International.Collections.St...
srcs, anns)
'元の文章を取得する
Console.WriteLine(strAnns.Source)
'笑う門には福来る
'ルビを列挙する
For i As Integer = 0 To strAnns.Count - 1
Console.Write(strAnns(i) + ",")
Next
'わら,,かど,,ふく,きた,,
Console.WriteLine()
'ルビを列挙する
For Each annotation As String In strAnns
Console.Write(annotation & ",")
Next
'わら,,かど,,ふく,きた,,
}}
#code(csharp){{
//基になる文章
System.Collections.Generic.List<string> srcs =
new System.Collections.Generic.List<string>()
{ "笑", "う", "門", "には", "福", "来", "る。" };
//ルビ
System.Collections.Generic.List<string> anns =
new System.Collections.Generic.List<string>()
{ "わら", "", "かど", "", "ふく", "きた", "" };
//StringAnnotationsオブジェクトを作成する
Microsoft.International.Collections.StringAnnotations str...
new Microsoft.International.Collections.StringAnnotat...
//元の文章を取得する
Console.WriteLine(strAnns.Source);
//笑う門には福来る
//ルビを列挙する
for (int i = 0; i < strAnns.Count; i++)
{
Console.Write(strAnns[i] + ",");
}
//わら,,かど,,ふく,きた,,
Console.WriteLine();
//ルビを列挙する
foreach (string annotation in strAnns)
{
Console.Write(annotation + ",");
}
//わら,,かど,,ふく,きた,,
}}
***ユニコード上のルビを振る [#zdaf3a2f]
次は、StringAnnotationsオブジェクトから[[ユニコード上のル...
#code(vbnet){{
'基になる文章
Dim srcs As New System.Collections.Generic.List(Of String...
"笑", "う", "門", "には","福", "来", "る。" _
}
'ルビ
Dim anns As New System.Collections.Generic.List(Of String...
"わら", "", "かど", "", "ふく", "きた", "" _
}
'StringAnnotationsオブジェクトを作成する
Dim strAnns As New Microsoft.International.Collections.St...
srcs, anns)
'StringAnnotationInterLinearAnnotationFormatProviderオブ...
Dim fp As New _
Microsoft.International.StringAnnotationInterLinearAn...
'ユニコード上のルビが振られた文章を取得する
Dim result As String = strAnns.ToString(Nothing, fp)
'ヘルプの「Yomigana Framework Library の How Do I」では次...
'Dim result As String = fp.Format(fp.FormatString, strAnn...
Console.WriteLine(result.Replace(Char.ConvertFromUtf32(&H...
.Replace(Char.ConvertFromUtf32(&H...
.Replace(Char.ConvertFromUtf32(&H...
}}
#code(csharp){{
//基になる文章
System.Collections.Generic.List<string> srcs =
new System.Collections.Generic.List<string>()
{ "笑", "う", "門", "には", "福", "来", "る。" };
//読み仮名
System.Collections.Generic.List<string> anns =
new System.Collections.Generic.List<string>()
{ "わら", "", "かど", "", "ふく", "きた", "" };
//StringAnnotationsオブジェクトを作成する
Microsoft.International.Collections.StringAnnotations str...
new Microsoft.International.Collections.StringAnnotat...
//StringAnnotationInterLinearAnnotationFormatProviderオブ...
Microsoft.International.StringAnnotationInterLinearAnnota...
new Microsoft.International.StringAnnotationInterLine...
//ユニコード上のルビが振られた文章を取得する
string result = strAnns.ToString(null, fp);
//ヘルプの「Yomigana Framework Library の How Do I」では...
//string result = fp.Format(fp.FormatString, strAnns, fp);
Console.WriteLine(result.Replace("\uFFF9", "\\uFFF9")
.Replace("\uFFFA", "\\uFFFA")
.Replace("\uFFFB", "\\uFFFB"));
}}
この結果、以下のような文字列が出力されます。
|\uFFF9笑\uFFFAわら\uFFFBう\uFFF9門\uFFFAかど\uFFFBには\u...
前のコードと違うのは、StringAnnotations.ToStringメソッド...
コード内のコメントにも書きましたが、Yomigana Frameworkの...
***JIS X 4052上のルビを振る [#h3d595f3]
同じように、JIS X 4052上のルビを振るには、StringAnnotatio...
#code(vbnet){{
'基になる文章
Dim srcs As New System.Collections.Generic.List(Of String...
"笑", "う", "門", "には","福", "来", "る。" _
}
'ルビ
Dim anns As New System.Collections.Generic.List(Of String...
"わら", "", "かど", "", "ふく", "きた", "" _
}
'StringAnnotationsオブジェクトを作成する
Dim strAnns As New Microsoft.International.Collections.St...
srcs, anns)
'StringAnnotationJisX4052FormatProviderオブジェクトを作成...
Dim fp As New Microsoft.International.StringAnnotationJis...
'JIS X 4052上のルビが振られた文章を取得する
Dim result As String = strAnns.ToString(Nothing, fp)
Console.WriteLine(result)
}}
#code(csharp){{
//基になる文章
System.Collections.Generic.List<string> srcs =
new System.Collections.Generic.List<string>()
{ "笑", "う", "門", "には", "福", "来", "る。" };
//ルビ
System.Collections.Generic.List<string> anns =
new System.Collections.Generic.List<string>()
{ "わら", "", "かど", "", "ふく", "きた", "" };
//StringAnnotationsオブジェクトを作成する
Microsoft.International.Collections.StringAnnotations str...
new Microsoft.International.Collections.StringAnnotat...
//StringAnnotationJisX4052FormatProviderオブジェクトを作...
Microsoft.International.StringAnnotationJisX4052FormatPro...
new Microsoft.International.StringAnnotationJisX4052F...
//JIS X 4052上のルビが振られた文章を取得する
string result = strAnns.ToString(null, fp);
Console.WriteLine(result);
}}
この結果、以下のような文字列が出力されます。
|_^笑(わら)^_う_^門(かど)^_には_^福(ふく)...
***読み仮名を指定した文字列で囲む [#ncc2072d]
一番初めの例ではルビが丸括弧で囲まれましたが、StringAnnot...
以下の例では、<< >> で囲んでいます。
#code(vbnet){{
'基になる文章
Dim srcs As New System.Collections.Generic.List(Of String...
"笑", "う", "門", "には","福", "来", "る。" _
}
'ルビ
Dim anns As New System.Collections.Generic.List(Of String...
"わら", "", "かど", "", "ふく", "きた", "" _
}
'StringAnnotationsオブジェクトを作成する
Dim strAnns As New Microsoft.International.Collections.St...
srcs, anns)
'StringAnnotationInterOpenCloseBracketsFormatProviderオブ...
Dim fp As New _
Microsoft.International.StringAnnotationInterOpenClos...
'ルビを囲む文字列を指定する
fp.OpenBracket = "<<"
fp.CloseBracket = ">>"
'ルビを << >> で囲んだ文章を取得する
Dim result As String = strAnns.ToString(Nothing, fp)
Console.WriteLine(result)
'笑<<わら>>う門<<かど>>には福<<ふく>>来<<きた>>る。
}}
#code(csharp){{
//基になる文章
System.Collections.Generic.List<string> srcs =
new System.Collections.Generic.List<string>()
{ "笑", "う", "門", "には", "福", "来", "る。" };
//ルビ
System.Collections.Generic.List<string> anns =
new System.Collections.Generic.List<string>()
{ "わら", "", "かど", "", "ふく", "きた", "" };
//StringAnnotationsオブジェクトを作成する
Microsoft.International.Collections.StringAnnotations str...
new Microsoft.International.Collections.StringAnnotat...
//StringAnnotationInterOpenCloseBracketsFormatProviderオ...
Microsoft.International.StringAnnotationInterOpenCloseBra...
new Microsoft.International.StringAnnotationInterOpen...
//ルビを囲む文字列を指定する
fp.OpenBracket = "<<";
fp.CloseBracket = ">>";
//ルビを << >> で囲んだ文章を取得する
string result = strAnns.ToString(null, fp);
Console.WriteLine(result);
//笑<<わら>>う門<<かど>>には福<<ふく>>来<<きた>>る。
}}
***HTML上のルビを振る [#hc0dca0b]
[[HTML上のルビ>http://ja.wikipedia.org/wiki/%E3%83%AB%E3%...
そこで、ユニコード上のルビをHTML上のルビに変換するメソッ...
#code(vbnet){{
''' <summary>
''' 指定された文字列内のUnicodeルビ表示制御文字をHTMLタグ...
''' </summary>
''' <param name="interLinear">
''' Unicodeルビ表示制御文字を含む文字列
''' </param>
''' <param name="rpOpen">
''' RUBYタグに対応していないブラウザでルビの前に表示する...
''' </param>
''' <param name="rpClose">
''' RUBYタグに対応していないブラウザでルビの後ろに表示す...
''' </param>
''' <returns>
''' interLinearのUnicodeルビ表示制御文字をHTMLタグに置換...
''' </returns>
Public Shared Function ReplaceInterlinearCharToHtmlTag(By...
By...
By...
As Str...
If interLinear Is Nothing Then
Return interLinear
End If
Dim sb As New System.Text.StringBuilder(interLinear)
sb.Replace(Char.ConvertFromUtf32(&HFFF9), _
"<ruby><rb>")
If String.IsNullOrEmpty(rpOpen) Then
sb.Replace(Char.ConvertFromUtf32(&HFFFA), _
"</rb><rt>")
Else
sb.Replace(Char.ConvertFromUtf32(&HFFFA), _
"</rb><rp>" & rpOpen & "</rp><rt>")
End If
If String.IsNullOrEmpty(rpClose) Then
sb.Replace(Char.ConvertFromUtf32(&HFFFB), _
"</rt></ruby>")
Else
sb.Replace(Char.ConvertFromUtf32(&HFFFB), _
"</rt><rp>" & rpClose & "</rp></ruby>")
End If
Return sb.ToString()
End Function
''' <summary>
''' 指定された文字列内のUnicodeルビ表示制御文字をHTMLタグ...
''' </summary>
''' <param name="interLinear">
''' Unicodeルビ表示制御文字を含む文字列
''' </param>
''' <returns>
''' interLinearのUnicodeルビ表示制御文字をHTMLタグに置換...
''' </returns>
Public Shared Function ReplaceInterlinearCharToHtmlTag(By...
As String
Return ReplaceInterlinearCharToHtmlTag(interLinear, "...
End Function
End Class
}}
#code(csharp){{
/// <summary>
/// 指定された文字列内のUnicodeルビ表示制御文字をHTMLタグ...
/// </summary>
/// <param name="interLinear">
/// Unicodeルビ表示制御文字を含む文字列
/// </param>
/// <param name="rpOpen">
/// RUBYタグに対応していないブラウザでルビの前に表示する...
/// </param>
/// <param name="rpClose">
/// RUBYタグに対応していないブラウザでルビの後ろに表示す...
/// </param>
/// <returns>
/// interLinearのUnicodeルビ表示制御文字をHTMLタグに置換...
/// </returns>
public static string ReplaceInterlinearCharToHtmlTag(
string interLinear, string rpOpen, string rpClose)
{
if (interLinear == null)
{
return interLinear;
}
System.Text.StringBuilder sb = new System.Text.String...
sb.Replace("\ufff9", "<ruby><rb>");
if (string.IsNullOrEmpty(rpOpen))
{
sb.Replace("\ufffa", "</rb><rt>");
}
else
{
sb.Replace("\ufffa", "</rb><rp>" + rpOpen + "</rp...
}
if (string.IsNullOrEmpty(rpClose))
{
sb.Replace("\ufffb", "</rt></ruby>");
}
else
{
sb.Replace("\ufffb", "</rt><rp>" + rpClose + "</r...
}
return sb.ToString();
}
/// <summary>
/// 指定された文字列内のUnicodeルビ表示制御文字をHTMLタグ...
/// </summary>
/// <param name="interLinear">
/// Unicodeルビ表示制御文字を含む文字列
/// </param>
/// <returns>
/// interLinearのUnicodeルビ表示制御文字をHTMLタグに置換...
/// </returns>
public static string ReplaceInterlinearCharToHtmlTag(stri...
{
return ReplaceInterlinearCharToHtmlTag(interLinear, "...
}
}}
使い方は、以下のような感じです。
#code(vbnet){{
'基になる文章
Dim srcs As New System.Collections.Generic.List(Of String...
"笑", "う", "門", "には","福", "来", "る。" _
}
'ルビ
Dim anns As New System.Collections.Generic.List(Of String...
"わら", "", "かど", "", "ふく", "きた", "" _
}
'StringAnnotationsオブジェクトを作成する
Dim strAnns As New Microsoft.International.Collections.St...
srcs, anns)
'StringAnnotationInterLinearAnnotationFormatProviderオブ...
Dim fp As New _
Microsoft.International.StringAnnotationInterLinearAn...
'ユニコード上のルビが振られた文章を取得する
Dim result As String = strAnns.ToString(Nothing, fp)
'HTML上のルビに変換する
Dim html As String = ReplaceInterlinearCharToHtmlTag(resu...
'結果を表示する
Console.WriteLine(html)
}}
#code(csharp){{
//基になる文章
System.Collections.Generic.List<string> srcs =
new System.Collections.Generic.List<string>()
{ "笑", "う", "門", "には", "福", "来", "る。" };
//ルビ
System.Collections.Generic.List<string> anns =
new System.Collections.Generic.List<string>()
{ "わら", "", "かど", "", "ふく", "きた", "" };
//StringAnnotationsオブジェクトを作成する
Microsoft.International.Collections.StringAnnotations str...
new Microsoft.International.Collections.StringAnnotat...
//StringAnnotationInterLinearAnnotationFormatProviderオブ...
Microsoft.International.StringAnnotationInterLinearAnnota...
new Microsoft.International.StringAnnotationInterLine...
//ユニコード上のルビが振られた文章を取得する
string result = strAnns.ToString(null, fp);
//HTML上のルビに変換する
string html = ReplaceInterlinearCharToHtmlTag(result);
//結果を表示する
Console.WriteLine(html);
}}
この結果、以下のように出力されます。
|<ruby><rb>笑</rb><rp>(</rp><rt>わら</rt><rp>)</rp></ruby...
なおユニコード上のルビからHTML上のルビへの変換は、ASP.NET...
**ルビが振られた文字列からStringAnnotationsオブジェクトを...
ルビが振られた文字列からStringAnnotationsオブジェクトを作...
***ユニコード上のルビが振られた文字列からStringAnnotation...
ユニコード上のルビが振られた文字列から作成する場合は、そ...
#code(vbnet){{
'ユニコード上のルビが振られた文字列
Dim source As String = Char.ConvertFromUtf32(&HFFF9) & "...
Char.ConvertFromUtf32(&HFFFA) & "わら" & Char.Convert...
"う" & Char.ConvertFromUtf32(&HFFF9) & "門" & Char.Co...
"かど" & Char.ConvertFromUtf32(&HFFFB) & "には" & "" ...
Char.ConvertFromUtf32(&HFFF9) & "福" & Char.ConvertFr...
Char.ConvertFromUtf32(&HFFFB) & "" & Char.ConvertFrom...
Char.ConvertFromUtf32(&HFFFA) & "きた" & Char.Convert...
'StringAnnotationsオブジェクトを作成する
Dim strAnns As Microsoft.International.Collections.String...
Microsoft.International.Collections.StringAnnotations...
'括弧で囲んだルビが振られた文章を取得する
Dim result As String = strAnns.ToString(Nothing, Nothing)
'結果を表示する
Console.WriteLine(result)
'笑(わら)う門(かど)には福(ふく)来(きた)る。
}}
#code(csharp){{
//ユニコード上のルビが振られた文字列
string source = "\uFFF9笑\uFFFAわら\uFFFBう\uFFF9門\uFFFA...
"\uFFF9福\uFFFAふく\uFFFB\uFFF9来\uFFFAきた\uFFFBる。";
//StringAnnotationsオブジェクトを作成する
Microsoft.International.Collections.StringAnnotations str...
Microsoft.International.Collections.StringAnnotations...
//括弧で囲んだルビが振られた文章を取得する
string result = strAnns.ToString(null, null);
//結果を表示する
Console.WriteLine(result);
//笑(わら)う門(かど)には福(ふく)来(きた)る。
}}
***JIS X 4052上のルビが振られた文字列からStringAnnotation...
JIS X 4052上のルビが振られた文字列からStringAnnotationsオ...
#code(vbnet){{
'JIS X 4052上のルビが振られた文字列
Dim source As String = "_^笑(わら)^_う_^門(かど...
"_^福(ふく)^__^来(きた)^_る。"
'StringAnnotationsオブジェクトを作成する
Dim strAnns As Microsoft.International.Collections.String...
Microsoft.International.Collections.StringAnnotations...
source, New Microsoft.International.StringAnnotat...
'括弧で囲んだルビが振られた文章を取得する
Dim result As String = strAnns.ToString(Nothing, Nothing)
'結果を表示する
Console.WriteLine(result)
'笑(わら)う門(かど)には福(ふく)来(きた)る。
}}
#code(csharp){{
//JIS X 4052上のルビが振られた文字列
string source = "_^笑(わら)^_う_^門(かど)^_に...
"_^福(ふく)^__^来(きた)^_る。";
//StringAnnotationsオブジェクトを作成する
Microsoft.International.Collections.StringAnnotations str...
Microsoft.International.Collections.StringAnnotations...
new Microsoft.International.StringAnnotationJisX4052F...
//括弧で囲んだルビが振られた文章を取得する
string result = strAnns.ToString(null, null);
//結果を表示する
Console.WriteLine(result);
//笑(わら)う門(かど)には福(ふく)来(きた)る。
}}
***ルビが括弧で囲まれた文字列からStringAnnotationsオブジ...
ルビが括弧で囲まれた文字列からStringAnnotationsオブジェク...
また、ルビが括弧で囲まれている形式では親文字が明確であり...
この方法によってStringAnnotationsオブジェクトを作成する例...
#code(vbnet){{
'ルビが色々な括弧で囲まれた文字列
Dim source As String = "笑(わら)う()門<かど>には()福[ふく...
'ルビを囲んでいる括弧の種類を指定して、StringAnnotations...
Dim strAnns As Microsoft.International.Collections.String...
Microsoft.International.Collections.StringAnnotations...
source, New String() {"()", "<>", "[]", "{}"})
'JIS X 4052上のルビが振られた文章を取得する
Dim result As String = strAnns.ToString( _
Nothing, New Microsoft.International.StringAnnotation...
'結果を表示する
Console.WriteLine(result)
'_^笑(わら)^_う_^門(かど)^_には_^福(ふく)...
}}
#code(csharp){{
//ルビが色々な括弧で囲まれた文字列
string source = "笑(わら)う()門<かど>には()福[ふく]来{き...
//ルビを囲んでいる括弧の種類を指定して、StringAnnotations...
Microsoft.International.Collections.StringAnnotations str...
Microsoft.International.Collections.StringAnnotations...
new string[] { "()", "<>", "[]", "{}" });
//JIS X 4052上のルビが振られた文章を取得する
string result = strAnns.ToString(null,
new Microsoft.International.StringAnnotationJisX4052F...
//結果を表示する
Console.WriteLine(result);
//_^笑(わら)^_う_^門(かど)^_には_^福(ふく...
}}
**ルビが振られた文字列を比較する [#qa4a88c9]
***ユニコード上のルビが振られた2つの文字列が等しいか調べ...
Yomigana Frameworkには、ユニコード上のルビが振られた文字...
等しいと判断されるのは、すべてのルビと、その親文字が等し...
また、ルビの数が違っていても、それ以外が同じならば、同じ...
ルビとその親文字をどのような方法で比較するか(大文字小文...
なお、ルビだけ、あるいは親文字だけを比較する方法は用意さ...
以下の例では、序数の文字列比較を使って比較しています。
#code(vbnet){{
'比較するユニコード上のルビが振られた文字列
Dim s1 As String = Char.ConvertFromUtf32(&HFFF9) & "福" & _
Char.ConvertFromUtf32(&HFFFA) & "ふく" & Char.Convert...
"" & Char.ConvertFromUtf32(&HFFF9) & "来" & Char.Conv...
"きた" & Char.ConvertFromUtf32(&HFFFB) & "る。"
Dim s2 As String = Char.ConvertFromUtf32(&HFFF9) & "福" & _
Char.ConvertFromUtf32(&HFFFA) & "ふく" & Char.Convert...
"" & Char.ConvertFromUtf32(&HFFF9) & "来" & Char.Conv...
"きた" & Char.ConvertFromUtf32(&HFFFB) & "よ。"
'序数の文字列比較を使って比較する
Dim comparer As New Microsoft.International.InterlinearAn...
StringComparer.Ordinal)
'比較を行う
Dim result As Integer = comparer.Compare(s1, s2)
Console.WriteLine(result)
'0
}}
#code(csharp){{
//比較するユニコード上のルビが振られた文字列
string s1 = "\uFFF9福\uFFFAふく\uFFFB\uFFF9来\uFFFAきた\u...
string s2 = "\uFFF9福\uFFFAふく\uFFFB\uFFF9来\uFFFAきた\u...
//序数の文字列比較を使って比較する
Microsoft.International.InterlinearAnnotationComparer com...
new Microsoft.International.InterlinearAnnotationComp...
StringComparer.Ordinal);
//比較を行う
int result = comparer.Compare(s1, s2);
Console.WriteLine(result);
//0
}}
なおInterlinearAnnotationComparerの代わりにStringAnnotati...
***ひらがなとカタカナ、小書きの仮名と普通の仮名、濁音や半...
ひらがなとカタカナを区別しないで比較したり、小書きの仮名...
以下にその例を示します。
#code(vbnet){{
'比較するユニコード上のルビが振られた文字列
Dim s1 As String = Char.ConvertFromUtf32(&HFFF9) & "福" & _
Char.ConvertFromUtf32(&HFFFA) & "ふく" & Char.Convert...
"" & Char.ConvertFromUtf32(&HFFF9) & "来" & Char.Conv...
"きた" & Char.ConvertFromUtf32(&HFFFB) & "る。"
Dim s2 As String = Char.ConvertFromUtf32(&HFFF9) & "福" & _
Char.ConvertFromUtf32(&HFFFA) & "フク" & Char.Convert...
"" & Char.ConvertFromUtf32(&HFFF9) & "来" & Char.Conv...
"ギダ" & Char.ConvertFromUtf32(&HFFFB) & "よ。"
'序数の文字列比較を使って比較する
Dim comp1 As New Microsoft.International.InterlinearAnnot...
StringComparer.Ordinal)
'ひらがなとカタカナを区別しない
Dim comp2 As New Microsoft.International.IgnoreKanaKindCo...
'小書きの仮名と普通の仮名を区別しない
Dim comp3 As New Microsoft.International.IgnoreSmallKanaL...
'濁音や半濁音と清音を区別しない
Dim comp4 As New _
Microsoft.International.IgnoreVoicedSemiVoicedSoundMa...
'比較を行う
Dim result As Integer = comp4.Compare(s1, s2)
Console.WriteLine(result)
'0
}}
#code(csharp){{
//比較するユニコード上のルビが振られた文字列
string s1 = "\uFFF9福\uFFFAふく\uFFFB\uFFF9来\uFFFAきた\u...
string s2 = "\uFFF9福\uFFFAフク\uFFFB\uFFF9来\uFFFAギダ\u...
//序数の文字列比較を使って比較する
Microsoft.International.InterlinearAnnotationComparer com...
new Microsoft.International.InterlinearAnnotationComp...
//ひらがなとカタカナを区別しない
Microsoft.International.IgnoreKanaKindComparison comp2 =
new Microsoft.International.IgnoreKanaKindComparison(...
//小書きの仮名と普通の仮名を区別しない
Microsoft.International.IgnoreSmallKanaLetterComparison c...
new Microsoft.International.IgnoreSmallKanaLetterComp...
//濁音や半濁音と清音を区別しない
Microsoft.International.IgnoreVoicedSemiVoicedSoundMarksC...
new Microsoft.International.IgnoreVoicedSemiVoicedSou...
//比較を行う
int result = comp4.Compare(s1, s2);
Console.WriteLine(result);
//0
}}
***大小の比較について [#se857980]
このようなCompareメソッドによる比較では、2つが等しいかだ...
+インデックスが0のルビをStringComparer.Compareメソッドで...
+インデックスが0の親文字をStringComparer.Compareメソッド...
+節がもうなければ、0を返す。まだあれば、次のインデックス...
つまり、ルビの大小がまず比較され、次に親文字が比較されま...
**次回予告 [#k3a00669]
今回はStringAnnotationsクラスについて色々調べてみましたが...
**コメント [#va111758]
#comment
//これより下は編集しないでください
#pageinfo([[:Category/.NET]] [[:Category/ASP.NET]],2011-0...
ページ名:
▲
▼
[
トップ
] [
新規
|
子ページ作成
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
]