Skip to content
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Include token in subgraph even if not covered by segmentation node #334.

## [4.1.1] - 2026-01-13

### Added
Expand Down
12 changes: 6 additions & 6 deletions graphannis/src/annis/db/corpusstorage/subgraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,12 @@ fn get_left_right_token_with_offset_with_segmentation(
}
})?;

let left_seg = *covering_segmentation_nodes
.first()
.ok_or(GraphAnnisError::NoCoveredTokenForSubgraph)?;
let right_seg = *covering_segmentation_nodes
.last()
.ok_or(GraphAnnisError::NoCoveredTokenForSubgraph)?;
let (left_seg, right_seg) = match covering_segmentation_nodes[..] {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering if it was better to keep the error at part of the code and handle it in the calling function, thus falling back to the covered token borders in any case when there is an error. The method name could otherwise be a little bit misleading because one could expect it to only work when the segmentation coverage is given.

Since get_left_right_token_with_offset_with_segmentation is only part of the internal API of subgraph.rs and not (crate) public this probably does not make a lot of a difference where the fallback is implemented.

Also, the fallback at this position makes sense, since the segmentation parameter is only used to extend the context to the left and right, not to reduce the already existing context given by the covered token by throwing an error.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had similar thoughts when deciding where to put the fallback, and concluded on the reasoning you're giving in your last paragraph. The with_segmentation in the method name could be understood as "extend the context according to the segmentation if there is segmentation coverage" (and otherwise don't extend it).

Handling it at the call site by applying the fallback in case of any error can potentially obscure other, unrelated, errors, so I thought keeping it here is the safer option.

// If none of the covered tokens are covered by a segmentation node, return without context
[] => return Ok((left_most_covered_token, right_most_covered_token)),
[only] => (only, only),
[left, .., right] => (left, right),
};

// The context might be larger than the actual document, try to get the
// largest possible context
Expand Down
61 changes: 57 additions & 4 deletions graphannis/src/annis/db/corpusstorage/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ fn create_simple_graph(cs: &mut CorpusStorage) {
}

#[test]
fn subgraphs_simple() {
fn subgraph_simple() {
let tmp = tempfile::tempdir().unwrap();
let mut cs = CorpusStorage::with_auto_cache_size(tmp.path(), false).unwrap();

Expand Down Expand Up @@ -440,7 +440,7 @@ fn subgraphs_simple() {
}

#[test]
fn subgraphs_non_overlapping_regions() {
fn subgraph_non_overlapping_regions() {
let tmp = tempfile::tempdir().unwrap();
let mut cs = CorpusStorage::with_auto_cache_size(tmp.path(), false).unwrap();

Expand Down Expand Up @@ -573,7 +573,7 @@ fn subgraphs_non_overlapping_regions() {
}

#[test]
fn subgraphs_non_overlapping_regions_one_context_zero() {
fn subgraph_non_overlapping_regions_one_context_zero() {
let tmp = tempfile::tempdir().unwrap();
let mut cs = CorpusStorage::with_auto_cache_size(tmp.path(), false).unwrap();

Expand Down Expand Up @@ -674,7 +674,7 @@ fn subgraphs_non_overlapping_regions_one_context_zero() {
}

#[test]
fn subgraphs_non_overlapping_regions_no_context_tokens_specified_out_of_order() {
fn subgraph_non_overlapping_regions_no_context_tokens_specified_out_of_order() {
let tmp = tempfile::tempdir().unwrap();
let mut cs = CorpusStorage::with_auto_cache_size(tmp.path(), false).unwrap();

Expand Down Expand Up @@ -999,6 +999,59 @@ fn subgraph_with_segmentation_and_gap() {
.unwrap()
.is_some()
);

// Get the context for the token in the gap using the norm segmentation
let g = cs
.subgraph(
&corpus_name,
vec!["SegmentationWithGaps/doc01#tok_13".to_string()],
0,
0,
Some("norm".to_string()),
)
.unwrap();
// Check that the token is included even though it is not covered by a segmentation node
assert!(
g.get_node_annos()
.get_node_id_from_name("SegmentationWithGaps/doc01#tok_13")
.unwrap()
.is_some()
);
}

#[test]
fn subgraph_with_node_spanning_multiple_segmentation_nodes() {
let tmp = tempfile::tempdir().unwrap();
let cs = CorpusStorage::with_auto_cache_size(tmp.path(), false).unwrap();

let mut g = GraphUpdate::new();
example_generator::create_multiple_segmentations(&mut g, "root/doc1");

cs.apply_update("root", &mut g).unwrap();

let graph = cs
.subgraph(
"root",
vec!["root/doc1#b3".to_string()],
0,
0,
Some("a".to_string()),
)
.unwrap();
assert!(
graph
.get_node_annos()
.get_node_id_from_name("root/doc1#a2")
.unwrap()
.is_some()
);
assert!(
graph
.get_node_annos()
.get_node_id_from_name("root/doc1#a3")
.unwrap()
.is_some()
);
}

#[test]
Expand Down
Loading