[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Erbot-cvs] erbot contrib/translate.el ./erbtranslate.el ./...
From: |
Vivek Dasmohapatra |
Subject: |
[Erbot-cvs] erbot contrib/translate.el ./erbtranslate.el ./... |
Date: |
Fri, 12 May 2006 02:02:22 +0000 |
CVSROOT: /sources/erbot
Module name: erbot
Branch:
Changes by: Vivek Dasmohapatra <address@hidden> 06/05/12 02:02:22
Modified files:
contrib : translate.el
. : erbtranslate.el ChangeLog
Log message:
Moved translation program calling code to contrib/translate.el
CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/erbot/erbot/contrib/translate.el.diff?tr1=1.1&tr2=1.2&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/erbot/erbot/erbtranslate.el.diff?tr1=1.19&tr2=1.20&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/erbot/erbot/ChangeLog.diff?tr1=1.57&tr2=1.58&r1=text&r2=text
Patches:
Index: erbot/ChangeLog
diff -u erbot/ChangeLog:1.57 erbot/ChangeLog:1.58
--- erbot/ChangeLog:1.57 Wed May 10 21:22:37 2006
+++ erbot/ChangeLog Fri May 12 02:02:22 2006
@@ -1,3 +1,15 @@
+2006-05-12 Vivek Dasmohapatra <address@hidden>
+
+ * contrib/translate.el: the symbol -> string coercion is not
+ required for arguments to translate.el, that's something
+ specific to user-visible erbot functions.
+
+ * erbtranslate.el: most translation code moved to translate.el
+ fsi-translate-web-page temporarily disabled till I've tested
+ it a bit more and made the implementation a little smarter
+ than it currently is. (It should check ti see if a web page
+ service is actualy available)
+
2006-05-10 D Goel <address@hidden>
* erbtranslate.el: update authors
Index: erbot/contrib/translate.el
diff -u erbot/contrib/translate.el:1.1 erbot/contrib/translate.el:1.2
--- erbot/contrib/translate.el:1.1 Thu May 11 00:17:57 2006
+++ erbot/contrib/translate.el Fri May 12 02:02:22 2006
@@ -8,7 +8,8 @@
;; Created: 2006-05-10
;; Description: use gnome translate/libtranslate to translate text
;; Compatibility: Emacs21, Emacs22
-;; Last modified: Thu 2006-05-11 01:06:34 +0100
+;; Last modified: Fri 2006-05-12 02:52:44 +0100
+
;; Based on work by:
;; Deepak Goel <address@hidden>
@@ -151,8 +152,6 @@
(defun translate-list-pairs (&optional from to)
"Return the subset of `translate-pairs' that matches the FROM and TO
arguments."
- (setq from (format "%s" (or from "*"))
- to (format "%s" (or to "*")))
(if (string-match "^\\(?:\*\\|any\\|-\\|\\)$" from) (setq from nil))
(if (string-match "^\\(?:\*\\|any\\|-\\|\\)$" to ) (setq to nil))
(if (not (translate-load-pairs))
Index: erbot/erbtranslate.el
diff -u erbot/erbtranslate.el:1.19 erbot/erbtranslate.el:1.20
--- erbot/erbtranslate.el:1.19 Wed May 10 21:22:37 2006
+++ erbot/erbtranslate.el Fri May 12 02:02:22 2006
@@ -1,16 +1,16 @@
;;; erbtranslate.el --- Natural Language translation functions.
-;; Time-stamp: <2006-05-10 17:19:17 deego>
+;; Time-stamp: <2006-05-12 02:53:13 +0100 vivek>
;; Copyright (C) 2002 Alejandro Benitez
;; Emacs Lisp Archive entry
-;; Filename: erbc
+;; Filename: erbtranslate.el
;; Package: erbot
;; Authors: Alejandro Benitez <address@hidden>,
;; Vivek Dasmohapatra <address@hidden>
;; Deepak Goel <address@hidden>
-;; Version: 0.0DEV
+;; Maintainer: Vivek Dasmohapatra <address@hidden>
+;; Version: 0.1DEV
;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErBot
-
;; This file is NOT (yet) part of GNU Emacs.
;; This is free software; you can redistribute it and/or modify
@@ -33,239 +33,92 @@
;; http://packages.ubuntu.com/dapper/libs/libtranslate-bin
;; See also:
-;; This file needs shs.el
-;;(require 'shs)
-
-
-(defvar erbtranslate-version "0.0dev")
+(defvar erbtranslate-version "0.1dev")
-(defvar erbtranslate-pairs nil
- "A cache for the language pairs. A list of entries of the form: \n
- '((fromaliases) (toaliases) (types)).\n
-The first elements of fromaliases and toaliases are the canonical two letter
-language codes (possibly with a -XX country variant extension). Any remaining
-elements are human-readable aliases. (types) is a list of translation types,
-usually text, and occasionally web-page as well. No other types are currently
-known.")
-
-(defvar erbtranslate-unsupported-langs '("ar" "he")
- "Languages (two letter codes) that we cannot utf-8 encode yet.")
+(require 'translate)
(defun erbtranslate-enabled-check ()
(erbutils-enabled-check erbn-translate-p))
(defalias 'fsi-t8 'fsi-translate)
-(defvar erbn-translate-program "translate"
- "External program")
-
(defcustom erbn-translate-p nil
"Enabling this should be completely safe. We do use call-process
here whenever passing any arguments to external commands.")
-(defun erbtranslate-req-to-pair (from to)
- (let ( (code nil) )
- (mapc
- (lambda (p)
- (if (and (member-ignore-case from (car p))
- (member-ignore-case to (cadr p)))
- (setq code (cons (caar p) (car (cadr p))) )) )
- erbtranslate-pairs)
- code))
-
-(defun erbtranslate-full-name (code-or-name)
- "Return the full name of a language based on a code or one of its aliases."
- (let ((name nil) (lang nil) (ldata erbtranslate-pairs))
- (while (and ldata (not name))
- (setq lang (car ldata) ldata (cdr ldata))
- (if (member-ignore-case code-or-name (car lang))
- (setq lang (car lang))
- (if (member-ignore-case code-or-name (cadr lang))
- (setq lang (cadr lang))
- (setq lang nil)))
- (when lang
- (setq name (mapconcat (lambda (l) (format "%s" l)) (cdr lang) " ")) ))
- name))
-
(defun fsi-translate (from to &rest text)
(erbtranslate-enabled-check)
- (require 'shs)
- (setq text (mapconcat #'(lambda (arg) (format "%s" arg)) text " "))
- ;; =======================================================================
- ;; the temp file should be written as utf-8, hence coding-system-for-write
- ;; -----------------------------------------------------------------------
- ;; since we dump the file contents already encoded to utf-8 (that's what
- ;; libtranslate expects), we must force the process to 'no-conversion to
- ;; avoid double-encoding (with process-coding-system-alist).
- ;; -----------------------------------------------------------------------
- ;; we might have to force the locale, according to the translate docs,
- ;; but this doesn't actually seem to be necessary at the moment.
- ;; -----------------------------------------------------------------------
- (let ((process-coding-system-alist '(("." . no-conversion)))
- (coding-system-for-write 'utf-8)
- (translation nil)
- (code nil)
- (from-lang (format "%s" from))
- (to-lang (format "%s" to))
- ;;(locale (getenv "LC_ALL"))
- )
- (setq code (erbtranslate-req-to-pair from-lang to-lang))
- (cond
- ( (not code)
- (concat (format "%s -> %s: no matching translation services found.\n"
- from-lang to-lang)
- "Syntax: translate FROM TO TEXT\n") )
- ( (member (car code) erbtranslate-unsupported-langs)
- (format "Sorry, unicode support for %s is not yet complete."
- (erbtranslate-full-name from-lang)) )
- ( (member (cdr code) erbtranslate-unsupported-langs)
- (format "Sorry, unicode support for %s is not yet complete."
- (erbtranslate-full-name to-lang)) )
- (t
- (setq translation
- (shsp (list erbn-translate-program
- "-f" (car code) "-t" (cdr code)) nil text)
- translation (decode-coding-string translation 'utf-8))
- translation)) ))
+ (setq text (mapconcat #'(lambda (arg) (format "%s" arg)) text " ")
+ from (format "%s" from)
+ to (format "%s" to ))
+ (condition-case caught
+ (translate from to text)
+ (error (concat "libtranslate error:" (cdr caught) )) ))
(defalias 'fsi-t8-l 'fsi-translate-list-pairs)
-(makunbound 'erbtranslate-pair-regex)
-(defconst erbtranslate-pair-regex
- (concat "^\\([a-z]\\{2,3\\}\\(?:-..\\)?\\)" ;; language code (from)
- "\\s-+"
- "(\\(.*\\))" ;; language names (from)
- "\\s-+->\\s-+"
- "\\([a-z]\\{2,3\\}\\(?:-..\\)?\\)" ;; language code (to)
- "\\s-+"
- "(\\(.*\\)):" ;; language aliases (to)
- "\\s-+"
- "\\(.*\\)")) ;; capabilities
-
-(defun erbtranslate-parse-pair (pair-line)
- "Parse a single line of output from translate --list-pairs, returns
-an element for insertion into erbtranslate-pairs."
- (if (string-match erbtranslate-pair-regex pair-line)
- (let ( (from (match-string 1 pair-line))
- (from-alias (match-string 2 pair-line))
- (to (match-string 3 pair-line))
- (to-alias (match-string 4 pair-line))
- (cap (match-string 5 pair-line))
- (cleanup (lambda (x) (replace-regexp-in-string ",.*" "" x)))
- (from-names nil)
- (to-names nil))
- (setq from-alias (split-string from-alias ";")
- to-alias (split-string to-alias ";")
- from-alias (mapcar cleanup from-alias)
- to-alias (mapcar cleanup to-alias )
- cap (split-string cap ",\\s-+"))
- (mapc (lambda (x)
- (let ((pos 0))
- (while (setq pos (string-match "\\<\\(\\S-+\\)\\>" x pos))
- (setq from-names (cons (match-string 1 x) from-names)
- pos (match-end 1)) )))
- from-alias)
- (mapc (lambda (x)
- (let ((pos 0))
- (while (setq pos (string-match "\\<\\(\\S-+\\)\\>" x pos))
- (setq to-names (cons (match-string 1 x) to-names)
- pos (match-end 1)) )))
- to-alias)
- (list (cons from from-names)
- (cons to to-names ) cap))
- (message "%S does not match.\n" pair-line) nil))
-
-(defun erbtranslate-load-pairs ()
- "Parse the output of `erbn-translate-program' -l into `erbtranslate-pairs'"
- (when (not erbtranslate-pairs)
- (let ( (y nil)
- (pair-text (erbn-shell-command-to-string
- (concat erbn-translate-program " -l") '(t))) )
- (mapc
- (lambda (x)
- (when (setq y (erbtranslate-parse-pair x))
- (setq erbtranslate-pairs (cons y erbtranslate-pairs))))
- (split-string pair-text "\n")) ))
- erbtranslate-pairs)
-
(defun fsi-translate-list-pairs (&optional from to &rest args)
"Allow the user to search for translation pairs. Only gives counts
unless both from and to are specified. *, any, - are allowed as wildcards."
(erbtranslate-enabled-check)
- (setq from (format "%s" (or from "*"))
- to (format "%s" (or to "*")))
- (if (string-match "^\\(?:\*\\|any\\|-\\|\\)$" from) (setq from nil))
- (if (string-match "^\\(?:\*\\|any\\|-\\|\\)$" to ) (setq to nil))
- (if (not (erbtranslate-load-pairs))
- "translate doesn't seem to have been setup - no languages found."
+ (let ((pair-data))
+ (setq from (format "%s" (or from "*"))
+ to (format "%s" (or to "*"))
+ pair-data (translate-list-pairs from to))
+ (if (string-match "^\\(?:\*\\|any\\|-\\|\\)$" from) (setq from nil))
+ (if (string-match "^\\(?:\*\\|any\\|-\\|\\)$" to ) (setq to nil))
(cond
( (and (not from) (not to)) ;; neither end point specified
(concat
- (format "%d language pair(s) available.\n" (length erbtranslate-pairs))
+ (format "%d language pair(s) available.\n" (length pair-data))
"Specify an origin and/or destination language to see a list:\n"
" translate-list-pairs es ja\n"
" translate-list-pairs castilian\n"
" translate-list-pairs * zh-TW\n") )
( (or (not to) (not from)) ;; one end point specified
(let ( (dir (if from "From" "To"))
- (op (if from 'car 'cadr))
- (op2 (if from 'cadr 'car))
+ (op (if from 'cadr 'car))
(s nil)
- (x 0)
+ (x (length pair-data))
(fl (format "%s" (or from to))) )
- (mapc
- (lambda (p) (if (member-ignore-case fl (funcall op p))
- (setq x (1+ x)
- s (cons (car (funcall op2 p)) s))))
- erbtranslate-pairs)
- (setq fl (or (erbtranslate-full-name fl) fl))
+ (setq s (mapcar (lambda (p) (car (funcall op p))) pair-data)
+ fl (or (translate-full-name fl) fl))
(apply 'concat
(format "%s %s: %d language(s) available.\n" dir fl x)
(if (<= (length s) 100)
(list
(mapconcat
- (lambda (x) (erbtranslate-full-name x)) s ", ")) ))) )
+ (lambda (x) (translate-full-name x)) s ", ")) ))) )
(t ;; fully spec'd translation
- (let ( (s nil)
- (x 0)
- (fl (format "%s" from))
- (tl (format "%s" to )) )
- (mapc
- (lambda (p)
- (if (and (member-ignore-case fl (car p))
- (member-ignore-case tl (cadr p)))
- (setq x (1+ x) s (cons p s)) ))
- erbtranslate-pairs)
- (setq fl (or (erbtranslate-full-name fl) fl)
- tl (or (erbtranslate-full-name tl) tl))
+ (let ( (x (length pair-data)) )
+ (setq from (or (translate-full-name from) from)
+ to (or (translate-full-name to ) to ))
(apply 'concat
- (format "%s -> %s: %d pair(s) available.\n" fl tl x)
+ (format "%s -> %s: %d pair(s) available.\n" from to x)
(mapcar (lambda (x)
(format "%s -> %s\n"
(princ (car x))
- (princ (cadr x)))) s)) )) ) ))
+ (princ (cadr x)))) pair-data)) )) ) ))
(defalias 'fsi-t8-s 'fsi-translate-list-services)
-
(defun fsi-translate-list-services (&rest args)
(erbtranslate-enabled-check)
(erbn-shell-command-to-string
(concat erbn-translate-program " --list-services")
'(t)))
+;; temporarily disabled till clean support is provided by translate.el
+;; (defun fsi-translate-web-page (from to url &rest args)
+;; (erbtranslate-enabled-check)
+;; (shsp (list erbn-translate-program
+;; "-f"
+;; (format "%s" from) "-t"
+;; (format "%s" to)
+;; (format "%s" url))))
-
-(defun fsi-translate-web-page (from to url &rest args)
- (erbtranslate-enabled-check)
- (shsp (list erbn-translate-program
- "-f"
- (format "%s" from) "-t"
- (format "%s" to)
- (format "%s" url))))
-
-(defalias 'fsi-t8-w 'fsi-translate-web-page)
+;; (defalias 'fsi-t8-w 'fsi-translate-web-page)
(provide 'erbtranslate)
;;; erbtranslate.el ends here
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Erbot-cvs] erbot contrib/translate.el ./erbtranslate.el ./...,
Vivek Dasmohapatra <=