0

我正在编写一个像谷歌建议一样工作的脚本。问题是我正在尝试为接下来的 2 个最有可能的单词提供建议。该示例使用 txt 文件 working_bee.txt。在写文本“mis”时,我应该得到诸如“Miss Mary, Miss Taylor, ...”之类的建议。我只得到“小姐,......”。我怀疑 Ajax responseText 方法只给出一个单词?有什么想法有什么问题吗?

# 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 回答