LoginSignup
4
0

More than 1 year has passed since last update.

【UiPath】よみがなを取得する

Posted at

はじめに

この投稿は、RPAツール「UiPath」での 実装例 について記事です。

漢字のよみがなを取得

以下のように漢字の読み仮名を取得してみます。

令和 -> レイワ
平成 -> ヘイセイ

Excelには、漢字のよみがなを取得する「GetPhonetic」メソッドがあります。(phonetic 【形】 〔人の発話の〕音声の、音声に関する、表音つづり字)

.NETのコードで「Excel.Application」を操作し、読みがなを取得してみます。

以下のコードをInvokeCode(コード呼び出し)に記述すると変換されます。

' # Argument
' - in_text
' - out_text
Dim ap As New Microsoft.Office.Interop.Excel.Application
Try
    out_text = ap.GetPhonetic(in_text)
    '// カナ->かな変換(1041:日本語)
    ' out_text = Microsoft.VisualBasic.Strings.StrConv(out_text, microsoft.VisualBasic.VbStrConv.Hiragana, 1041)
    Console.writeLine(out_text)
Catch ex As Exception
    If Not(ap Is Nothing) Then ap = Nothing
    Console.writeLine(ex.ToString)
    Throw(ex)
Finally
    If Not(ap Is Nothing) Then ap = Nothing
End Try

英単語のカタカナ読みを取得

今度は英単語をカタカナ読みにしてみます。例えば以下のような変換です。

YOUNG -> ヤング
YOU'VE ->  ユーブ
YOU'RE ->  ユーアー

この「英語カタカナ読み変換」は上述のようなアプリケーションで解決する方法がないので、辞書ファイルを用いて変換します。

今回は以下の辞書ファイルを使用しました。
 Bilingual Emacspeak Project(BEP) GPLライセンス

' # Argument
' - in_text
' - in_filepath_dictionary '// 辞書ファイルのパス
' - out_text
Try
    '// 辞書ファイルの読み込み
    Dim dic As New Dictionary(Of String, String)
    Using sr As StreamReader = New StreamReader(in_filepath_dictionary, System.Text.Encoding.GetEncoding("UTF-8"))
        Dim line As String = sr.ReadLine()
        Dim aryLine As String()
        Do Until line Is Nothing
            If Not(line.StartsWith("#")) Then
                aryLine = Strings.Split(line, " "c)
                dic.Item(aryLine(0).ToUpper) = aryLine(1)
            End If
            line = sr.ReadLine()
        Loop
    End Using   

    '// 変換対象テキストを「スペース」区切りでループ
    Dim lstText As New List(Of String)
    For Each tx As String In  Strings.Split(in_text, " "c)
        '// 辞書から読みを取得
        If dic.ContainsKey(tx.ToUpper) Then
            lstText.Add(dic(tx.ToUpper))
        Else
            '// 最後の文字が「S」なら「S無し」で検索
            Dim lastw As String = tx.Substring(tx.Length-1, 1).ToUpper
            If lastw = "S" AndAlso dic.ContainsKey(tx.Substring(0, tx.Length-1).ToUpper) Then
                lstText.Add(dic(tx.Substring(0, tx.Length-1).ToUpper) & "ス")
            End If
        End If
    Next
    out_text = Strings.Join(lstText.ToArray, " "c)
    Console.writeLine(out_text)
Catch ex As Exception
    Console.writeLine(ex.ToString)
    Throw(ex)
End Try

終わりに

いかがでしたでしょうか。実装時に役立てば幸いです。
この記事が参考になったら、 LGTMをお願いします。閲覧ありがとうございました。

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0