1
;;; predict-google-suggest.scm: google suggest prediction module
3
;;; Copyright (c) 2011- uim Project http://code.google.com/p/uim/
5
;;; All rights reserved.
7
;;; Redistribution and use in source and binary forms, with or without
8
;;; modification, are permitted provided that the following conditions
10
;;; 1. Redistributions of source code must retain the above copyright
11
;;; notice, this list of conditions and the following disclaimer.
12
;;; 2. Redistributions in binary form must reproduce the above copyright
13
;;; notice, this list of conditions and the following disclaimer in the
14
;;; documentation and/or other materials provided with the distribution.
15
;;; 3. Neither the name of authors nor the names of its contributors
16
;;; may be used to endorse or promote products derived from this software
17
;;; without specific prior written permission.
19
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
20
;;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
;;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
;;; ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
23
;;; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25
;;; OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26
;;; HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27
;;; LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28
;;; OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32
(require-extension (srfi 1))
33
(require "http-client.scm")
37
(require-dynlib "expat")
39
(define-class predict-google-suggest predict
42
(internal-charset "UTF-8")
48
(define google-suggest-charset-alist
49
'((ja . "Shift-JIS")))
51
(class-set-method! predict-google-suggest parse
52
(lambda (self xml-str)
53
(let ((parser (xml-parser-create "UTF-8"))
56
(define (elem-start name atts)
57
(if (and (equal? name "suggestion")
58
(equal? path '("toplevel" "CompleteSuggestion")))
59
(set! data (append data
61
(filter (lambda (x) (equal? (car x) "data")) atts)))))
62
(set! path (append path (list name))))
63
(define (elem-end name)
64
(set! path (drop-right path 1)))
67
(xml-element-handler-set! parser elem-start elem-end)
68
(xml-parse parser xml-str 1)
72
(class-set-method! predict-google-suggest suggest
74
(define (iconv-convert to-code from-code from-str)
75
(if (equal? to-code from-code)
77
(and-let* ((ic (iconv-open to-code from-code))
78
(to-str (iconv-code-conv ic from-str)))
81
(define google-suggest-server
82
(if (predict-google-suggest-use-ssl self)
83
"encrypted.google.com"
86
(if (assq (predict-google-suggest-language self)
87
google-suggest-charset-alist)
88
(format "&hl=~a" (symbol->string (predict-google-suggest-language self)))
90
(define (string->lang str)
91
(if (assq (predict-google-suggest-language self)
92
google-suggest-charset-alist)
93
(iconv-convert "UTF-8"
94
(assq-cdr (predict-google-suggest-language self)
95
google-suggest-charset-alist)
98
(and-let* ((uri-string (predict->internal-charset self str)))
99
(let* ((proxy (make-http-proxy-from-custom))
100
(ssl (and (predict-google-suggest-use-ssl self)
101
(make-http-ssl (SSLv3-client-method) 443)))
102
(result (http:get google-suggest-server
103
(format "/complete/search?output=toolbar&q=~a~a"
109
(parsed (predict-google-suggest-parse self (string->lang result))))
111
(predict->external-charset self s))
114
(class-set-method! predict-google-suggest search
116
(let* ((suggest (predict-google-suggest-suggest self
118
(ret (if (< (predict-google-suggest-limit self) (length suggest))
119
(take suggest (predict-google-suggest-limit self))
124
(map (lambda (x) "") (iota (length ret)))))))
127
(define (make-predict-google-suggest-with-custom)
128
(let ((obj (make-predict-google-suggest)))
129
(predict-google-suggest-set-limit! obj predict-custom-google-suggest-candidates-max)
130
(predict-google-suggest-set-language! obj predict-custom-google-suggest-language)
131
(predict-google-suggest-set-use-ssl! obj predict-custom-google-suggest-use-ssl)