@konfuzio/document-validation-ui 0.2.0-dev.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -153,6 +153,31 @@ const getters = {
153
153
  return annotations.length > 0;
154
154
  },
155
155
 
156
+ /* Checks if the label has annotations to show */
157
+ isLabelMultiFalseAndGroupOfAnns: (state) => (label) => {
158
+ return (
159
+ label &&
160
+ label.annotations &&
161
+ label.annotations.length > 1 &&
162
+ !label.has_multiple_top_candidates &&
163
+ state.enableGroupingFeature
164
+ );
165
+ },
166
+
167
+ /* Returns the annotations ordered by highest confidence */
168
+ annotationsByConfidence: (state) => (annotations) => {
169
+ annotations.sort((a, b) => {
170
+ if (a.confidence < b.confidence) {
171
+ return -1;
172
+ } else if (a.confidence > b.confidence) {
173
+ return 1;
174
+ }
175
+ return 0;
176
+ });
177
+
178
+ return annotations;
179
+ },
180
+
156
181
  /* Checks if the document has an annotation set */
157
182
  annotationSetExists: (state) => (annotationSetId) => {
158
183
  return state.annotationSets.find((annSet) => annSet.id === annotationSetId);
@@ -256,11 +281,16 @@ const getters = {
256
281
  /* Get label for a given annotation */
257
282
  labelOfAnnotation: (state) => (annotationToFind) => {
258
283
  let foundLabel = null;
259
- state.annotationSets.forEach((annotationSet) => {
260
- annotationSet.labels.forEach((label) => {
261
- label.annotations.forEach((annotation) => {
262
- if (annotation.id === annotationToFind.id) {
263
- foundLabel = label;
284
+ if (state.annotationSets) {
285
+ state.annotationSets.forEach((annotationSet) => {
286
+ annotationSet.labels.forEach((label) => {
287
+ label.annotations.forEach((annotation) => {
288
+ if (annotation.id === annotationToFind.id) {
289
+ foundLabel = label;
290
+ return;
291
+ }
292
+ });
293
+ if (foundLabel) {
264
294
  return;
265
295
  }
266
296
  });
@@ -268,14 +298,12 @@ const getters = {
268
298
  return;
269
299
  }
270
300
  });
271
- if (foundLabel) {
272
- return;
273
- }
274
- });
301
+ }
302
+
275
303
  return foundLabel;
276
304
  },
277
305
 
278
- getAnnotationsFiltered: (state) => {
306
+ getAnnotationsFiltered: (state, getters) => {
279
307
  // group annotations for sidebar
280
308
  let annotations = [];
281
309
  let labels = [];
@@ -331,6 +359,18 @@ const getters = {
331
359
  return false;
332
360
  };
333
361
 
362
+ const sortByConfidenceOrByAnnotationSelected = (annotations) => {
363
+ annotations = getters.annotationsByConfidence(annotations);
364
+ if (state.annotationId) {
365
+ for (let i = 0; i < annotations.length; i++) {
366
+ if (state.annotationId == annotations[i].id) {
367
+ annotations.unshift(annotations.splice(i, 1)[0]);
368
+ }
369
+ }
370
+ }
371
+ return annotations;
372
+ };
373
+
334
374
  if (state.annotationSets) {
335
375
  state.annotationSets.forEach((annotationSet) => {
336
376
  labels = [];
@@ -350,6 +390,13 @@ const getters = {
350
390
  !state.annotationFilters.showFeedbackNeeded ||
351
391
  !state.annotationFilters.showAccepted
352
392
  ) {
393
+ if (!label.has_multiple_top_candidates) {
394
+ // if multi label = false, sort by confidence
395
+ label.annotations = sortByConfidenceOrByAnnotationSelected(
396
+ label.annotations
397
+ );
398
+ }
399
+
353
400
  label.annotations.forEach((annotation) => {
354
401
  if (
355
402
  state.annotationFilters.showFeedbackNeeded &&
@@ -379,6 +426,12 @@ const getters = {
379
426
  }
380
427
  });
381
428
  } else {
429
+ if (!label.has_multiple_top_candidates) {
430
+ // if multi label = false, sort by confidence
431
+ label.annotations = sortByConfidenceOrByAnnotationSelected(
432
+ label.annotations
433
+ );
434
+ }
382
435
  // add annotations to the document array
383
436
  label.annotations.forEach((annotation) => {
384
437
  const added = addAnnotation(
@@ -963,6 +1016,15 @@ const actions = {
963
1016
  commit("SET_DOC_ID", id);
964
1017
  },
965
1018
  setAnnotationId: ({ commit, dispatch, getters }, id) => {
1019
+ if (id) {
1020
+ // check if part of label with multi ann as false
1021
+ const annotation = getters.annotationById(id);
1022
+ const label = getters.labelOfAnnotation(annotation);
1023
+ if (getters.isLabelMultiFalseAndGroupOfAnns(label)) {
1024
+ dispatch("setAnnotationAsFirstInLabel", { label, annotation });
1025
+ }
1026
+ }
1027
+
966
1028
  commit("SET_ANNOTATION_ID", id);
967
1029
  setURLAnnotationHash(id);
968
1030
  },
@@ -1542,6 +1604,63 @@ const actions = {
1542
1604
  showAcceptedAnnotations({ commit }, show) {
1543
1605
  commit("SET_SHOW_ACCEPTED_ANNOTATIONS", show);
1544
1606
  },
1607
+ unloadDocumentPage: ({ commit }, pageNumber) => {
1608
+ commit("REMOVE_PAGE", pageNumber);
1609
+ },
1610
+ putNextAnnotationInLabelFirst({ commit }, label) {
1611
+ commit("PUT_NEXT_ANN_IN_LABEL_FIRST", label);
1612
+ },
1613
+ setAnnotationAsFirstInLabel({ commit, state }, { label, annotation }) {
1614
+ state.annotationSets.forEach((annotationSet) => {
1615
+ annotationSet.labels.forEach((labelToFind) => {
1616
+ if (labelToFind.id === label.id) {
1617
+ if (labelToFind.annotations && labelToFind.annotations.length > 1) {
1618
+ for (let i = 0; i < labelToFind.annotations.length; i++) {
1619
+ if (labelToFind.annotations[i].id === annotation.id) {
1620
+ labelToFind.annotations.unshift(
1621
+ labelToFind.annotations.splice(i, 1)[0]
1622
+ );
1623
+ commit("SET_ANNOTATIONS_IN_LABEL", {
1624
+ label: labelToFind,
1625
+ annotations: labelToFind.annotations,
1626
+ });
1627
+ }
1628
+ }
1629
+ }
1630
+ }
1631
+ });
1632
+ });
1633
+ },
1634
+ putNextAnnotationInLabelFirst({ commit, state, dispatch }, label) {
1635
+ dispatch("setAnnotationId", null);
1636
+ let newFirstAnn = null;
1637
+ state.annotationSets.forEach((annotationSet) => {
1638
+ annotationSet.labels.forEach((labelToFind) => {
1639
+ if (labelToFind.id === label.id) {
1640
+ if (labelToFind.annotations && labelToFind.annotations.length > 1) {
1641
+ const firstElement = labelToFind.annotations.shift();
1642
+ labelToFind.annotations.push(firstElement);
1643
+ commit("SET_ANNOTATIONS_IN_LABEL", {
1644
+ label: labelToFind,
1645
+ annotations: labelToFind.annotations,
1646
+ });
1647
+ newFirstAnn = labelToFind.annotations[0];
1648
+ }
1649
+ }
1650
+ });
1651
+ });
1652
+
1653
+ if (newFirstAnn) {
1654
+ dispatch("setDocumentAnnotationSelected", {
1655
+ annotation: newFirstAnn,
1656
+ label: label,
1657
+ span: newFirstAnn.span[0],
1658
+ scrollTo: true,
1659
+ });
1660
+
1661
+ dispatch("scrollToDocumentAnnotationSelected");
1662
+ }
1663
+ },
1545
1664
  };
1546
1665
 
1547
1666
  const mutations = {
@@ -1687,6 +1806,13 @@ const mutations = {
1687
1806
  SET_LABELS: (state, labels) => {
1688
1807
  state.labels = labels;
1689
1808
  },
1809
+ SET_ANNOTATIONS_IN_LABEL: (state, { label, annotations }) => {
1810
+ state.labels.forEach((labelToFind) => {
1811
+ if (labelToFind.id === label.id) {
1812
+ labelToFind.annotations = annotations;
1813
+ }
1814
+ });
1815
+ },
1690
1816
  SET_EDIT_ANNOTATION: (state, editAnnotation) => {
1691
1817
  state.editAnnotation = editAnnotation;
1692
1818
  },
@@ -1815,6 +1941,9 @@ const mutations = {
1815
1941
  SET_SHOW_FEEDBACK_NEEDED_ANNOTATIONS: (state, show) => {
1816
1942
  state.annotationFilters.showFeedbackNeeded = show;
1817
1943
  },
1944
+ REMOVE_PAGE: (state, pageNumber) => {
1945
+ state.pages = state.pages.filter((p) => p.number !== pageNumber);
1946
+ },
1818
1947
  };
1819
1948
 
1820
1949
  export default {