[Message Prev][Message Next][Thread Prev][Thread Next][Message Index][Thread Index]

[MD:705] Re: [MD:682]about IMM API



後藤です

>>>>> From: Miyashita Hisashi(宮下 尚:HIMI) <himi@xxxxxxxxxxxxxxxxxxxxxxxxx>
> それじゃ、IMMの講義 1回目

なんだか興味が湧いて、練習がてらにこんなものを作ってみました。

postal.el -- IMEを使って郵便番号から住所文字列をel

人が手動でIMEつかって変換する分には、こんなの要らないのですが、
elispプログラムで ZIP -> ADDRESS変換するのに使えます。
#IMEを使うため、当然 Meadow限定ですが。。。(^^;

きっかけはBBDBをあれこれいじってるときに、「郵便番号がわかってりゃ
住所はほとんど入力しなくてもいいじゃん」と思ったからなのですが、
いつかはなにかの役に立つかも、です。(^o^)

(postal-zip-to-address-list "100-6090") とか
(postal-zip-to-address-list "060-0002") とか
(postal-zip-to-address-list-2 "060-0002") とか試してみてちょ。
M-x postal-zip-to-address-insert もね


P.S.
Microsoftが配布している7桁郵便番号辞書を使うと、
住所のうちのかなりの部分が出てくるのが楽しい。

--- Regards,
 Shun-ichi Goto  <gotoh@xxxxxxxxxxx>
   R&D Group, TAIYO Corp., Tokyo, JAPAN
;; -*- Emacs-Lisp -*-
;;
;; postal.el 
;; $Revision: 1.1 $
;;
;; Author: Shun-ichi GOTO <gotoh@xxxxxxxxxxx>
;; Create: Sat Apr 11 22:38:02 1998

;; Commentary:
;;

;; Code:

(defvar postal-zip-allow-katakana nil
  "Allow katakana-jisx0201 character in candidates")

(defvar postal-zip-allow-ascii nil
  "Allow ascii character in candidates")

(defconst postal-zip-old-style-regex-1
  "^[0-9][0-9][0-9]*$"
  "Regexp for 3 digits style (old) zip number")

(defconst postal-zip-old-style-regex-2
  "^\\([0-9][0-9][0-9]\\)-?\\([0-9][0-9]\\)$"
  "Regexp for 5 digits style (old) zip number")

(defconst postal-zip-new-style-regex
  "^\\([0-9][0-9][0-9]\\)-?\\([0-9][0-9][0-9][0-9]\\)$"
  "Regexp for 7 digits style zip number")


(defun postal-zip-to-address-list (zip)
  "Convert ZIP to address with IME postal dictionaly (Meadow Only).
Retuns list of candadates, or nil if no candidate.
1st argument ZIP is string or integer.
If ZIP is integer, convert into string beore format check.
ZIP string is allowed in following formats:
 \"123\"                  ... old-style 3 digits
 \"123-45\", \"12345\"      ... old-style 5 digits
 \"123-4567\", \"1234567\"  ... new-style 7 digits
"
  (if (not (featurep 'meadow))
      (error "function postal-zip-to-address is Meadow only feature"))
  (let (agent compl (ret nil))
    ;; integer => zip-string
    (if (integerp zip)
	(setq zip (cond 
		   ((< zip 1000)	; old style 3 digits
		    (format "%03d" zip))
		   ((< zip 100000)	; old style 5 digits
		    (format "%05d" zip))
		   (t			; may be new style 7 digits
		    (format "%07d" zip)))))
    ;; check format : "123" or "123-45" is old style
    ;;                "123-4567" is new style
    ;;                (hyphen can be ommit)
    (cond 
     ((not (stringp zip))
      (error "Argument should be string or integer"))
     ((string-match "[^-0-9]" zip)
      (error "unexpected char in ZIP string"))
     ((string-match postal-zip-old-style-regex-1 zip)
      ;; old style
      )
     ((or (string-match postal-zip-old-style-regex-2 zip) ; old style
	  (string-match postal-zip-new-style-regex zip))  ; new style
      ;; make hyphenated zip string (for IME)
      (setq zip (concat (substring zip (match-beginning 1) (match-end 1))
			"-"
			(substring zip (match-beginning 2) (match-end 2))))
      )
     (t (error "Invalid ZIP format")))
    ;; now convert with IME
    (setq agent (w32-ime-create-conversion-agent)) 
    (unwind-protect ; to destroy agent
	(progn
	  (w32-ime-set-composition-string
	   agent
	   (list (cons zip 'target-converted))
	   'string
	   t)
	  (w32-ime-deal-with-context agent 'convert)
	  (setq compl (w32-ime-get-composition-string agent 'comp))
	  (if (and (= (length compl) 1)	; check single composition
		   (string= zip (car (car compl))))
	      (progn
		(condition-case nil
		    (w32-ime-open-candidate agent 0)
		  (error))
		(mapcar
		 '(lambda (x)
		    (if (or (not (stringp x))
			    (and (not postal-zip-allow-ascii)
				 (memq 'ascii (find-charset-string x)))
			    (and (not postal-zip-allow-katakana)
				 (memq 'katakana-jisx0201
				       (find-charset-string x)))
			    (not (string-match "^.?.?.?[都道府県]" x)))
			()
		      (setq ret (cons x ret))
		      ))
		 (w32-ime-get-candidate-list agent 0)))))
      (w32-ime-destroy-conversion-agent agent))
    (nreverse ret)))


(defun postal-zip-to-address-list-2 (zip)
  "Convert ZIP to address, then return max-matched string and rests
"
  (let ((cl (postal-zip-to-address-list zip))
	max len)
    (if (null cl)
	()
      ;; make maximum matched string
      (setq max (try-completion "" (mapcar '(lambda (x) (list x)) cl)))
      ;; make return value
      (if (= (length cl) 1)
	  cl				; only 1 cadicates ... as is
	(setq len (length max))
	(cons max (mapcar '(lambda (x) (substring x len)) cl))))
    ))
      
(defun postal-zip-to-address-insert (zip &optional buffer)
  ""
  (interactive "sZIP: ")
  (let ((addr (car (postal-zip-to-address-list-2 zip))))
    (if (or (null buffer)
	    (eq buffer t))
	(insert addr)			; inseret to current buffer
      (save-excursion
	(set-buffer buffer)
	(insert addr)))
    ))

;; postal.el ends here