0

私はグーグルの提案のように機能するスクリプトを書いています。問題は、私が次の2つの最も可能性の高い単語の提案を得ようとしていることです。この例では、txtファイルworking_bee.txtを使用しています。「mis」というテキストを書くときは、「Miss Mary、Miss Taylor、...」のような提案を受け取る必要があります。「ミス、...」しか出てこない。Ajax responseTextメソッドは1つの単語しか与えないのではないかと思いますか?何が悪いのか考えてみませんか?

# 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 '''
<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 = ""
            for (i = 0; i < 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 for w in ws if w.startswith(q)]

txtファイル:

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." 
4

1 に答える 1

0

あなたのコードを見ると、あなたは正しいものをApacheに送っていないと思います。あなたはapacheにリストを送信していて、apacheは文字列を期待しています。jsonへの復帰を変更することをお勧めします:

import json
def complete(req, q):
    req.content_type = "text"
    return json.dumps([w for w in ws if w.startswith(q)])
于 2012-06-16T20:52:31.277 に答える