本文整理汇总了Python中Classifier.Classifier.createVectSpacePost方法的典型用法代码示例。如果您正苦于以下问题:Python Classifier.createVectSpacePost方法的具体用法?Python Classifier.createVectSpacePost怎么用?Python Classifier.createVectSpacePost使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Classifier.Classifier
的用法示例。
在下文中一共展示了Classifier.createVectSpacePost方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from Classifier import Classifier [as 别名]
# 或者: from Classifier.Classifier import createVectSpacePost [as 别名]
def main(**kwargs):
iduser = sys.argv[1]
#Find user by id
user = findUserById(iduser)
#Find tweets and posts (here or on java?)
posts = findPosts(user)
#posts = [(u'1', u'Eu amo computadores! Eu gostaria de comprar um asus notebook.', u'Post', u'Twitter'),(u'2', u'Minha geladeira quebrou!...', u'Post', u'Facebook'),(u'3', u'Eu amo assistir TV todo sabado anoite!', u'Post', u'Twitter'),(u'4', u'Eu amo assitir netflix na minha Smart TV', u'Post', u'Twitter')]
conf = SparkConf().setAppName(APP_NAME).setMaster("local").set("spark.executor.memory", "1g")
sc = SparkContext(conf=conf)
for post in posts:
print post
print 'Generating posts RDD'
postsRDD = sc.parallelize(posts)
tokens, category, categoryAndSubcategory = getTokensAndCategories()
stpwrds = stopwords.words('portuguese')
print 'Generating product RDD'
productRDD = sc.parallelize(findProductsByCategory([]))
print 'Union posts with product'
productAndPostRDD = productRDD.union(postsRDD)
print 'Generating corpusRDD'
corpusRDD = (productAndPostRDD.map(lambda s: (s[0], word_tokenize(s[1].lower()), s[2], s[3]))
.map(lambda s: (s[0], [PorterStemmer().stem(x) for x in s[1] if x not in stpwrds], s[2], s[3]))
.map(lambda s: (s[0], [x for x in s[1] if x in tokens], s[2], s[3]))
.filter(lambda x: len(x[1]) >= 20 or x[2] == u'Post')
.cache())
#corpusRDD = productAndPostRDD.map(lambda s: (s[0], word_tokenize(s[1].lower()), s[2], s[3])).map(lambda s: (s[0], [PorterStemmer().stem(x) for x in s[1] if x not in stpwrds], s[2], s[3] )).map(lambda s: (s[0], [x[0] for x in pos_tag(s[1]) if x[1] == 'NN' or x[1] == 'NNP'], s[2], s[3])).cache()
print 'Generating idfsRDD'
idfsRDD = idfs(corpusRDD)
idfsRDDBroadcast = sc.broadcast(idfsRDD.collectAsMap())
print 'Generating tdidfRDD'
tfidfRDD = corpusRDD.map(lambda x: (x[0], tfidf(x[1], idfsRDDBroadcast.value), x[2], x[3])).cache()
tfidfPostsRDD = tfidfRDD.filter(lambda x: x[2]=='Post').cache()
tfidfPostsBroadcast = sc.broadcast(tfidfPostsRDD.map(lambda x: (x[0], x[1])).collectAsMap())
corpusPostsNormsRDD = tfidfPostsRDD.map(lambda x: (x[0], norm(x[1]))).cache()
corpusPostsNormsBroadcast = sc.broadcast(corpusPostsNormsRDD.collectAsMap())
print 'Generating Classifier'
#classifier = Classifier(sc, 'NaiveBayes')
#modelNaiveBayesCategory = classifier.getModel('/dados/models/naivebayes/category_new')
#postsSpaceVectorRDD = classifier.createVectSpacePost(tfidfPostsRDD, tokens)
#predictions = postsSpaceVectorRDD.map(lambda p: (modelNaiveBayesCategory.predict(p[1]), p[0])).groupByKey().mapValues(list).collect()
classifier = Classifier(sc, 'NaiveBayes')
modelNaiveBayesSubcategory = classifier.getModel('/dados/models/naivebayes/subcategory_new')
postsSpaceVectorRDD = classifier.createVectSpacePost(tfidfPostsRDD, tokens)
predictions = postsSpaceVectorRDD.map(lambda p: (modelNaiveBayesSubcategory.predict(p[1]), p[0])).groupByKey().mapValues(list).collect()
#classifier = Classifier(sc, 'DecisionTree')
#modelDecisionTree = classifier.getModel('/dados/models/dt/category_new')
#postsSpaceVectorRDD = classifier.createVectSpacePost(tfidfPostsRDD, tokens)
#predictions = modelDecisionTree.predict(postsSpaceVectorRDD.map(lambda x: x)).collect()
for prediction in predictions:
print '=================================> PREDICTION {}'.format(prediction)
category_to_use = categoryAndSubcategory[int(prediction[0])][0]
print '=================================> CATEGORY TO USE {}'.format(category_to_use)
tfidfProductsCategoryRDD = tfidfRDD.filter(lambda x: x[2]==category_to_use).cache()
tfidfProductsCategoryBroadcast = sc.broadcast(tfidfProductsCategoryRDD.map(lambda x: (x[0], x[1])).collectAsMap())
corpusInvPairsProductsRDD = tfidfProductsCategoryRDD.flatMap(lambda r: ([(x, r[0]) for x in r[1]])).cache()
corpusInvPairsPostsRDD = tfidfPostsRDD.flatMap(lambda r: ([(x, r[0]) for x in r[1]])).filter(lambda x: x[1] in prediction[1]).cache()
commonTokens = (corpusInvPairsProductsRDD.join(corpusInvPairsPostsRDD)
.map(lambda x: (x[1], x[0]))
.groupByKey()
.cache())
corpusProductsNormsRDD = tfidfProductsCategoryRDD.map(lambda x: (x[0], norm(x[1]))).cache()
corpusProductsNormsBroadcast = sc.broadcast(corpusProductsNormsRDD.collectAsMap())
print '### PREDICTION Similarities RDD'
similaritiesRDD = (commonTokens
.map(lambda x: cosineSimilarity(x, tfidfProductsCategoryBroadcast.value, tfidfPostsBroadcast.value, corpusProductsNormsBroadcast.value, corpusPostsNormsBroadcast.value))
.cache())
suggestions = (similaritiesRDD
.map(lambda x: (x[0][1], (x[0][0], x[1])))
.filter(lambda x: x[1][1]>threshold)
.groupByKey()
.mapValues(list)
.join(postsRDD)
.join(postsRDD.map(lambda x: (x[0], x[3])))
.collect())
if len(suggestions) > 0:
insertSuggestions(suggestions, iduser, productRDD)
user['statusRecomendacao'] = u'F'
#.........这里部分代码省略.........