Add categories extracting.

Make icon names more human-readable as well.
This commit is contained in:
Sergey Vartanov 2021-11-13 08:23:14 +03:00
parent 479983558e
commit fc8f9e1f9e
2 changed files with 45 additions and 4 deletions

View file

@ -43,12 +43,12 @@ class Shape:
path: str # SVG icon path path: str # SVG icon path
offset: np.ndarray # vector that should be used to shift the path offset: np.ndarray # vector that should be used to shift the path
id_: str # shape identifier id_: str # shape identifier
name: Optional[str] = None # icon description name: Optional[str] = None # shape human-readable description
is_right_directed: Optional[bool] = None is_right_directed: Optional[bool] = None
emojis: set[str] = field(default_factory=set) emojis: set[str] = field(default_factory=set)
is_part: bool = False is_part: bool = False
group: str = "" group: str = ""
categories: list[str] = field(default_factory=list) categories: set[str] = field(default_factory=set)
@classmethod @classmethod
def from_structure( def from_structure(
@ -70,6 +70,9 @@ class Shape:
""" """
shape: "Shape" = cls(path, offset, id_, name) shape: "Shape" = cls(path, offset, id_, name)
if "name" in structure:
shape.name = structure["name"]
if "directed" in structure: if "directed" in structure:
if structure["directed"] == "right": if structure["directed"] == "right":
shape.is_right_directed = True shape.is_right_directed = True
@ -87,7 +90,7 @@ class Shape:
shape.group = structure["group"] shape.group = structure["group"]
if "categories" in structure: if "categories" in structure:
shape.categories = structure["categories"] shape.categories = set(structure["categories"])
return shape return shape
@ -394,6 +397,14 @@ class Icon:
"""Get all shape identifiers in the icon.""" """Get all shape identifiers in the icon."""
return [x.shape.id_ for x in self.shape_specifications] return [x.shape.id_ for x in self.shape_specifications]
def has_names(self) -> bool:
"""Check whether oll shape names are known."""
for specification in self.shape_specifications:
if not specification.shape.name:
return False
return True
def get_names(self) -> list[str]: def get_names(self) -> list[str]:
"""Get all shape names in the icon.""" """Get all shape names in the icon."""
return [ return [
@ -401,6 +412,36 @@ class Icon:
for x in self.shape_specifications for x in self.shape_specifications
] ]
def get_name(self) -> str:
"""Get combined human-readable icon name."""
names: list[str] = self.get_names()
if len(names) == 1:
return names[0]
if len(names) == 2:
return names[0] + " and " + names[1]
if len(names) > 2:
return ", ".join(names[:-1]) + " and " + names[-1]
def has_categories(self) -> bool:
"""Check whether oll shape categories are known."""
for specification in self.shape_specifications:
if specification.shape.categories:
return True
return False
def get_categories(self) -> set[str]:
"""Get all shape names in the icon."""
result: set[str] = set()
for specification in self.shape_specifications:
result = result.union(specification.shape.categories)
return result
def draw( def draw(
self, self,
svg: svgwrite.Drawing, svg: svgwrite.Drawing,

View file

@ -141,7 +141,7 @@ class IconCollection:
def get_file_name(x: Icon) -> str: def get_file_name(x: Icon) -> str:
"""Generate human-readable file name.""" """Generate human-readable file name."""
return f"Röntgen {' + '.join(x.get_names())}.svg" return f"Röntgen {x.get_name()}.svg"
else: else: