Skip to content

Commit

Permalink
Merge pull request #594 from kbss-cvut/enhancement#587-remember-manua…
Browse files Browse the repository at this point in the history
…l-annotations

[Enhancement #587] Use backend to generated new occurrence selectors.
  • Loading branch information
ledsoft authored Dec 10, 2024
2 parents 617b291 + 9023ac0 commit 2111cc9
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 53 deletions.
33 changes: 0 additions & 33 deletions src/component/annotator/AnnotationDomHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@ import {
import VocabularyUtils from "../../util/VocabularyUtils";
import HtmlParserUtils from "./HtmlParserUtils";
import HtmlDomUtils from "./HtmlDomUtils";
import { TextQuoteSelector } from "../../model/TermOccurrence";
import Utils from "../../util/Utils";

export const AnnotationType = {
OCCURRENCE: VocabularyUtils.TERM_OCCURRENCE,
DEFINITION: VocabularyUtils.DEFINITION,
};

export const SELECTOR_CONTEXT_LENGTH = 32;

function toHtmlString(nodeList: NodeList): string {
let result = "";
for (let i = 0; i < nodeList.length; i++) {
Expand Down Expand Up @@ -120,13 +117,6 @@ const AnnotationDomHelper = {
};
},

replaceAnnotation(
oldAnnotation: DomHandlerNode,
newAnnotation: DomHandlerNode
): void {
DomUtils.replaceElement(oldAnnotation, newAnnotation);
},

createNewAnnotation(
about: string,
nodeList: NodeList,
Expand Down Expand Up @@ -159,29 +149,6 @@ const AnnotationDomHelper = {
}
return score <= Number(node.attribs.score);
},

generateSelector(node: DomHandlerNode): TextQuoteSelector {
let prefix = undefined;
let suffix = undefined;
if (node.previousSibling) {
prefix = HtmlDomUtils.getTextContent(node.previousSibling);
if (prefix.length > SELECTOR_CONTEXT_LENGTH) {
prefix = prefix.substring(prefix.length - SELECTOR_CONTEXT_LENGTH);
}
}
if (node.nextSibling) {
suffix = HtmlDomUtils.getTextContent(node.nextSibling);
if (suffix.length > SELECTOR_CONTEXT_LENGTH) {
suffix = suffix.substring(0, SELECTOR_CONTEXT_LENGTH);
}
}
return {
exactMatch: HtmlDomUtils.getTextContent(node),
prefix,
suffix,
types: [VocabularyUtils.TEXT_QUOTE_SELECTOR],
};
},
};

export default AnnotationDomHelper;
9 changes: 6 additions & 3 deletions src/component/annotator/AnnotatorUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import VocabularyUtils, { IRI, IRIImpl } from "../../util/VocabularyUtils";
import JsonLdUtils from "../../util/JsonLdUtils";
import Term from "../../model/Term";
import TermOccurrence from "../../model/TermOccurrence";
import AnnotationDomHelper from "./AnnotationDomHelper";

const OCCURRENCE_SEPARATOR = "/occurrences";

Expand All @@ -22,7 +21,7 @@ export function createTermOccurrence(
annotationElement: Element,
fileIri: IRI
) {
return new TermOccurrence({
const to = new TermOccurrence({
iri: annotationIdToTermOccurrenceIri(
annotationElement.attribs.about,
fileIri
Expand All @@ -32,9 +31,13 @@ export function createTermOccurrence(
source: {
iri: fileIri.namespace + fileIri.fragment,
},
selectors: [AnnotationDomHelper.generateSelector(annotationElement)],
selectors: [],
types: [VocabularyUtils.FILE_OCCURRENCE_TARGET],
},
types: [],
});
to.elementAbout = annotationElement.attribs.about.substring(
JsonLdUtils.BNODE_PREFIX.length
);
return to;
}
17 changes: 0 additions & 17 deletions src/component/annotator/__tests__/AnnotationDomHelper.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -290,21 +290,4 @@ describe("AnnotationDomHelper", () => {
);
});
});

describe("generateSelector", () => {
it("creates TextQuoteSelector from text content of the specified node", () => {
const selector = sut.generateSelector(annotationSpan);
expect(selector).toBeDefined();
expect(selector.exactMatch).toEqual(
(annotationSpan.children![0] as DataNode).data
);
});

it("uses previous and next siblings to provide selector prefix and suffix", () => {
const selector = sut.generateSelector(annotationSpan);
expect(selector).toBeDefined();
expect(selector.prefix).toEqual("First paragraph.\n ");
expect(selector.suffix).toEqual("\n ");
});
});
});
3 changes: 3 additions & 0 deletions src/model/TermOccurrence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const textQuoteSelectorCtx = {
exactMatch: VocabularyUtils.NS_TERMIT + "má-přesný-text-quote",
prefix: VocabularyUtils.NS_TERMIT + "má-prefix-text-quote",
suffix: VocabularyUtils.NS_TERMIT + "má-suffix-text-quote",
elementAbout: VocabularyUtils.DC_IDENTIFIER,
};

/**
Expand Down Expand Up @@ -56,6 +57,8 @@ export interface TermOccurrenceData extends TermAssignmentData {
export default class TermOccurrence extends TermAssignment {
public target: OccurrenceTarget;

public elementAbout?: string;

constructor(data: TermOccurrenceData) {
super(data);
this.target = data.target;
Expand Down

0 comments on commit 2111cc9

Please sign in to comment.