Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#259 - Wrong entities are removed when iterating through CAS #260

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cassis/cas.py
Original file line number Diff line number Diff line change
Expand Up @@ -788,9 +788,9 @@ def _copy(self) -> "Cas":
return result


def _sort_func(a: FeatureStructure) -> Tuple[int, int]:
def _sort_func(a: FeatureStructure) -> Tuple[int, int, int]:
d = a.__slots__
if "begin" in d and "end" in d:
return (a.begin, a.end)
return a.begin, a.end, id(a)
else:
return (sys.maxsize, sys.maxsize)
return sys.maxsize, sys.maxsize, id(a)
23 changes: 23 additions & 0 deletions tests/test_cas.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
TYPE_NAME_ANNOTATION,
TYPE_NAME_INTEGER,
TYPE_NAME_INTEGER_ARRAY,
TYPE_NAME_STRING,
TYPE_NAME_TOP,
AnnotationHasNoSofa,
)
Expand Down Expand Up @@ -450,6 +451,28 @@ def test_removing_throws_if_fs_in_other_view(small_typesystem_xml, tokens, sente
view.remove(tokens[0])


def test_removing_many_annotations():
typesystem = TypeSystem()
NamedEntity = typesystem.create_type(name="NamedEntity", supertypeName=TYPE_NAME_ANNOTATION)
typesystem.create_feature(domainType=NamedEntity, name="source", rangeType=TYPE_NAME_STRING)

count_to_generate = 100

cas = Cas(typesystem)
for i in range(count_to_generate):
cas.add(NamedEntity(source=("A" if (i % 2) else "B")))

assert len(cas.select(NamedEntity.name)) == count_to_generate
assert sum(1 for e in cas.select(NamedEntity.name) if e.source == "A") == count_to_generate / 2
assert sum(1 for e in cas.select(NamedEntity.name) if e.source == "B") == count_to_generate / 2

for e in cas.select(NamedEntity.name):
if e.source == "A":
cas.remove(e)
assert sum(1 for e in cas.select(NamedEntity.name) if e.source == "B") == count_to_generate / 2
assert sum(1 for e in cas.select(NamedEntity.name) if e.source == "A") == 0


def test_fail_on_duplicate_fs_id(small_typesystem_xml):
cas = Cas(typesystem=load_typesystem(small_typesystem_xml))

Expand Down