#!/usr/bin/env python3
+# © Copyright 2021-2022, Scott Gasch
+
"""This is a module concerned with the creation of and searching of a
corpus of documents. The corpus is held in memory for fast
-searching."""
+searching.
-from __future__ import annotations
+"""
+from __future__ import annotations
import enum
import sys
from collections import defaultdict
-from typing import Any, Dict, List, NamedTuple, Optional, Sequence, Set, Tuple, Union
+from dataclasses import dataclass, field
+from typing import Any, Dict, List, Optional, Sequence, Set, Tuple, Union
class ParseError(Exception):
self.message = message
-class Document(NamedTuple):
- """A tuple representing a searchable document."""
+@dataclass
+class Document:
+ """A class representing a searchable document."""
+
+ # A unique identifier for each document.
+ docid: str = ''
+
+ # A set of tag strings for this document. May be empty.
+ tags: Set[str] = field(default_factory=set)
- docid: str # a unique idenfier for the document
- tags: Set[str] # an optional set of tags
- properties: List[Tuple[str, str]] # an optional set of key->value properties
- reference: Any # an optional reference to something else
+ # A list of key->value strings for this document. May be empty.
+ properties: List[Tuple[str, str]] = field(default_factory=list)
+
+ # An optional reference to something else; interpreted only by
+ # caller code, ignored here.
+ reference: Optional[Any] = None
class Operation(enum.Enum):
... )
>>> c.query('author:Scott and important')
{1}
+ >>> c.query('*')
+ {1, 2, 3}
+ >>> c.query('*:*')
+ {1, 2, 3}
+ >>> c.query('*:Scott')
+ {1, 3}
"""
def __init__(self) -> None:
def get_docids_by_exact_tag(self, tag: str) -> Set[str]:
"""Return the set of docids that have a particular tag."""
-
return self.docids_by_tag[tag]
def get_docids_by_searching_tags(self, tag: str) -> Set[str]:
def invert_docid_set(self, original: Set[str]) -> Set[str]:
"""Invert a set of docids."""
- return set([docid for docid in self.documents_by_docid.keys() if docid not in original])
+ return {docid for docid in self.documents_by_docid if docid not in original}
def get_doc(self, docid: str) -> Optional[Document]:
"""Given a docid, retrieve the previously added Document."""
key, value = tag.split(":")
except ValueError as v:
raise ParseError(f'Invalid key:value syntax at "{tag}"') from v
- if value == "*":
- r = self.corpus.get_docids_with_property(key)
+
+ if key == '*':
+ r = set()
+ for kv, s in self.corpus.docids_by_property.items():
+ if value in ('*', kv[1]):
+ r.update(s)
else:
- r = self.corpus.get_docids_by_property(key, value)
+ if value == '*':
+ r = self.corpus.get_docids_with_property(key)
+ else:
+ r = self.corpus.get_docids_by_property(key, value)
else:
- r = self.corpus.get_docids_by_exact_tag(tag)
+ if tag == '*':
+ r = set()
+ for s in self.corpus.docids_by_tag.values():
+ r.update(s)
+ else:
+ r = self.corpus.get_docids_by_exact_tag(tag)
retval.update(r)
else:
raise ParseError(f"Unexpected query {tag}")