2012-06-16 28 views
0

我正在寫一個腳本,像谷歌建議一樣工作。問題是我正試圖爲下兩個最可能的單詞提供建議。 該示例使用txt文件working_bee.txt。當寫一個文本「錯誤」,我應該得到像「瑪麗小姐,泰勒小姐,......」的建議。我只得到「小姐,......」。我懷疑Ajax responseText方法只給出一個單詞? 任何想法是什麼錯?腳本像谷歌建議在python

# 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." 
+0

我可能建議寫出你試圖逐一完成的步驟,並單獨與他們合作,而不是作爲一個整體項目。它可以讓你找出你犯了錯誤的地方。同時我會很樂意嘗試並幫助你。 – gabeio

+0

您還使用什麼服務器來運行索引和完整功能?沒有這個,很難理解可能會出現什麼問題。 – gabeio

+0

我使用的是Apache 2.2。我一步一步地檢查代碼,並且我懷疑服務器只返回onreadystatechange函數中的第一個單詞。 Python函數提供了正確的結果。 – user1460646

回答

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)]) 
+0

謝謝你,但我使用Python 2.5並試圖安裝simplejson,但有問題。它似乎安裝好,但當我嘗試導入時,我得到錯誤「沒有名爲simplejson模塊」。任何想法如何安裝? – user1460646

+0

你在做什麼系統,你如何安裝它? – gabeio

+0

有很多方法可以安裝python庫。恰恰是我最喜歡的是[pip](http://pypi.python.org/pypi/pip)。另一個只是大多數庫隨附的基本setup.py腳本。最後是下載源代碼並將源文件夾(本例中爲simplejson的項目名稱)解壓縮到您正在運行腳本的文件夾中。 – gabeio