Script like google suggest in python

Script like google suggest in python

glisglis Posts: 1Questions: 0Answers: 0
edited June 2012 in General
Hi,
I am writing a script that works like google suggest. Problem is that I am trying to get a suggestion for next 2 most likely words.
The example uses a txt file working_bee.txt. When writing a text "mis" I should get suggestions like "Miss Mary , Miss Taylor, ...". I only get "Miss, ...". I suspect the Ajax responseText method gives only a single word?
Any ideas what is wrong?

# Something that looks like Google suggest

def count_words(xFile):
frequency = {}
words=[]
for l in open(xFile, "rt"):
l = l.strip().lower()
for r in [',', '.', "'", '"', "!", "?", ":", ";"]:
l = l.replace(r, " ")
words += l.split()
for i 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 from file
ws = count_words("c:/mod_python/working_bee.txt").keys()

def index(req):
req.content_type = "text/html"
return '''

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 = ""
for (i = 0; i < ws.length; i++)
e.innerHTML += ws[i] + "
"

}
}
xhr.send(null)

}



'''

def complete(req, q):
req.content_type = "text"
return [w for w in ws if w.startswith(q)]


txt file:
IV. Miss Taylor's Working Bee

"So you must. Well, then, here goes!" Mr. Dyce swung her up to his shoulder and went, two steps at a time, in through the crowd of girls, so that he arrived there first when the door was opened. There in the hall stood Miss Mary Taylor, as pretty as a pink.

"I heard there was to be a bee here this afternoon, and I've brought Phronsie; that's my welcome," he announced.

"See, I've got a bag," announced Phronsie from her perch, and holding it forth.

So the bag was admired, and the girls trooped in, going up into Miss Mary's pretty room to take off their things. And presently the big library, with the music-room adjoining, was filled with the gay young people, and the bustle and chatter began at once.

"I should think you'd be driven wild by them all wanting you at the same minute." Mr. Dyce, having that desire at this identical time, naturally felt a bit impatient, as Miss Mary went about inspecting the work, helping to pick out a stitch here and to set a new one there, admiring everyone's special bit of prettiness, and tossing a smile and a gay word in every chance moment between.

"Oh, no," said Miss Mary, with a little laugh, "they're most of them my Sunday- school scholars, you know."

Replies

  • allanallan Posts: 63,075Questions: 1Answers: 10,384 Site admin
    I'm not sure you'll find much help here for your Python script. This is a forum for the Javascript library DataTables, not a general programming forum :-). I'd suggest you ask somewhere like StackOverflow.

    Allan
This discussion has been closed.