本文整理汇总了Python中scinet3.model.Keyword类的典型用法代码示例。如果您正苦于以下问题:Python Keyword类的具体用法?Python Keyword怎么用?Python Keyword使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Keyword类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: receive_feedbacks
def receive_feedbacks(self, session, feedbacks):
"""
Receive feedbacks from user
The format of feedback is:
{
"docs": [[doc_id, feedback_value], ...],
"kws": [[keyword_id, feedback_value], ...],
"dockws": [[keyword_id, doc_id, feedback_value], ...]
}
"""
print "propagation started..."
for doc_fb in feedbacks.get("docs", []):
doc_id, fb = doc_fb
doc = Document.get(doc_id)
self.ppgt.fb_from_doc(doc, fb, session)
for kw_fb in feedbacks.get("kws", []):
kw_id, fb = kw_fb
kw = Keyword.get(kw_id)
self.ppgt.fb_from_kw(kw, fb, session)
for dockw_fb in feedbacks.get("dockws", []):
kw_id, doc_id, fb = dockw_fb
doc = Document.get(doc_id)
kw = Keyword.get(kw_id)
self.ppgt.fb_from_dockw(kw, doc, fb, session)
# propagation is done
# updates the feedback value
self.upd.update(session)
print "propagation finished"
示例2: setUp
def setUp(self):
#make the fmim
kw_filters = [self.my_kw_filter]
doc_filters = [self.kw_count_filter, self.has_database_filter]
self.r = LinRelRecommender(2, 2,
1.0, 0.1, 1.0, 0.1,
#the default configuration
kw_filters = None, doc_filters = [self.kw_count_filter, self.has_database_filter],
**fmim.__dict__)
self.session = get_session()
self.session.update_kw_feedback(Keyword.get("redis"), .7)
self.session.update_kw_feedback(Keyword.get("database"), .6)
self.session.update_doc_feedback(Document.get(1), .7)
self.session.update_doc_feedback(Document.get(2), .7)
self.session.update_doc_feedback(Document.get(8), .7)
filtered_kws = self.r._filter_objs(kw_filters, kws = Keyword.all_kws)
filtered_docs = self.r._filter_objs(doc_filters, docs = Document.all_docs)
kw2doc_submat, kw_ind_map, kw_ind_map_r = self.r._submatrix_and_indexing(filtered_kws, filtered_docs, fmim.kw2doc_m, fmim.kw_ind, fmim.doc_ind)
doc2kw_submat, doc_ind_map, doc_ind_map_r = self.r._submatrix_and_indexing(filtered_docs, filtered_kws, fmim.doc2kw_m, fmim.doc_ind, fmim.kw_ind)
self.fmim = FeatureMatrixAndIndexMapping(kw_ind_map, doc_ind_map, kw2doc_submat, doc2kw_submat, kw_ind_map_r, doc_ind_map_r)
示例3: recommend_keywords
def recommend_keywords(self, fmim,
session, top_n, mu, c,
sampler=None):
"""
fmim: FeatureMatrixAndIndexMapping, the fmim for the sub-matrix
session: Session,
top_n: how many kws are returned
mu,c: float, the parameters for LinRel algorithm
Return
KeywordList: a list of keyword ids as well as their scores
"""
kws = Keyword.get_many(fmim.kw_ind.keys())
fbs = dict([(kw.id, kw.fb(session)) for kw in kws])
id_with_scores, id_with_explt_scores, id_with_explr_scores = self.generic_rank(fmim.kw2doc_m, fbs,
fmim.kw_ind, fmim.kw_ind_r,
mu, c)
kws = []
for kw_id, score in id_with_scores.items()[:top_n]:
kw = Keyword.get(kw_id)
kw['score'] = score
kw['recommended'] = True
kws.append(kw)
return kws
示例4: test_get_many
def test_get_many(self):
doc_ids = [1,2]
kw_ids = ["a", "the"]
self.assertEqual(Document.get_many([1,2]),
Document.get_many(doc_ids))
self.assertEqual(Keyword.get_many(["a", "the"]),
Keyword.get_many(kw_ids))
示例5: test_add_kws
def test_add_kws(self):
iter1 = Keyword.get_many(["redis", "database", "mysql"])
iter2 = Keyword.get_many(["redis", "database", "python"])
self.session.add_kw_recom_list(iter1)
self.assertEqual([iter1], self.session.recom_kws)
self.session.add_kw_recom_list(iter2)
self.assertEqual([iter1, iter2], self.session.recom_kws)
示例6: test_one
def test_one(self):
docs = [Document.get_many([1,2]), Document.get_many([1,2]), Document.get_many([2,1])]
kws = [Keyword.get_many(["redis", "database"]), Keyword.get_many(["redis", "database"]), Keyword.get_many(["redis", "database"])]
scores = self.e.evaluate(docs, kws)
expected = ([1,1,1], [1,1,1])
self.assertArrayAlmostEqual(expected[0], scores[0])
self.assertArrayAlmostEqual(expected[1], scores[1])
示例7: test_kw_fb_filter
def test_kw_fb_filter(self):
kw = Keyword.get("redis")
kw.rec_fb_from_doc(Document.get(1), 1, self.session)
self.session.add_doc_recom_list(Document.get_many([1, 2, 6]))
self.session.update_kw_feedback(kw, kw.fb_weighted_sum(self.session))
actual = FilterRepository.filters["kw_fb"]([kw])
expected = Keyword.get_many(["redis"])
self.assertEqual(expected, actual)
示例8: test_associated_keywords_from_documents
def test_associated_keywords_from_documents(self):
kws = self.r.associated_keywords_from_docs(Document.get_many([1,2]))
self.assertEqual(set(Keyword.get_many(["a", "database", "redis", "the"])),
set(kws))
exclude_kws = [Keyword.get("redis")]
kws = self.r.associated_keywords_from_docs(Document.get_many([1,2]), exclude_kws)
self.assertEqual(set(Keyword.get_many(["a", "database", "the"])),
set(kws))
示例9: test_kw_fb_threshold_filter
def test_kw_fb_threshold_filter(self):
#change the feedback
self.session.update_kw_feedback(Keyword.get("python"), .2)
self.session.update_kw_feedback(Keyword.get("a"), .0999999)
actual = kw_fb_threshold_filter(0.1, self.session,
kws = Keyword.all_kws, with_fb = False)
expected = Keyword.get_many(["python"])
self.assertEqual(expected, actual)
示例10: test_two
def test_two(self):
docs = [Document.get_many([8,10]), Document.get_many([3,4]), Document.get_many([2,1])]
kws = [Keyword.get_many(["a", "the"]), Keyword.get_many(["python", "database"]), Keyword.get_many(["database", "redis"])]
scores = self.e.evaluate(docs, kws)
expected = ([0.34491169135422844, 0.1726882003112921, 1.0],
[0.4834283906452939, 0.759679156743632, 0.9999999999999999])
self.assertArrayAlmostEqual(expected[0], scores[0])
self.assertArrayAlmostEqual(expected[1], scores[1])
示例11: test_sample_documents_associated_with_keywords
def test_sample_documents_associated_with_keywords(self):
"""
normal case
"""
docs = self.r.sample_documents_associated_with_keywords(Keyword.get_many(["python", "redis"]), 2)
self.assertEqual(2, len(docs))
for doc in docs:
self.assertTrue((Keyword.get("python") in doc.keywords) or \
(Keyword.get("redis") in doc.keywords))
示例12: test_affected_kws
def test_affected_kws(self):
kws = [Keyword.get("python"), Keyword.get("redis")]
self.session.add_affected_kws(*kws)
self.assertEqual(kws, self.session.affected_kws)
kw3 = Keyword.get("a")
kws.append(kw3)
self.session.add_affected_kws(kw3)
self.assertEqual(set(kws),
set(self.session.affected_kws))
示例13: test_kw_hashable
def test_kw_hashable(self):
d = {}
kwlist1 = Keyword.get_many(["redis", "a", "the"])
kwlist2 = Keyword.get_many(["a", "the", "redis"])
kwlist3 = Keyword.get_many(["redis", "a", "python"])
d[kwlist1] = 1
d[kwlist2] = 2 #override
d[kwlist3] = 3
self.assertEqual({kwlist1:2, kwlist3: 3}, d)
示例14: test_model2modellist_similarity
def test_model2modellist_similarity(self):
#for keywords
kw = Keyword.get("redis")
kwlist = Keyword.get_many(["database", "mysql"])
self.assertAlmostEqual(0.3754029265429976, kw.similarity_to(kwlist))
#for documents
doc = Document.get(6)
doclist = Document.get_many([1, 2])
self.assertAlmostEqual(0.7382455893131392, doc.similarity_to(doclist))
示例15: test_modellist2modellist_similarity
def test_modellist2modellist_similarity(self):
#for keywords
kwlist1 = Keyword.get_many(["redis", "a"])
kwlist2 = Keyword.get_many(["database", "the"])
self.assertAlmostEqual(0.42205423035497763, kwlist1.similarity_to(kwlist2))
#for documents
doclist1 = Document.get_many([3,5])
doclist2 = Document.get_many([4,6])
self.assertAlmostEqual(0.6990609119502719, doclist1.similarity_to(doclist2))