""" Classes representing HED search results and tokens. """
[docs]class SearchResult:
""" Holder for and manipulation of search results. """
[docs] def __init__(self, group, tag):
self.group = group
# todo: rename tag: children
if not isinstance(tag, list):
new_tags = [tag]
else:
new_tags = tag.copy()
self.tags = new_tags
[docs] def merge_and_result(self, other):
"""Returns a new result, with the combined tags/groups from this and other."""
# Returns a new
new_tags = self.tags.copy()
for tag in other.tags:
if any(tag is this_tag for this_tag in self.tags):
continue
new_tags.append(tag)
new_tags.sort(key=lambda x: str(x))
if self.group != other.group:
raise ValueError("Internal error")
return SearchResult(self.group, new_tags)
def __str__(self):
return str(self.group) + " Tags: " + "---".join([str(tag) for tag in self.tags])
[docs]class Token:
"""Represents a single term/character"""
And = 0
Tag = 1
DescendantGroup = 4
DescendantGroupEnd = 5
Or = 6
LogicalGroup = 7
LogicalGroupEnd = 8
LogicalNegation = 9
Wildcard = 10
ExactMatch = 11
ExactMatchEnd = 12
ExactMatchOptional = 14
NotInLine = 13 # Not currently a token. In development and may become one.
[docs] def __init__(self, text):
tokens = {
",": Token.And,
"and": Token.And,
"or": Token.Or,
"[": Token.DescendantGroup,
"]": Token.DescendantGroupEnd,
"(": Token.LogicalGroup,
")": Token.LogicalGroupEnd,
"~": Token.LogicalNegation,
"?": Token.Wildcard, # Any tag or group
"??": Token.Wildcard, # Any tag
"???": Token.Wildcard, # Any Group
"{": Token.ExactMatch, # Nothing else
"}": Token.ExactMatchEnd, # Nothing else
":": Token.ExactMatchOptional,
"@": Token.NotInLine
}
self.kind = tokens.get(text, Token.Tag)
self.text = text
def __str__(self):
return self.text
def __eq__(self, other):
if self.kind == other:
return True
return False