From: Dag.H.Wanvik@kvatro.no (Dag H. Wanvik)
Subject: Re: Tone marks for Pin-Yin using LaTeX?
Organization: Kvatro A/S, Trondheim, Norway
Date: 29 Sep 92 18:15:48

>>>>> "Vincent" == Vincent Cate <vac> writes:

Vincent> I am learning spoken Chinese.  I would like to be able to
Vincent> edit Pin-Yin complete with tone marks so I could make a small
Vincent> dictionary of the words I should know for class.  Does anyone
Vincent> have a way of doing this?  (in LaTeX, Scribe, FrameMaker,
Vincent> Troff, anything?)

Do you have to use LaTeX? 

I am also learning Chinese and I feel much could be done with the
existing GB utilities, GB2PS and Emacs.

cxterm.el lets you edit GB text using cxterm and Emacs. I have hacked
some utilities to "go the other way", i.e. given a GB character, what
is its pinyin? Taking this a bit further, given a region/buffer of GB
text, merge in pinyin between the lines (great for reading Chinese
text if you are unsure about the pronuniciation of some characters;
also makes it much faster to look up Chinese in a dictionary :-)

Of course, this presumes that your material in already in GB...

However, using Emacs Lisp it should be easy to make a small dictionary
(check out pig-mode.el by zhou@okra.fnal.gov (Ping Zhou) and adapt
that code to learner's glossary book. This would enable easy query and
printing (using GB2PS)... In fact, I have been toying with the idea
for some time..

I could let this query me with either the Chinese character OR the
pin-yin of a Chinese word, and it would record my scores, allowing
"easy" words to gradually slip into the background; thus keeping my
efforts on the new and/or difficult words...

Let me know what else you find :-)

Dag

Enclosure:

----------------------------------------8<--------------------hanyu.el
(defvar big5-dict "/u1/dag/cx/cxterm/Dict/PY-b5.tit")
(defvar gb-dict "/u1/dag/cx/cxterm/Dict/TONEPY.tit")

(defun lookup-sound (file)
  "Return list of possible pinyin strings for character after point."
  (save-excursion
    (let ((char (buffer-substring (point) (+ (point) 2)))
	  tmp
	  (results nil))
      (find-file file)
      (goto-char (point-min))
      (search-forward "BEGINDICTIONARY")
      (while (search-forward char nil t)
	(beginning-of-line)
	(if (not (looking-at "[a-z][a-z:]*[1-5]?"))
	    (error "Hmm...")
	  (setq tmp (buffer-substring (match-beginning 0)
				      (match-end 0)))
	  (if (encoding-start char)
	      (setq results (cons tmp results))))
	(beginning-of-line 2))
      (bury-buffer (current-buffer))
      results)))
  
(defun encoding-start (str)
  ;; Check if str starts at even pos in encoding
  (save-excursion
    (let (found
	  (case-fold-search nil))
      (search-forward "\t")
      (while (and (not (eolp))
		  (not found))
	(if (string-equal str (buffer-substring (point) (+ (point) 2)))
	    (setq found t)
	  (forward-char 2)))
      found)))
      
(defun b5-lookup (arg)
  "Print pinyin of b5 character under point in message area.
If called with prefix argument, just return the pinyin as a string."
  (interactive "P")
  (let ((str-list (lookup-sound big5-dict)))
    (if (not arg)
	(message "%s" str-list)
      str-list)))
  
(defun gb-lookup (arg)
  "Print pinyin of gb character under point in message area.
If called with prefix argument, just return the pinyin as a string."
  (interactive "P")
  (let ((str-list (lookup-sound gb-dict)))
    (if (not arg)
	(message "%s" str-list)
      str-list)))

(defun gb2pinyin (from to)
  "Merge lines in region containing gb characters with pinyin translation."
  (interactive "r")
  (pinyinize 'gb-lookup from to)
  (pinyin-merge from to))

(defun b52pinyin (from to)
  "Merge lines in region containing b5 characters with pinyin translation."
  (interactive "r")
  (pinyinize 'b5-lookup from to)
  (pinyin-merge from to))

(defun pinyinize (func from to)
  (let (str)
    (save-window-excursion
      (save-excursion
	(save-restriction
	  (with-output-to-temp-buffer "*pinyin*"
	    (narrow-to-region from to)
	    (goto-char (point-min))
	    (while (not (eobp))
	      (if (> (following-char) 127)
		  (setq str (funcall func t))
		(setq str nil))
	      (if (not str)
		  (if (> (following-char) 127)
		      ;; something else...
		      (progn
			(princ " ** ")
			(forward-char 2))
		    (princ (buffer-substring (point) (1+ (point))))
		    (forward-char 1))
		(princ " ")
		(if (= (length str) 1)
		    (princ (car str))
		  (princ str))
		(forward-char 2)))))))))


(defun chew-py-line ()
  (let (from
	(old-buf (current-buffer)))
    (set-buffer "*pinyin*")
    (setq from (point))
    (prog1
	(if (not (eobp))
	    (progn
	      (beginning-of-line 2)
	      (buffer-substring from (point)))
	  nil)
      (set-buffer old-buf))))
  
(defun pinyin-merge (from to)
  (save-excursion
    (let ((old-buf (current-buffer))
	  pin start)
      (goto-char (min from to))
      (set-buffer "*pinyin*")
      (goto-char (point-min))
      (set-buffer old-buf)
      (while (setq pin (chew-py-line))
	(setq start (point))
	(beginning-of-line 2)
	(if (string-equal (buffer-substring start (point))
			  pin)
	    ()
	  (setq start (point))
	  (insert "\n" pin "\n")
	  (fill-region start (point)))))))
     
(defun hz2gb ()
  "Convert text in region from hz to gb"
  (interactive)
  (save-excursion
    (setq buffer-read-only nil)
    (shell-command-on-region (point-min) (point-max) "hz2gb" t)))

(defun gb2hz ()
  "Convert text in region from gb to hz"
  (interactive)
  (save-excursion
    (setq buffer-read-only nil)
    (shell-command-on-region (point-min) (point-max) "gb2hz" t)))

(defun hz2gb-article ()
  "Function called when in Subject buffer of GNUS to convert gb to hz."
  (interactive)
  (save-excursion
    (set-buffer "*Article*")
    (hz2gb)))
  
(defun gb2hz-article ()
  "Function called when in Subject buffer of GNUS to convert hz to gb."
  (interactive)
  (save-excursion
    (set-buffer "*Article*")
    (gb2hz)))

  

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Dag H. Wanvik  ,   Senior Systems Engineer
Internet e-mail:   wanvik@kvatro.no
Mailing address:   KVATRO AS, Pirsenteret, N-7005 Trondheim, NORWAY
Telephone      :   +47 7 520090
Fax            :   +47 7 520140
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Dag H. Wanvik  ,   Senior Systems Engineer
Internet e-mail:   wanvik@kvatro.no
Mailing address:   KVATRO AS, Pirsenteret, N-7005 Trondheim, NORWAY
Telephone      :   +47 7 520090
Fax            :   +47 7 520140
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~