- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用 Google App Engine 和 Python 开发一个网站。我希望在网站上添加一个功能,用户可以在其中输入一个单词,系统将根据该单词(基于用法)给出最接近的匹配单词/句子作为对用户的建议。现在我已经实现了一个基于 Peter Norvig 方法拼写检查算法的算法。但我觉得从长远来看,这不是一个非常可扩展的解决方案。我正在寻找在 Google App Engine 上实现此类功能的建议方法。预测 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))
最佳答案
与自己制作相比,使用 Prediction API 是最可靠和可扩展的。无需重新发明轮子。
如果您要编写自己的代码,这可能是一个漫长的复杂过程,并且道路上有很多颠簸,除非您对学习和编写该系统有浓厚的兴趣,否则我建议您使用现有工具。
这是一个 example来自谷歌自己。
这是 documentation for the Prediction API .
Hello World program与预测 API。
关于python - 在 Google App Engine 中使用 python 进行文本预测的建议解决方案是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31815857/
我是一个相对较新的程序员; CS 学士学位,大学毕业大约 2 年,主要使用 C# 中的 .NET。我对 SQL 交互/脚本编写相当流利,并且对 ASP.NET 做了一些工作(主要是维护现有站点)。 我
我计划开发一个简单的解决方案,使我能够即时执行非常基本的视频流分析。我以前从未做过类似的事情,因此这是一个非常笼统和开放的问题。主要重点是检查流是否正常运行,例如 - 卡住帧、黑屏以及音频是否存在。同
我正在考虑重组一个大型 Maven 项目...... 我们当前结构的基本概述: build [MVN plugins, third party dependency management]:5.1
我需要有关附加查询的建议。该查询执行了一个多小时,并根据解释计划进行了全表扫描。我对查询调优还很陌生,希望得到一些建议。 首先,为什么我要进行全表扫描,即使我使用的所有列都在其上创建了索引。 其次,有
我正在做一个项目,我需要在 4 个模型之间创建三个多对多关系。这是它的过程: 常见问题类别可以有许多常见问题子类别,反之亦然。 常见问题组可以有许多常见问题的子类别,反之亦然。 常见问题可以有许多常见
对于代码大小比语音质量更重要的 PIC 和/或 ARM 嵌入式系统,是否有任何易于使用的免费或廉价的语音合成库?现在似乎 1 meg 的封装被认为是“紧凑的”,但很多微 Controller 都比它小
我们正在使用 Solr 建议器功能进行 businessName 查找。当用户输入查询以及匹配的名称时,我们希望 solr 发送来自个人资料的其他属性,如 id、地址、城市、州、国家等字段。 我尝试使
我正在构建一个用户界面。我的计划将包括 4 个主要部分: 1) 顶部菜单 - TMainMenu。一个窗口的顶部 2) 主菜单 - TTreeView。一个窗口的左边。 TreeView的每一项=对应
我的公司需要一个任务管理系统来处理从“为X购买一台计算机”到“将一个人转移到另一个国家”这样简单的场景。简单的场景是由一个人处理的单个任务,而更大的任务可以分解为在工作流程中委派给多个人的多个子任务。
MarkLogic 服务器的林大小与实际内存的建议比率是多少?例如,我目前有一个 190GB 的数据库,并且该数据库随着时间的推移而不断增长。由于数据库会不断增长,我最终需要对该数据库进行集群。因此,
去年我收到了一个礼物,它是一个索尼 CMT700Ni 音频站,支持 wifi。它还具有类似于广播的功能,称为“PartyStreaming”。我目前正在挖掘内部,探索它,所以也许我可以结束拥有自己的“
有没有我可以阅读的研究论文/书籍可以告诉我针对手头的问题哪种特征选择算法最有效。 我试图简单地将 Twitter 消息识别为 pos/neg(首先)。我从基于频率的特征选择开始(从 NLTK 书开始)
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
我正在浏览 stackoverflow 以查找有关使用 jUnit 进行测试的常见建议,但仍然有几个问题。我知道,如果要测试的方法很复杂,最好的方法是将其分成小的单独部分并测试每个部分。但问题是 -
我有一个方法如下 public List> categorize(List customClass){ List> returnValue = new ArrayList<>();
我的问题是,当按照下面的程序合并时,在最佳实践场景中,“将分支折叠回主干”程序的最后一步是正确的方法吗? 我已经使用 svn 很多年了。在我的个人项目中,我总是毫不犹豫地在主干上愉快地进行修改,并且在
我读过 UINavigationController当您想从 n 个屏幕跳转到第一个屏幕时,这是最佳选择。这样做需要以下代码: NSMutableArray *array=[[NSMutableArr
我有一个文件输入类。它在构造函数中有一个字符串参数来加载提供的文件名。但是,如果文件不存在,它就会退出。如果文件不存在,我希望它输出一条消息 - 但不确定如何...... 这是类(class): pu
我希望创建一个“您访问过的国家/地区” map - 就像您可能在 Facebook、TravelAdvisor 和诸如此类的网站上看到的那样。 我尝试过不同的闪光灯套件,但它们并不像我希望的那样先进。
我需要一些关于如何处理我想用 Perl 编写的脚本的建议。基本上我有一个看起来像这样的文件: id: 1 Relationship: "" name: shelby pet: 1
我是一名优秀的程序员,十分优秀!