当前位置: 首页>>代码示例>>Python>>正文


Python Grocery.predict方法代码示例

本文整理汇总了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)
开发者ID:TimePi,项目名称:forwork,代码行数:36,代码来源:SuperModel.py

示例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))
开发者ID:haomingchan0811,项目名称:iPIN,代码行数:34,代码来源:prepData.py

示例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
开发者ID:Honlan,项目名称:CleverTL,代码行数:28,代码来源:classify.py

示例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)
开发者ID:MMPlatform,项目名称:textclass,代码行数:12,代码来源:seg3.py

示例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)
开发者ID:SHENbeyond,项目名称:TextGrocery,代码行数:13,代码来源:runtests.py

示例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)
开发者ID:henryluki,项目名称:word_filter,代码行数:47,代码来源:classify.py

示例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)
开发者ID:jkmiao,项目名称:ipin2015,代码行数:26,代码来源:api_peifeng.py

示例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

开发者ID:neverkevin,项目名称:python-python,代码行数:29,代码来源:test_Grocery.py

示例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
开发者ID:betterenvi,项目名称:backout-research-backup,代码行数:22,代码来源:2predict.py

示例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)



开发者ID:wac81,项目名称:LSI-for-ChineseDocument,代码行数:29,代码来源:service.py

示例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)
开发者ID:jizhihang,项目名称:adavanced-aritificial-intelligence,代码行数:32,代码来源:validate.py

示例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
开发者ID:Gbacillus,项目名称:crawler_fg,代码行数:32,代码来源:classify.py

示例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
开发者ID:LiaoPan,项目名称:MyGit,代码行数:32,代码来源:trydb.py

示例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("")
开发者ID:wac81,项目名称:LSI-for-ChineseDocument,代码行数:31,代码来源:News_SvmClass_predict.py


注:本文中的tgrocery.Grocery.predict方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。