2017-06-29 67 views
1

如何使用whoosh庫進行拼寫檢查。我在文檔中添加了一些代碼。但它不糾正單詞。請找到我的代碼。如何使用whoosh python庫進行拼寫檢查

def main(): 
    print " Hi" 

    schema = Schema(title=TEXT(stored=True), path=ID(stored=True), content=TEXT) 
    ix = create_in("/home/praveen/Downloads/who", schema) 
    writer = ix.writer() 
    writer.add_document(title=u"First document", path=u"/a", content=u"This is the first document we've added!") 
    writer.add_document(title=u"Second document", path=u"/b",content=u"The second one is even more interesting!") 
    writer.commit() 

    qstring = "frm indea wroking for campany" 
    qp = qparser.QueryParser("content", ix.schema) 
    q = qp.parse(qstring) 
    # Try correcting the query 
    with ix.searcher() as s: 
      corrected = s.correct_query(q,qstring) 
      print(corrected) 
      print(corrected.query) 
      if corrected.query != q: 
       print("Did you mean:", corrected.string) 

if __name__ == "__main__": 
    main(); 

和我的輸出是:

Hi 
Correction(And([Term('content', u'frm'), Term('content', u'indea'), Term('content', u'wroking'), Term('content', u'campany')]), 'frm indea wroking for campany') 
(content:frm AND content:indea AND content:wroking AND content:campany) 

我沒有收到 「你的意思是:」 與修正的字符串。

請讓我知道提前

感謝,

+0

然後顯然'corrected.query == q'。 –

+0

我的程序需要做什麼修改。我從文檔中獲取代碼。輸入是應該接受拼寫檢查的qstring –

回答

3

腳本只能從索引的話得到修正。您要糾正的短語在索引中沒有類似的詞。

你的那句:

「FRM indea wroking爲CAMPANY」

索引短語:

「!這是我們添加了頭文件」

「第二個更有趣!

如果你給喜歡的一句話:"secend one is ewen" 您將獲得:

('Did you mean:', u'second one is even') 

它完全被糾正。