Skip to content

Instantly share code, notes, and snippets.

@ksasao
Last active September 11, 2023 07:04
Show Gist options
  • Save ksasao/4a89960735be8cbc8b2c9bd15c466ce1 to your computer and use it in GitHub Desktop.
Save ksasao/4a89960735be8cbc8b2c9bd15c466ce1 to your computer and use it in GitHub Desktop.
C#による漢字→かな変換。参照>COMで、Microsoft Excel XX.X Object Library を参照に追加する。
using System;
using System.IO;
using System.Runtime.InteropServices;
using ExcelApplication = Microsoft.Office.Interop.Excel.Application;
using Microsoft.VisualBasic;
namespace Kanji2Yomi
{
class Program
{
static void Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine("usage: kanji2yomi target_file");
return;
}
ExcelApplication objExcel = new ExcelApplication();
try
{
string path = args[0];
string[] src = File.ReadAllLines(path);
string[] dest = new string[src.Length];
for(int i = 0; i < src.Length; i++)
{
if (src[i].Trim() == "")
{
dest[i]="";
continue;
}
string katakana = objExcel.GetPhonetic(src[i]);
string hiragana = Strings.StrConv(katakana, VbStrConv.Hiragana);
dest[i] = katakana;
}
File.WriteAllLines(path+".yomi.txt", dest);
}
catch (COMException ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment