本文整理匯總了Python中tgrocery.Grocery.predict方法的典型用法代碼示例。如果您正苦於以下問題:Python Grocery.predict方法的具體用法?Python Grocery.predict怎麽用?Python Grocery.predict使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類tgrocery.Grocery
的用法示例。
在下文中一共展示了Grocery.predict方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: GroceryModel
# 需要導入模塊: from tgrocery import Grocery [as 別名]
# 或者: from tgrocery.Grocery import predict [as 別名]
class GroceryModel(object):
def __init__(self):
self.grocery = Grocery('TextClassify')
def train(self,train_file):
f = open(train_file,'r')
line = f.readline().decode('utf8')
dataset = []
while line:
tmp = line.split('\t')
dataset.append((tmp[0],''.join(tmp[1:])))
line = f.readline().decode('utf8')
f.close()
self.grocery.train(dataset)
self.grocery.save()
def load_model(self):
self.grocery.load()
def test(self,test_src):
self.load_model()
f = open(test_src,'r')
line = f.readline().decode('utf8')
dataset = []
while line:
tmp = line.split('\t')
dataset.append((tmp[0],''.join(tmp[1:])))
line = f.readline().decode('utf8')
f.close()
result = self.grocery.test(dataset)
print result
def predict(self,text):
print self.grocery.predict(text)
示例2: tGrocery
# 需要導入模塊: from tgrocery import Grocery [as 別名]
# 或者: from tgrocery.Grocery import predict [as 別名]
def tGrocery():
outFile = open('testResult.tmp', 'w')
[trainingSet, benchmark] = pickle.load(open('SampleSeg.pk'))
testingSet = []
correctLabel = []
for i in xrange(len(benchmark)):
print '%d out of %d' % (i, len(benchmark))
testingSet.append(benchmark[i][1])
correctLabel.append(benchmark[i][0])
grocery = Grocery('test')
grocery.train(trainingSet)
grocery.save()
# load
new_grocery = Grocery('test')
new_grocery.load()
Prediction = []
for i in xrange(len(testingSet)):
print '%d out of %d' % (i, len(testingSet))
prediction = new_grocery.predict(testingSet[i])
Prediction.append(prediction)
temp = correctLabel[i] + '<-->' + prediction + ' /x01' + testingSet[i] + '\n'
outFile.write(temp)
correct = 0
for i in xrange(len(Prediction)):
print Prediction[i], correctLabel[i],
if Prediction[i] == correctLabel[i]:
correct += 1
print 'Correct'
else:
print 'False'
print 'Correct Count:', correct
print 'Accuracy: %f' % (1.0 * correct / len(Prediction))
示例3: __init__
# 需要導入模塊: from tgrocery import Grocery [as 別名]
# 或者: from tgrocery.Grocery import predict [as 別名]
def __init__(self, keyword):
print '進行新聞分類'
(db, cursor) = connectdb()
cursor.execute("update task set status=1 where keyword=%s", [keyword])
cursor.execute("select id, title from news where keyword=%s",[keyword])
news = cursor.fetchall()
new_grocery = Grocery('static/paris')
new_grocery.load()
for item in news:
tag = new_grocery.predict(item['title'])
if tag == '新聞背景':
tag = 1
elif tag == '事實陳述':
tag = 2
elif tag == '事件演化':
tag = 3
elif tag == '各方態度':
tag = 4
elif tag == '直接關聯':
tag = 6
elif tag == '暫無關聯':
tag = 7
cursor.execute("update news set tag=%s where id=%s", [tag, item['id']])
closedb(db, cursor)
return
示例4: GET
# 需要導入模塊: from tgrocery import Grocery [as 別名]
# 或者: from tgrocery.Grocery import predict [as 別名]
def GET(self,name):
#i = web.input(name=None)
#url = "http://"+name
#html = urllib2.urlopen(url).read()
#soup = BeautifulSoup(html)
#title = soup.html.head.title.contents.pop().encode('utf-8')
title = name.encode('utf-8')
new_grocery = Grocery('sample')
new_grocery.load()
return new_grocery.predict(title)
示例5: test_main
# 需要導入模塊: from tgrocery import Grocery [as 別名]
# 或者: from tgrocery.Grocery import predict [as 別名]
def test_main(self):
grocery = Grocery(self.grocery_name)
grocery.train(self.train_src)
grocery.save()
new_grocery = Grocery('test')
new_grocery.load()
assert grocery.get_load_status()
assert grocery.predict('考生必讀:新托福寫作考試評分標準') == 'education'
# cleanup
if self.grocery_name and os.path.exists(self.grocery_name):
shutil.rmtree(self.grocery_name)
示例6: MyGrocery
# 需要導入模塊: from tgrocery import Grocery [as 別名]
# 或者: from tgrocery.Grocery import predict [as 別名]
class MyGrocery(object):
def __init__(self, name):
super(MyGrocery, self).__init__()
self.grocery = Grocery(name)
self.loaded = False
self.correct = 1.0
def train(self, src):
lines = []
for line in csv.reader(open(src)):
label, s = line[0],line[1]
text = s.decode('utf8')
lines.append((label, text))
self.grocery.train(lines)
def save_model(self):
self.grocery.save()
def train_and_save(self, src):
self.train(src)
self.save_model()
def load_model(self):
if not self.loaded:
self.grocery.load()
self.loaded = True
def predict(self, text):
self.load_model()
return self.grocery.predict(text)
def test(self, src):
self.load_model()
total, wrong_num = 0.0, 0.0
for line in csv.reader(open(src)):
total += 1
if line[0] != self.predict(line[1]):
wrong_num += 1
print "load test file from " + src
correct = (total - wrong_num ) / total
self.correct = correct
print "total: %d , wrong_num: %d, success percentage: %f" %(total, wrong_num, correct)
result = dict(type="test", total=total, wrong_num=wrong_num, correct=correct)
return json.dumps(result)
示例7: predict_corpus
# 需要導入模塊: from tgrocery import Grocery [as 別名]
# 或者: from tgrocery.Grocery import predict [as 別名]
def predict_corpus(input_file,output_csv):
import csv
csvfile = file(output_csv, 'wb')
writer = csv.writer(csvfile)
corpus = []
f = xlrd.open_workbook(input_file)
table = f.sheet_by_name('Sheet1')
nrows = table.nrows # 讀取行數
for rownum in range(0, nrows):
row = table.row_values(rownum)
row[2].strip()
corpus.append(row[2])
corpus_grocery = Grocery(project_name)
corpus_grocery.load()
output = []
for sentence in corpus:
predict = corpus_grocery.predict(sentence)
output.append((sentence,predict))
writer.writerows(output)
print('Done!')
csvfile.close()
開發者ID:frederic89,項目名稱:Event_Classification_and_Domain_Recognition,代碼行數:23,代碼來源:domain_predict_py2.py
示例8: jdParser
# 需要導入模塊: from tgrocery import Grocery [as 別名]
# 或者: from tgrocery.Grocery import predict [as 別名]
class jdParser(object):
def __init__(self):
self.clf = Grocery("./jdclf")
self.clf.load()
self.LINE_SPLIT = re.compile(u"[;。;\n]")
def get_demand_and_duty(self,jdstr):
linelist = [ line.strip() for line in self.LINE_SPLIT.split(jdstr) if len(line.strip()>4) ]
result = {}
demand = []
duty = []
for line in linelist:
pred = str(self.clf.predict(line))
if pred =="demand":
demand.append(line)
elif pred == "duty":
duty.append(line)
result['demand'] = '\n'.join(demand)
result['duty'] = '\n'.join(duty)
示例9: reload
# 需要導入模塊: from tgrocery import Grocery [as 別名]
# 或者: from tgrocery.Grocery import predict [as 別名]
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import MySQLdb
from tgrocery import Grocery
import sys
reload(sys)
sys.setdefaultencoding('utf8')
grocery = Grocery('sample')
dict_list = list()
conn = MySQLdb.connect(host = 'localhost', db = 'newsdata', user = 'root', passwd = 'root', charset = 'utf8', use_unicode = False)
cur = conn.cursor()
cur.execute('select com_new_type_id, com_new_name from tbl_new where com_new_type_id is not null')
for row in cur.fetchall():
dict_list.append(row)
grocery.train(dict_list)
grocery.save()
news_grocery = Grocery('sample')
news_grocery.load()
while True:
result = news_grocery.predict(raw_input('please input title:' ))
print result
示例10: Grocery
# 需要導入模塊: from tgrocery import Grocery [as 別名]
# 或者: from tgrocery.Grocery import predict [as 別名]
from tgrocery import Grocery
data_dir = "../data/"
src_fn = data_dir + 'train_set_100.txt'
grocery = Grocery('backout_reason')
grocery.train(src_fn)
tp_cnt = {}
f = open(data_dir + 'type.txt')
for line in f:
tps = line.split()
tp_cnt[tps[1]] = 0
f.close()
f = open(data_dir + 'bcmtmoz.merge')
for line in f:
tp = grocery.predict(line)
tp_cnt[tp] += 1
print tp_cnt
示例11:
# 需要導入模塊: from tgrocery import Grocery [as 別名]
# 或者: from tgrocery.Grocery import predict [as 別名]
concat = ','.join(no) + '$%^' + ','.join(qz)
print concat
clientSender.publish('similarResult', reqParamList[0] + '[email protected]#' + concat)
elif item['channel'] == 'abstract':
# 文本抽取
text = reqParamList[1]
tr4s.train(text=text, speech_tag_filter=True, lower=True, source = 'all_filters')
# 使用詞性過濾,文本小寫,使用words_all_filters生成句子之間的相似性
# abstractResult = '\n'.join(tr4s.get_key_sentences(num=1+len(text)/350))
abstractResult = tr4s.get_key_sentences(num=1+len(text)/250)
re = ''
'$%^'.join(abstractResult)
clientSender.publish('abstractResult', reqParamList[0] + '[email protected]#' + '$%^'.join(abstractResult))
elif item['channel'] == 'classification':
doc = reqParamList[1]
#data, data_vec = ldaModel.file_to_vec(doc)
#out_put, out_put_class = ldaModel.pre(data_vec)
t_pre_result = grocery.predict(delete_stop_words(doc))
out_put_class = t_pre_result.predicted_y
clientSender.publish('classificationResult', reqParamList[0] + '[email protected]#' + out_put_class)
示例12: print
# 需要導入模塊: from tgrocery import Grocery [as 別名]
# 或者: from tgrocery.Grocery import predict [as 別名]
resultlist=[]
i=0
for line in validate_reader:
content=pp.getcontent(validate_reader,i)
i=i+1
if(i%5000==0):
print ("%d "%(i))+'#'*30
#if(i>10):
#break
if(content==''):
print line
else:
str=content.split('\t')
len=str[0].__len__()
result=grocery.predict(content[len+3:])
if(result==str[1]):
if(str[1]==u'0'):
TN=TN+1
else:
TP=TP+1
else:
if(str[1]==u'0'):
FP=FP+1
fileOutput.write('FP: '+line+' \n')
else:
FN=FN+1
fileOutput.write('FN: '+line+' \n')
precision=TP/(TP+FP)
recall=TP/(TP+FN)
示例13: open
# 需要導入模塊: from tgrocery import Grocery [as 別名]
# 或者: from tgrocery.Grocery import predict [as 別名]
ftest = open(path2, 'w')
for line in open(path):
if random.random() < theta:
ftest.write(line)
else:
ftrain.write(line)
ftrain.close()
ftest.close()
def train(path,name):
grocery = Grocery(name)
grocery.train(path)
grocery.save()
if __name__ == "__main__":
data2tt(sys.argv[3], sys.argv[1], sys.argv[2], 0.02)
train(sys.argv[1], "music")
new_grocey = Grocery("music")
new_grocey.load()
n = 0
for line in open(sys.argv[2],"r"):
ls = line.strip().split("\t")
predict = new_grocey.predict(ls[1])
test = ls[0]
result = 0
if test == str(predict):
result = 1
n += result
print predict,test,result
print n
示例14:
# 需要導入模塊: from tgrocery import Grocery [as 別名]
# 或者: from tgrocery.Grocery import predict [as 別名]
q=0
for c in mycontent:
if c:
k=mysign[q]
p=[k,c]
train_listc.append(p)
q=q+1
for t in mytitle:
m=mysign[i]
n=[m,t]
train_list.append(n)
i=i+1
grocery.train(train_listc)
grocery.train(train_list)
grocery.save()
new_grocery=Grocery('trydb')
new_grocery.load()
pc=message.getContent1()
pt=message.getTitle1()
g=1
for newscontent in pc:
if newscontent:
num=new_grocery.predict(newscontent+pt[g-1])
message.saveContent(g,num)
else:
num=new_grocery.predict(pt[g-1])
message.saveContent(g,num)
g=g+1
示例15: delete_stop_words
# 需要導入模塊: from tgrocery import Grocery [as 別名]
# 或者: from tgrocery.Grocery import predict [as 別名]
##########################################
# init
model_choose = "svm" # svm, lda, rnn
grocery_name = "./SVM_models/svm_for_news"
corpus_path = "./Corpus/NewsClassCorpus/"
file_path = "./"
file_name = "post.txt"
t_text = delete_stop_words(codecs.open(file_path + file_name, encoding="UTF-8").read())
###########################################
# 調用 SVM 模型分類
if model_choose == "svm":
tic = time.time()
grocery = Grocery(grocery_name)
grocery.load()
t_pre_result = grocery.predict(delete_stop_words(t_text))
toc = time.time()
t_label = t_pre_result.predicted_y
print("Sentiment: ", t_label)
print("How much: ", t_pre_result.dec_values[t_label])
print("Elapsed time of predict is: %s s" % (toc - tic))
elif model_choose == "lda":
pass
elif model_choose == "rnn":
pass
else:
print("")