Thread Rating:
  • 536 Vote(s) - 2.77 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Google サジェストから候補データを取得し、オートコンプリートを作る
07-07-2011, 10:49 AM, (This post was last modified: 07-07-2011, 10:50 AM by hokada.)
#1
Google サジェストから候補データを取得し、オートコンプリートを作る
Google サジェストから候補データを取得し、オートコンプリート機能を作るサンプルです。

以下のようにhttp://suggestqueries.google.comにリクエストを投げれば、JSONが返ってくるので、それを解析するだけでOK。

Code:
{define-proc {find-google-suggestions
                 word:String,
                 host-locale:Locale = {get-host-locale}
             }:{Array-of String}
    def query =
        "http://suggestqueries.google.com/complete/search?hl=%s&qu=%s&json=t"
    def suggestions = {{Array-of String}}
    def qu = {url-encode-string word}
    {with-open-streams
        tis = {read-open {url {format query, host-locale.name, qu}}}
     do
        def results = {JsonValue-parse tis} asa JsonArray
        {for v in results[1] asa JsonArray do
            {suggestions.append v asa String}
        }
    }
    {return suggestions}
}

{define-class public open GoogleSuggestionProvider
  {inherits AutocompleteProvider}

  {method public open {refresh acf:AutocompleteMixin}:void
    def word = acf.text-field.value
    {if word.size > 0 then
        {acf.populate-from-strings
            {find-google-suggestions word}
        }
    }
  }

  {constructor public {default max-results:int = 10}
    {construct-super max-results}
  }
}

{value
    {AutocompleteTextField
        height = 1cm,
        width = 10cm,
        font-size = 14px,
        {GoogleSuggestionProvider},
        {on FocusIn at acf:AutocompleteTextField do
            {acf.provider.refresh acf}
        }
    }
}

この中では、Curl External LibraryのAutoCompleteTextFieldコントロールを使っています。


Attached Files
.zip   Google Suggestion API.zip (Size: 8.9 KB / Downloads: 976)


Possibly Related Threads...
Thread Author Replies Views Last Post
  Google Translation APIを使って翻訳 hokada 1 12,003 08-02-2011, 03:07 AM
Last Post: RobertShiplett
Forum Jump:


Users browsing this thread:
2 Guest(s)

MyBB SQL Error

MyBB has experienced an internal SQL error and cannot continue.

SQL Error:
1017 - Can't find file: 'mybb_threadviews' (errno: 2)
Query:
INSERT INTO mybb_threadviews (tid) VALUES('105')