Skip to content

armgit / mwmbl

Public Imported from GitHub
0

An open source, non-profit web search engine

Ensure Document title and extract are never None

Daoud Clarkef01ae42f6b7f
diff --git a/mwmbl/tinysearchengine/indexer.py b/mwmbl/tinysearchengine/indexer.py
index d12cfd6..506b9b9 100644
--- a/mwmbl/tinysearchengine/indexer.py
+++ b/mwmbl/tinysearchengine/indexer.py
@@ -51,6 +51,14 @@ class Document:
term: Optional[str] = None
state: Optional[int] = None
+ def __init__(self, title, url, extract, score, term=None, state=None):
+ self.title = title if title is not None else ''
+ self.url = url
+ self.extract = extract if extract is not None else ''
+ self.score = score
+ self.term = term
+ self.state = state
+
@dataclass
class TokenizedDocument(Document):
diff --git a/mwmbl/tinysearchengine/rank.py b/mwmbl/tinysearchengine/rank.py
index cd6ba3a..22cc5d9 100644
--- a/mwmbl/tinysearchengine/rank.py
+++ b/mwmbl/tinysearchengine/rank.py
@@ -50,6 +50,8 @@ def score_match(last_match_char, match_length, total_possible_match_length):
def get_features(terms, title, url, extract, score, is_complete):
assert url is not None
+ assert title is not None
+ assert extract is not None
features = {}
parsed_url = urlparse(url)
domain = parsed_url.netloc
@@ -59,10 +61,8 @@ def get_features(terms, title, url, extract, score, is_complete):
(domain, 'domain', True),
(domain, 'domain_tokenized', False),
(path, 'path', True)]:
- part_not_none = part if part is not None else 'socks'
- print("Part", part, name)
last_match_char, match_length, total_possible_match_length, match_terms = \
- get_match_features(terms, part_not_none, is_complete, is_url)
+ get_match_features(terms, part, is_complete, is_url)
features[f'last_match_char_{name}'] = last_match_char
features[f'match_length_{name}'] = match_length
features[f'total_possible_match_length_{name}'] = total_possible_match_length
diff --git a/test/test_indexer.py b/test/test_indexer.py
index 06de1df..fe67876 100644
--- a/test/test_indexer.py
+++ b/test/test_indexer.py
@@ -116,4 +116,10 @@ def test_get_page_data_many_docs_subset_fit():
serialized_data = _get_page_data(page_size, items)
padded_trimmed_data = _pad_to_page_size(trimmed_data, page_size)
- assert serialized_data == padded_trimmed_data
\ No newline at end of file
+ assert serialized_data == padded_trimmed_data
+
+
+def test_constructing_document_removes_none():
+ document = Document(title=None,url='url',extract=None,score=1.0)
+ assert document.title == ''
+ assert document.extract == ''
diff --git a/test/test_rank.py b/test/test_rank.py
index d26ca07..cdb9266 100644
--- a/test/test_rank.py
+++ b/test/test_rank.py
@@ -13,15 +13,3 @@ def test_order_result():
ordered_results = order_results(["bananas"], documents, True)
assert ordered_results[0].title == 'Bananas and apples'
-
-
-def test_order_results_where_document_has_properties_that_are_none():
- doc1 = Document(title=None, url='https://something.com', extract='extract2', score=2.0)
- doc2 = Document(title='Bananas and apples', url='https://something.com', extract=None, score=1.0)
-
- documents = [doc1, doc2]
-
- # Sort the documents
- ordered_results = order_results(["bananas"], documents, True)
-
- assert ordered_results[0].title == 'Bananas and apples'