わんすけに聞いてみる WinActor [WinActor]ひらがな⇔カタカナのフリガナ変換

[WinActor]ひらがな⇔カタカナのフリガナ変換

WinActorのフリガナ転記で、こっちに入力するときはカタカナでいいんだけど、こっちではひらがなで入れたいんだよねー・・・みたいな声が天から聞こえた。

 

めんどくさかったけども、思い立ったので書いてみました。

 

1.ひらがな⇔カタカナの変換

カタカナしか受け付けない入力フォームなのに英数混じってること良くあるヤン!

っていう事故対策で英数が混じってた時に英数までフリガナ化するか、排除するかっていう処理も追加してます。

いつもの如く、スクリプト実行ステージのスクリプトタブに張り付ければすぐに使えるスクリプトです。

inMOJI = !元の文字!
ConvType = !変換処理|ひらがな⇒カタカナ,カタカナ⇒ひらがな!
OptnType = !英数処理|排除,読み替え!
outMOJI = $変換後の文字$

tmpMOJI = ""

' 定義
hiragana = Array("ぁ","あ","ぃ","い","ぅ","う","ぇ","え","ぉ","お","か","が","き","ぎ","く","ぐ","け","げ","こ","ご","さ","ざ","し","じ","す","ず","せ","ぜ","そ","ぞ","た","だ","ち","ぢ","っ","つ","づ","て","で","と","ど","な","に","ぬ","ね","の","は","ば","ぱ","ひ","び","ぴ","ふ","ぶ","ぷ","へ","べ","ぺ","ほ","ぼ","ぽ","ま","み","む","め","も","ゃ","や","ゅ","ゆ","ょ","よ","ら","り","る","れ","ろ","わ","ゎ","ゐ","ゑ","を","ん")
katakana = Array("ァ","ア","ィ","イ","ゥ","ウ","ェ","エ","ォ","オ","カ","ガ","キ","ギ","ク","グ","ケ","ゲ","コ","ゴ","サ","ザ","シ","ジ","ス","ズ","セ","ゼ","ソ","ゾ","タ","ダ","チ","ヂ","ッ","ツ","ヅ","テ","デ","ト","ド","ナ","ニ","ヌ","ネ","ノ","ハ","バ","パ","ヒ","ビ","ピ","フ","ブ","プ","ヘ","ベ","ペ","ホ","ボ","ポ","マ","ミ","ム","メ","モ","ャ","ヤ","ュ","ユ","ョ","ヨ","ラ","リ","ル","レ","ロ","ワ","ヮ","ヰ","ヱ","ヲ","ン")
hankata = Array("ァ","ア","ィ","イ","ゥ","ウ","ェ","エ","ォ","オ","カ","ガ","キ","ギ","ク","グ","ケ","ゲ","コ","ゴ","サ","ザ","シ","ジ","ス","ズ","セ","ゼ","ソ","ゾ","タ","ダ","チ","ヂ","ッ","ツ","ヅ","テ","デ","ト","ド","ナ","ニ","ヌ","ネ","ノ","ハ","バ","パ","ヒ","ビ","ピ","フ","ブ","プ","ヘ","ベ","ペ","ホ","ボ","ポ","マ","ミ","ム","メ","モ","ャ","ヤ","ュ","ユ","ョ","ヨ","ラ","リ","ル","レ","ロ","ワ","ワ","イ","エ","ヲ","ン")

zenomj = Array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z")
zenkmj = Array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z")
hanomj = Array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z")
hankmj = Array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z")
yomikt = Array("エー","ビー","シー","ディー","イー","エフ","ジー","エイチ","アイ","ジェイ","ケー","エル","エム","エヌ","オー","ピー","キュー","アール","エス","ティー","ユー","ブイ","ダブリュー","エックス","ワイ","ゼット")

romanum = Array("Ⅰ","Ⅱ","Ⅲ","Ⅳ","Ⅴ","Ⅵ","Ⅶ","Ⅷ","Ⅸ","Ⅹ")
romayom = Array("ワン","ツー","スリー","フォー","ファイブ","シックス","セブン","エイト","ナイン","テン")

zennum = Array("0","1","2","3","4","5","6","7","8","9")
hannum = Array("0","1","2","3","4","5","6","7","8","9")
yominm = Array("ゼロ","イチ","ニ","サン","ヨン","ゴ","ロク","ナナ","ハチ","キュウ")

tanptn = Array("\d{9}","\d{8}","\d{7}","\d{6}","\d{5}","\d{4}","\d{3}","\d{2}", "\d{1}")
taninm = Array("オク","センマン","ヒャクマン","ジュウマン","マン","セン","ヒャク","ジュウ", "")


' 半角カナは全角カナにしておく
tmpMOJI = ConvAryStr(inMOJI, hankata, katakana)
' アルファベットは全角大文字でまとめとく
tmpMOJI = ConvAryStr(tmpMOJI, zenkmj, zenomj)
tmpMOJI = ConvAryStr(tmpMOJI, hankmj, zenomj)
tmpMOJI = ConvAryStr(tmpMOJI, hanomj, zenomj)
tmpMOJI = ConvAryStr(tmpMOJI, hankmj, zenomj)
' 数字は半角にしておく
tmpMOJI = ConvAryStr(tmpMOJI, zennum, hannum)

' 英数が混じっていた場合の対処
Select Case OptnType
  Case "排除"
    tmpMOJI = DeleteAryStr(tmpMOJI, yomikt)
    tmpMOJI = DeleteAryStr(tmpMOJI, hannum)
    tmpMOJI = DeleteAryStr(tmpMOJI, romanum)
  Case "読み替え"
    tmpMOJI = ConvAryStr(tmpMOJI, zenomj, yomikt)
    tmpMOJI = ConvAryStr(tmpMOJI, romanum, romayom)
    tmpMOJI = NumRead(tmpMOJI)
End Select

Select Case ConvType
  Case "ひらがな⇒カタカナ"
    tmpMOJI = ConvAryStr(tmpMOJI, hiragana, katakana)
  Case "カタカナ⇒ひらがな"
    tmpMOJI = ConvAryStr(tmpMOJI, katakana, hiragana)
End Select

Call SetUMSVariable(outMOJI, tmpMOJI)


' 以下、関数定義
Function ConvAryStr(SrcStr, BefAry, AftAry)
  Dim CnvStr
  CnvStr = SrcStr
  For i=0 To UBound(BefAry)
    CnvStr = Replace(CnvStr, BefAry(i), AftAry(i))
  Next
  ConvAryStr = CnvStr
End Function

Function DeleteAryStr(SrcStr, DelAry)
  Dim CnvStr
  CnvStr = SrcStr
  For i=0 To UBound(DelAry)
    CnvStr = Replace(CnvStr, DelAry(i), "")
  Next
  DeleteAryStr = CnvStr
End Function

Function NumRead(SrcStr)
  Dim CnvStr
  CnvStr = SrcStr
  Set reg = CreateObject("VBScript.RegExp")
  reg.Global = True
  ' 桁ごと判定
  For i=0 To UBound(tanptn)
    reg.Pattern = tanptn(i)
    Set mtchs = reg.Execute(CnvStr)
    For Each mtch In mtchs
      init = Left(mtch.Value, 1)
      initkn = ConvAryStr(init, hannum, yominm)
      cntin = Mid(mtch.Value, 2)
      If init = "0" Then
        CnvStr = Replace(CnvStr, mtch.Value, initkn & cntin)
      Else
        If init = "1" Then initkn = ""
        Do While Left(cntin, 1) = "0"
          cntin = Mid(cntin, 2)
        Loop
        CnvStr = Replace(CnvStr, mtch.Value, initkn & taninm(i) & cntin)
      End If
    Next
  Next
  NumRead = CnvStr
  Set reg = Nothing
End Function

 

2.設定タブは、こんな感じ。

  • 変換処理 : 「カタカナ⇒ひらがな」、「ひらがな⇒カタカナ」
  • 英数処理 : 「排除」、「読み替え」

なんとなくスクリプト書く手が暴走して、数字の桁を「ひゃく」とかちゃんと読めるロジック付けちゃったりして・・・。

 

例えば・・・、

元の文字「セイレキ2019」

変換後の文字「せいれきにせんじゅうきゅう」

みたいな。

使わねーわ、何やってんだろ。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

Related Post

[WinActor]CSVの差分抽出[WinActor]CSVの差分抽出

CSV系はもうね、それただのvbスクリプトでしょって話なんだけどもね。 GUI操作とか関係なく、なんか、もちろんやれるよね?的な雰囲気ですよ。 たとえば、 「毎日、特定ステータスのレコードをCSVでダンプ出力してるんだけ […]

[WinActor]変数が参照されているため、削除することができません。[WinActor]変数が参照されているため、削除することができません。

ノードやライブラリで変数が使われている状態で変数一覧から変数を削除しようとすると、このエラーがでます。 %変数名%表記で使われている変数ではこのメッセージがでないので注意しましょう。 ※参考 : 変数の値をメッセージやコ […]