Skip to content

Commit

Permalink
Merge pull request #313 from dkpro/feature/312-Ability-to-retrieve-a-…
Browse files Browse the repository at this point in the history
…type-by-its-short-name

#312 - Ability to retrieve a type by its short name
  • Loading branch information
reckart committed May 4, 2024
2 parents 1fa0932 + 4b0b5b2 commit 12f7c22
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
34 changes: 28 additions & 6 deletions cassis/typesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,15 +847,24 @@ def __iter__(self) -> Iterator[Type]:
return self.get_types()

def contains_type(self, typename: str) -> bool:
"""Checks whether this type system contains a type with name `typename`.
"""Checks whether this type system contains a type with name `typename`. If a type is a short name (i.e. it
does not contain a dot) and there is exactly one type with this short name, this method will consider the
type to be included in the type system.
Args:
typename: The name of type whose existence is to be checked.
Returns:
`True` if a type with `typename` exists, else `False`.
"""
return typename in self._types

if "." in typename:
return typename in self._types

try:
return self.get_type(typename) is not None
except TypeNotFoundError:
return False

def create_type(self, name: str, supertypeName: str = TYPE_NAME_ANNOTATION, description: str = None) -> Type:
"""Creates a new type and return it.
Expand Down Expand Up @@ -887,7 +896,8 @@ def create_type(self, name: str, supertypeName: str = TYPE_NAME_ANNOTATION, desc
return new_type

def get_type(self, type_name: str) -> Type:
"""Finds a type by name in the type system of this CAS.
"""Finds a type by name in the type system of this CAS. If a type is a short name (i.e. it
does not contain a dot) and there is exactly one type with this short name, this method will return this type.
Args:
typename: The name of the type to retrieve
Expand All @@ -897,10 +907,22 @@ def get_type(self, type_name: str) -> Type:
Raises:
Exception: If no type with `typename` could be found.
"""
if self.contains_type(type_name):
if type_name in self._types:
return self._types[type_name]
else:
raise TypeNotFoundError(f"Type with name [{type_name}] not found!")

if "." not in type_name:
types_by_simple_name = defaultdict(list)
for tn, t in self._types.items():
types_by_simple_name[t.short_name].append(t)

if types_by_simple_name[type_name]:
candidates = types_by_simple_name[type_name]
if len(candidates) == 1:
return candidates[0]
elif len(candidates) > 1:
raise TypeNotFoundError(f"Multiple types with short name [{type_name}] found: {candidates}")

raise TypeNotFoundError(f"Type with name [{type_name}] not found!")

def get_types(self, built_in: bool = False) -> Iterator[Type]:
"""Returns all types of this type system. Normally, this excludes the built-in types
Expand Down
2 changes: 2 additions & 0 deletions tests/test_cas.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ def test_select(small_typesystem_xml, tokens, sentences):
cas = Cas(typesystem=ts)
cas.add_all(tokens + sentences)

assert list(cas.select("Token")) == tokens
assert list(cas.select("cassis.Token")) == tokens
assert list(cas.select("Sentence")) == sentences
assert list(cas.select("cassis.Sentence")) == sentences
assert list(cas.select(ts.get_type("cassis.Token"))) == tokens
assert list(cas.select(ts.get_type("cassis.Sentence"))) == sentences
Expand Down
9 changes: 9 additions & 0 deletions tests/test_typesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,10 +515,19 @@ def test_type_name():
cas = Cas()

Annotation = cas.typesystem.get_type(TYPE_NAME_ANNOTATION)
assert cas.typesystem.contains_type(TYPE_NAME_ANNOTATION)
assert Annotation.name == TYPE_NAME_ANNOTATION
assert Annotation.short_name == "Annotation"


def test_get_type_by_short_name():
cas = Cas()

Annotation = cas.typesystem.get_type("Annotation")
assert cas.typesystem.contains_type("Annotation")
assert Annotation.name == TYPE_NAME_ANNOTATION


def test_get_types():
cas = Cas()

Expand Down

0 comments on commit 12f7c22

Please sign in to comment.