wikipedia fetch Ranking combining BM25 lexical relevance and embedding cosine similarity through reciprocal rank fusion
This commit is contained in:
@@ -12,9 +12,9 @@ from unittest.mock import Mock, call, patch
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parent.parent.parent))
|
||||
|
||||
from knowledge.expert import (KnowledgeConfig, KnowledgeExpert, _goal_skim_notice,
|
||||
_probe_seeds, _rank_article_titles)
|
||||
_probe_seeds)
|
||||
from knowledge.skim import SkimSheet
|
||||
from knowledge.wiki import WikiPage, WikiTable
|
||||
from knowledge.wiki import WikiPage, WikiSearchResult, WikiTable
|
||||
|
||||
|
||||
QUESTION = "What is the flow of Po?"
|
||||
@@ -36,11 +36,14 @@ class AnswerQuestionTests(unittest.TestCase):
|
||||
e = self.expert
|
||||
e.cfg = KnowledgeConfig()
|
||||
e.client = Mock()
|
||||
e.client.search.side_effect = [["Po"], ["Italy"]]
|
||||
e.client.search.side_effect = [
|
||||
[WikiSearchResult("Po", "Rivers flow through Italy. Po flows.")],
|
||||
[WikiSearchResult("Italy")]]
|
||||
e.client.fetch_rich.side_effect = lambda title: page(title)
|
||||
e._checked, e._merged = 5, 3
|
||||
e._prover_answer = Mock(return_value=None)
|
||||
e._probe = Mock(return_value=["row"])
|
||||
e._qvec = Mock(return_value=None)
|
||||
e._answer_from_rows = Mock(return_value=UNKNOWN)
|
||||
e._audit_ambiguity = Mock(side_effect=lambda _q, ans, _rows, _charge: ans)
|
||||
e._set_gate_hit = Mock(return_value=None)
|
||||
@@ -90,15 +93,17 @@ class AnswerQuestionTests(unittest.TestCase):
|
||||
e._answer_from_rows.assert_not_called()
|
||||
e.client.search.assert_not_called()
|
||||
|
||||
def test_search_deduplicates_and_preserves_query_winners_then_relevance(self):
|
||||
def test_search_deduplicates_and_globally_ranks_title_and_snippet(self):
|
||||
e = self.expert
|
||||
e.cfg.max_articles = 5
|
||||
e.client.search.side_effect = [
|
||||
["First", "Other", "Po flow", "Po river"],
|
||||
["Winner", "First", "Other"]]
|
||||
[WikiSearchResult("First"), WikiSearchResult("Other"),
|
||||
WikiSearchResult("Po flow"), WikiSearchResult("Po river")],
|
||||
[WikiSearchResult("Winner", "Po flow"), WikiSearchResult("First"),
|
||||
WikiSearchResult("Other")]]
|
||||
result = e.answer_question(QUESTION)
|
||||
self.assertEqual(result["stats"]["pages"],
|
||||
["First", "Winner", "Po flow", "Other", "Po river"])
|
||||
["Po flow", "Winner", "Po river", "First", "Other"])
|
||||
self.assertEqual(e.client.search.call_args_list, [
|
||||
call("query one", limit=4), call("query two", limit=4)])
|
||||
self.assertEqual(e._extract_entities, ["Po"])
|
||||
@@ -250,17 +255,6 @@ class PipelineHelperTests(unittest.TestCase):
|
||||
self.assertEqual(kws, ["flow"])
|
||||
self.assertEqual(_probe_seeds(kws, ""), kws)
|
||||
|
||||
def test_title_ranking_handles_empty_queries_and_duplicate_winners(self):
|
||||
found = [[], ["Winner", "Other", "Flow rate"], ["Winner", "Flow volume"]]
|
||||
self.assertEqual(_rank_article_titles(found, ["flow"]),
|
||||
["Winner", "Flow rate", "Flow volume", "Other"])
|
||||
self.assertEqual(_rank_article_titles([], ["flow"]), [])
|
||||
|
||||
def test_title_ranking_preserves_winners_even_without_relevant_terms(self):
|
||||
self.assertEqual(_rank_article_titles(
|
||||
[["Loire", "Other"], ["France", "Other"]], ["France"]),
|
||||
["Loire", "France", "Other"])
|
||||
|
||||
def test_skim_notices_include_goal_matches_and_filter_unrelated_surfaces(self):
|
||||
sheet = SkimSheet(title="Po", url="u", link_candidates=["River flow"])
|
||||
self.assertEqual(_goal_skim_notice(sheet, ["flow"]), sheet.notice())
|
||||
|
||||
Reference in New Issue
Block a user