ajax - Script like google suggest in python -
i writing script works google suggest. problem trying suggestion next 2 words. example uses txt file working_bee.txt. when writing text "mis" should suggestions "miss mary , miss taylor, ...". "miss, ...". suspect ajax responsetext method gives single word? ideas wrong?
# looks google suggest
def count_words(xfile): frequency = {} words=[] l in open(xfile, "rt"): l = l.strip().lower() r in [',', '.', "'", '"', "!", "?", ":", ";"]: l = l.replace(r, " ") words += l.split() in range(len(words)-1): frequency[words[i]+" "+words[i+1]] = frequency.get(words[i]+" "+words[i+1], 0) + 1 return frequency # read valid words file ws = count_words("c:/mod_python/working_bee.txt").keys() def index(req): req.content_type = "text/html" return ''' <script> function complete(q) { var xhr, ws, e e = document.getelementbyid("suggestions") if (q.length == 0) { e.innerhtml = '' return } xhr = xmlhttprequest() xhr.open('get', 'suggest_from_file.py/complete?q=' + q, true) xhr.onreadystatechange = function() { if (xhr.readystate == 4) { ws = eval(xhr.responsetext) e.innerhtml = "" (i = 0; < ws.length; i++) e.innerhtml += ws[i] + "<br>" } } xhr.send(null) } </script> <input type="text" onkeyup="complete(this.value)"> <div id="suggestions"></div> ''' def complete(req, q): req.content_type = "text" return [w w in ws if w.startswith(q)]
txt file:
iv. miss taylor's working bee "so must. well, then, here goes!" mr. dyce swung shoulder , went, 2 steps @ time, in through crowd of girls, arrived there first when door opened. there in hall stood miss mary taylor, pretty pink. "i heard there bee here afternoon, , i've brought phronsie; that's welcome," announced. "see, i've got bag," announced phronsie perch, , holding forth. bag admired, , girls trooped in, going miss mary's pretty room take off things. , presently big library, music-room adjoining, filled gay young people, , bustle , chatter began @ once. "i should think you'd driven wild them wanting @ same minute." mr. dyce, having desire @ identical time, naturally felt bit impatient, miss mary went inspecting work, helping pick out stitch here , set new 1 there, admiring everyone's special bit of prettiness, , tossing smile , gay word in every chance moment between. "oh, no," said miss mary, little laugh, "they're of them sunday- school scholars, know."
looking @ code believe not sending correct thing apache. sending apache list , apache expecting string. suggest changing return json:
import json def complete(req, q): req.content_type = "text" return json.dumps([w w in ws if w.startswith(q)])
Comments
Post a Comment