Skip to content

Commit 034f15e

Browse files
authored
Merge pull request #16 from mweiden/codex/remove-unused-methods-from-invertedindex
Remove legacy word-search helpers
2 parents dc2ca42 + 4071cf5 commit 034f15e

2 files changed

Lines changed: 7 additions & 38 deletions

File tree

src/search/inverted_index.py

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ def __init__(
2424
model: SentenceTransformer | None = None,
2525
model_name: str = "sentence-transformers/paraphrase-MiniLM-L3-v2",
2626
):
27-
self._inverted_index: dict[str, list[tuple[str, int]]] = defaultdict(list)
2827
self._words_per_doc: dict[str, int] = defaultdict(int)
2928
self._doc_id_to_url: dict[str, str] = dict()
3029
self._doc_id_to_title: dict[str, str] = dict()
@@ -38,12 +37,10 @@ def __init__(
3837

3938
@property
4039
def total_docs(self):
41-
return len(self._inverted_index)
40+
return len(self._doc_id_to_url)
4241

4342
def insert(self, doc: Node) -> None:
44-
counts, total = self._word_count(doc.text)
45-
for word, count in counts.items():
46-
self._inverted_index[word].append((doc.id, count))
43+
_, total = self._word_count(doc.text)
4744
self._words_per_doc[doc.id] = total
4845
self._doc_id_to_url[doc.id] = doc.url
4946
self._doc_id_to_title[doc.id] = doc.title
@@ -65,21 +62,6 @@ def _word_count(self, text: str) -> dict[str, int]:
6562
total += 1
6663
return counts, total
6764

68-
def _search(self, word: str) -> list[tuple[str, int]]:
69-
result = self._inverted_index.get(word)
70-
return [] if result is None else result
71-
72-
def search(self, word: str) -> list[SearchResult]:
73-
return [
74-
SearchResult(
75-
id=kv[0],
76-
url=self._doc_id_to_url[kv[0]],
77-
title=self._doc_id_to_title[kv[0]],
78-
score=None,
79-
)
80-
for kv in self._search(word)
81-
]
82-
8365
def num_words_in_doc(self, doc_id: str) -> int:
8466
return self._words_per_doc[doc_id]
8567

src/search/tests/test_inverted_index.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import numpy as np
22
import pytest
3-
import requests
4-
from sentence_transformers import SentenceTransformer
53

64
from web_crawler.node import Node
7-
from search.inverted_index import InvertedIndex, SearchResult
5+
from search.inverted_index import InvertedIndex
86
from search.tokenizer import tokenize
97

108

@@ -62,21 +60,10 @@ def get_sentence_embedding_dimension(self) -> int:
6260
return inverted_index
6361

6462

65-
@pytest.fixture
66-
def search_result0(node0) -> SearchResult:
67-
return SearchResult(node0.id, node0.url, node0.title)
68-
69-
70-
@pytest.fixture
71-
def search_result1(node1) -> SearchResult:
72-
return SearchResult(node1.id, node1.url, node1.title)
73-
74-
75-
def test_insert(inverted_index, search_result0, search_result1):
76-
assert list(tokenize("ipsum")) == ["ipsum"]
77-
assert list(tokenize("iterators")) == ["iterators"]
78-
assert inverted_index.search("ipsum") == [search_result0]
79-
assert inverted_index.search("iterators") == [search_result0, search_result1]
63+
def test_top_k_keywords(inverted_index, node0, node1):
64+
assert inverted_index.top_k("ipsum")[0].id == node0.id
65+
ids = [r.id for r in inverted_index.top_k("iterators")[:2]]
66+
assert ids == [node1.id, node0.id]
8067

8168

8269
def test_num_words_in_doc(inverted_index, node0, node1):

0 commit comments

Comments
 (0)