在Google App Engine中使用python进行文本预测的建议解决方案是什么?

在Google App Engine中使用python进行文本预测的建议解决方案是什么?,python,algorithm,google-app-engine,machine-learning,Python,Algorithm,Google App Engine,Machine Learning,我正在使用谷歌应用程序引擎和Python开发一个网站。我希望在网站上添加一个功能,用户可以在其中键入一个单词,系统将根据使用情况向用户提供与该单词最匹配的单词/句子作为建议。现在我已经实现了一个基于Peter Norvig方法拼写检查算法的算法。但我觉得从长远来看,这不是一个非常可扩展的解决方案。我正在寻找在谷歌应用程序引擎上实现这一功能的建议方法。预测Api是一条路吗?还是写我自己的算法是最好的方法?如果写我自己的算法是一种方式,那么有人能给我一些关于如何使解决方案健壮的建议吗 代码段: im

我正在使用谷歌应用程序引擎和Python开发一个网站。我希望在网站上添加一个功能,用户可以在其中键入一个单词,系统将根据使用情况向用户提供与该单词最匹配的单词/句子作为建议。现在我已经实现了一个基于Peter Norvig方法拼写检查算法的算法。但我觉得从长远来看,这不是一个非常可扩展的解决方案。我正在寻找在谷歌应用程序引擎上实现这一功能的建议方法。预测Api是一条路吗?还是写我自己的算法是最好的方法?如果写我自己的算法是一种方式,那么有人能给我一些关于如何使解决方案健壮的建议吗

代码段:

import re, collections
from bp_includes.models import User, SocialUser
from bp_includes.lib.basehandler import BaseHandler
from google.appengine.ext import ndb
import utils.ndb_json as ndb_json

class TextPredictionHandler(BaseHandler):
  alphabet_list = 'abcdefghijklmnopqrstuvwxyz' #list of alphabets

  #Creates corpus with frequency/probability distribution
  def trainForText(self,features):
    search_dict = collections.defaultdict(lambda: 1)
    for f in features:
      search_dict[f] += 1
    return search_dict

  #Heart of the code. Decides how many words can be formed by modifying a given word by one letter
  def edit_dist_one(self,word):
    splits      = [(word[:i],word[i:]) for i in range(len(word) + 1)]
    deletes     = [a + b[1:] for a,b in splits if b]
    transposes  = [a + b[1] + b[0] + b[2:] for a,b in splits if (len(b) > 1)]
    replaces = [a + c + b[1:] for a, b in splits for c in self.alphabet_list if b]
    inserts  = [a + c + b     for a, b in splits for c in self.alphabet_list]
    return set(deletes + transposes + replaces + inserts)

  #Checks for exact matches in Corpus for words 
  def existing_words(self,words,trainSet):
    return set(w for w in words if w in trainSet)

  #Checks for partial matches in Corpus for a word.
  def partial_words(self,word,trainSet):
    regex = re.compile(".*("+word+").*")    
    return set(str(m.group(0)) for l in trainSet for m in [regex.search(l)] if m)

  def found_words(self,word):
    word = word.lower()
    data = []
    q = models.SampleModel.query()    #This line will not work as I had to mask out the model I am using
    #Really bad way of making a Corpus. Needs to modified to be scalable. So many loops. Corpus can be stored in google cloud storage to reduce processing time.
    for upost in q.fetch(): 
        if upost.text!="":
          tempTextData = re.sub("[^\w]", " ",  upost.text).split()
          for t in range(len(tempTextData)):
            data.append(tempTextData[t].lower())
          # data.append(upost.text.lower())
        if upost.definition!="":
          tempData = re.sub("[^\w]", " ",  upost.definition).split()
          for t in range(len(tempData)):
            data.append(tempData[t].lower())
        if upost.TextPhrases:
         for e in upost.TextPhrases:
          for p in e.get().phrases: 
              data.append(p.lower())
        if upost.Tags:
          for h in upost.Tags:
            if h.get().text.replace("#","")!="" :
              data.append(h.get().text.replace("#","").lower())
    trainSet = self.trainForText(data)
    set_of_words = self.existing_words([word],trainSet).union(self.existing_words(self.edit_dist_one(word),trainSet))
    set_of_words = set_of_words.union(self.partial_words(word,trainSet))
    set_of_words = set_of_words.union([word])
    return set_of_words

  def get(self, search_text):
    outputData = self.found_words(search_text)
    data = {"texts":[]}
    for dat in outputData:
      pdata = {}
      pdata["text"] = dat;
      data["texts"].append(pdata)
    self.response.out.write(ndb_json.dumps(data))

使用预测API比创建自己的API更可靠、更具可扩展性。没有必要重新发明轮子。
如果你要自己编写代码,那可能是一个漫长而复杂的过程,路上会有很多坎坷,除非你对学习和编写这个系统非常感兴趣,我建议你使用现有的工具。
这是谷歌自己的一份报告。
这是我的建议。

使用预测API进行预测。

谢谢Meghdeep。关于如何继续使用预测Api或任何示例应用程序的链接,您有什么建议吗?将其添加到我的答案中。您认为这可以通过使用匹配评分的搜索Api来实现吗?我已经查看了您链接的示例,虽然它们对于基本介绍非常有用,但我想知道是否还有其他网站例如,我可以看看哪个?你可以尝试在GitHub上查找使用预测API的项目。有人能告诉我,带有匹配评分的Google搜索API是否可以帮助实现相同的功能吗?