ローマ字をひらがなに変換する関数
30分プログラム、その512。ローマ字をひらがなに変換する関数を書いてみた。
今日、b:id:banjunと"John"を"慈音"に変換できたらおもしろいんじゃね?みたな話をしてたので、とりあえずローマ字をひらがなに変換する関数を作ってみた。Schemeで。
本気でやるんだったら、SKKの辞書とかを流用したほうがいい気がするけど。あと、全部に対応するのは大変だったので、あ行とか行しか変換できない。
使い方
gosh> (romaji->hiragana "aka") ("あ" "か")
ソースコード
#! /opt/local/bin/gosh ;; -*- mode:scheme; coding:utf-8 -*- ;; ;; romaji.scm - ;; ;; Copyright(C) 2009 by mzp ;; Author: MIZUNO Hiroki / mzpppp at gmail dot com ;; http://howdyworld.org ;; ;; Timestamp: 2009/01/21 22:34:58 ;; ;; This program is free software; you can redistribute it and/or ;; modify it under MIT Lincence. ;; (define *table* '((a . あ) (i . い) (u . う) (e . え) (o . お) (ka . か) (ki . き) (ku . く) (ke . け) (ko . こ))) ;;; setup table (define *hash* (make-hash-table 'string=?)) (for-each (lambda (x) (hash-table-put! *hash* (symbol->string (car x)) (symbol->string (cdr x)))) *table*) (define *regexp* (string->regexp (string-append "^(?:" (string-join (sort (hash-table-keys *hash*) (lambda (x y) (> (string-length x) (string-length y)))) "|") ")"))) (define (romaji->hiragana romaji) (let1 match (*regexp* romaji) (if match (cons (hash-table-get *hash* (rxmatch-substring match)) (romaji->hiragana (rxmatch-after match))) '())))