caselawclient.search_parameters
1import re 2from dataclasses import dataclass 3from typing import Any, Dict, List, Optional, Set 4 5RESULTS_PER_PAGE = 10 6QUOTED_PHRASE_REGEX = '"([^"]*)"' 7 8 9@dataclass 10class SearchParameters: 11 """Represents search parameters for a case law search.""" 12 13 query: Optional[str] = None 14 court: Optional[str] = None 15 judge: Optional[str] = None 16 party: Optional[str] = None 17 neutral_citation: Optional[str] = None 18 document_name: Optional[str] = None 19 consignment_number: Optional[str] = None 20 specific_keyword: Optional[str] = None 21 order: Optional[str] = None 22 date_from: Optional[str] = None 23 date_to: Optional[str] = None 24 page: int = 1 25 page_size: int = RESULTS_PER_PAGE 26 show_unpublished: bool = False 27 only_unpublished: bool = False 28 only_with_html_representation: bool = False 29 collections: Optional[List[str]] = None 30 31 def as_marklogic_payload(self) -> Dict[str, Any]: 32 """ 33 Converts the search parameters to a dictionary payload suitable 34 fo MarkLogic. 35 """ 36 return { 37 "court": self._marklogic_courts, 38 "judge": str(self.judge or ""), 39 "page": max(1, int(self.page)), 40 "page-size": int(self.page_size), 41 "q": str(self.query or ""), 42 "party": str(self.party or ""), 43 "neutral_citation": str(self.neutral_citation or ""), 44 "document_name": str(self.document_name or ""), 45 "consignment_number": str(self.consignment_number or ""), 46 "specific_keyword": str(self.specific_keyword or ""), 47 "order": str(self.order or ""), 48 "from": str(self.date_from or ""), 49 "to": str(self.date_to or ""), 50 "show_unpublished": str(self.show_unpublished).lower(), 51 "only_unpublished": str(self.only_unpublished).lower(), 52 "only_with_html_representation": str(self.only_with_html_representation).lower(), 53 "collections": self._marklogic_collections, 54 "quoted_phrases": self._quoted_phrases, 55 } 56 57 @property 58 def _marklogic_collections(self) -> str: 59 return ",".join(self.collections or []).replace(" ", "").replace(",,", ",") 60 61 @property 62 def _marklogic_courts(self) -> Optional[List[str]]: 63 court_text = self._join_court_text(self.court or "") 64 if not (court_text).strip(): 65 return None 66 courts = self._court_list_splitter(court_text) 67 alternative_court_names = self._get_alternative_court_names(courts) 68 return list(courts | alternative_court_names) 69 70 @property 71 def _quoted_phrases(self) -> List[str]: 72 if self.query is None: 73 return [] 74 return re.findall(QUOTED_PHRASE_REGEX, self.query) 75 76 @staticmethod 77 def _join_court_text(court_text: str) -> str: 78 return ",".join(court_text) if isinstance(court_text, list) else court_text 79 80 @staticmethod 81 def _court_list_splitter(court_text: str) -> Set[str]: 82 return set(court_text.lower().replace(" ", "").split(",")) 83 84 @staticmethod 85 def _get_alternative_court_names(courts: Set[str]) -> Set[str]: 86 ALTERNATIVE_COURT_NAMES_MAP = { 87 "ewhc/qb": "ewhc/kb", 88 "ewhc/kb": "ewhc/qb", 89 "ewhc/scco": "ewhc/costs", 90 "ewhc/costs": "ewhc/scco", 91 "ukait": "ukut/iac", 92 "ukut/iac": "ukait", 93 } 94 alternative_court_names = set() 95 for primary_name, secondary_name in ALTERNATIVE_COURT_NAMES_MAP.items(): 96 if primary_name in courts and secondary_name not in courts: 97 alternative_court_names.add(secondary_name) 98 return alternative_court_names
RESULTS_PER_PAGE =
10
QUOTED_PHRASE_REGEX =
'"([^"]*)"'
@dataclass
class
SearchParameters:
10@dataclass 11class SearchParameters: 12 """Represents search parameters for a case law search.""" 13 14 query: Optional[str] = None 15 court: Optional[str] = None 16 judge: Optional[str] = None 17 party: Optional[str] = None 18 neutral_citation: Optional[str] = None 19 document_name: Optional[str] = None 20 consignment_number: Optional[str] = None 21 specific_keyword: Optional[str] = None 22 order: Optional[str] = None 23 date_from: Optional[str] = None 24 date_to: Optional[str] = None 25 page: int = 1 26 page_size: int = RESULTS_PER_PAGE 27 show_unpublished: bool = False 28 only_unpublished: bool = False 29 only_with_html_representation: bool = False 30 collections: Optional[List[str]] = None 31 32 def as_marklogic_payload(self) -> Dict[str, Any]: 33 """ 34 Converts the search parameters to a dictionary payload suitable 35 fo MarkLogic. 36 """ 37 return { 38 "court": self._marklogic_courts, 39 "judge": str(self.judge or ""), 40 "page": max(1, int(self.page)), 41 "page-size": int(self.page_size), 42 "q": str(self.query or ""), 43 "party": str(self.party or ""), 44 "neutral_citation": str(self.neutral_citation or ""), 45 "document_name": str(self.document_name or ""), 46 "consignment_number": str(self.consignment_number or ""), 47 "specific_keyword": str(self.specific_keyword or ""), 48 "order": str(self.order or ""), 49 "from": str(self.date_from or ""), 50 "to": str(self.date_to or ""), 51 "show_unpublished": str(self.show_unpublished).lower(), 52 "only_unpublished": str(self.only_unpublished).lower(), 53 "only_with_html_representation": str(self.only_with_html_representation).lower(), 54 "collections": self._marklogic_collections, 55 "quoted_phrases": self._quoted_phrases, 56 } 57 58 @property 59 def _marklogic_collections(self) -> str: 60 return ",".join(self.collections or []).replace(" ", "").replace(",,", ",") 61 62 @property 63 def _marklogic_courts(self) -> Optional[List[str]]: 64 court_text = self._join_court_text(self.court or "") 65 if not (court_text).strip(): 66 return None 67 courts = self._court_list_splitter(court_text) 68 alternative_court_names = self._get_alternative_court_names(courts) 69 return list(courts | alternative_court_names) 70 71 @property 72 def _quoted_phrases(self) -> List[str]: 73 if self.query is None: 74 return [] 75 return re.findall(QUOTED_PHRASE_REGEX, self.query) 76 77 @staticmethod 78 def _join_court_text(court_text: str) -> str: 79 return ",".join(court_text) if isinstance(court_text, list) else court_text 80 81 @staticmethod 82 def _court_list_splitter(court_text: str) -> Set[str]: 83 return set(court_text.lower().replace(" ", "").split(",")) 84 85 @staticmethod 86 def _get_alternative_court_names(courts: Set[str]) -> Set[str]: 87 ALTERNATIVE_COURT_NAMES_MAP = { 88 "ewhc/qb": "ewhc/kb", 89 "ewhc/kb": "ewhc/qb", 90 "ewhc/scco": "ewhc/costs", 91 "ewhc/costs": "ewhc/scco", 92 "ukait": "ukut/iac", 93 "ukut/iac": "ukait", 94 } 95 alternative_court_names = set() 96 for primary_name, secondary_name in ALTERNATIVE_COURT_NAMES_MAP.items(): 97 if primary_name in courts and secondary_name not in courts: 98 alternative_court_names.add(secondary_name) 99 return alternative_court_names
Represents search parameters for a case law search.
SearchParameters( query: Optional[str] = None, court: Optional[str] = None, judge: Optional[str] = None, party: Optional[str] = None, neutral_citation: Optional[str] = None, document_name: Optional[str] = None, consignment_number: Optional[str] = None, specific_keyword: Optional[str] = None, order: Optional[str] = None, date_from: Optional[str] = None, date_to: Optional[str] = None, page: int = 1, page_size: int = 10, show_unpublished: bool = False, only_unpublished: bool = False, only_with_html_representation: bool = False, collections: Optional[List[str]] = None)
def
as_marklogic_payload(self) -> Dict[str, Any]:
32 def as_marklogic_payload(self) -> Dict[str, Any]: 33 """ 34 Converts the search parameters to a dictionary payload suitable 35 fo MarkLogic. 36 """ 37 return { 38 "court": self._marklogic_courts, 39 "judge": str(self.judge or ""), 40 "page": max(1, int(self.page)), 41 "page-size": int(self.page_size), 42 "q": str(self.query or ""), 43 "party": str(self.party or ""), 44 "neutral_citation": str(self.neutral_citation or ""), 45 "document_name": str(self.document_name or ""), 46 "consignment_number": str(self.consignment_number or ""), 47 "specific_keyword": str(self.specific_keyword or ""), 48 "order": str(self.order or ""), 49 "from": str(self.date_from or ""), 50 "to": str(self.date_to or ""), 51 "show_unpublished": str(self.show_unpublished).lower(), 52 "only_unpublished": str(self.only_unpublished).lower(), 53 "only_with_html_representation": str(self.only_with_html_representation).lower(), 54 "collections": self._marklogic_collections, 55 "quoted_phrases": self._quoted_phrases, 56 }
Converts the search parameters to a dictionary payload suitable fo MarkLogic.