diff --git a/medcat-trainer/webapp/api/api/model_cache.py b/medcat-trainer/webapp/api/api/model_cache.py index e3e2a9a84..9a841bea1 100644 --- a/medcat-trainer/webapp/api/api/model_cache.py +++ b/medcat-trainer/webapp/api/api/model_cache.py @@ -167,6 +167,9 @@ def get_medcat(project, cdb_map: Dict[str, CDB]=CDB_MAP, vocab_map: Dict[str, Vocab]=VOCAB_MAP, cat_map: Dict[str, CAT]=CAT_MAP): + cat = get_cached_medcat(project, cat_map) + if cat is not None: + return cat try: if project.model_pack is None: cat = get_medcat_from_cdb_vocab(project, cdb_map, vocab_map, cat_map) diff --git a/medcat-trainer/webapp/api/api/solr_utils.py b/medcat-trainer/webapp/api/api/solr_utils.py index 58e8c04a6..cc14ea8f0 100644 --- a/medcat-trainer/webapp/api/api/solr_utils.py +++ b/medcat-trainer/webapp/api/api/solr_utils.py @@ -165,7 +165,7 @@ def drop_collection(cdb_model: ConceptDB): def ensure_concept_searchable(cui, cdb: CDB, cdb_model: ConceptDB): """ - Adds a single cui and associated metadata is available in the assocaited solr search index. + Adds a single cui and associated metadata is available in the associated solr search index. Args: cui: concept unique identifier of the concept to make searchable cdb: the MedCAT CDB where the cui can be found diff --git a/medcat-trainer/webapp/api/api/utils.py b/medcat-trainer/webapp/api/api/utils.py index dde42cc4f..b731ad36f 100644 --- a/medcat-trainer/webapp/api/api/utils.py +++ b/medcat-trainer/webapp/api/api/utils.py @@ -237,7 +237,6 @@ def get_create_cdb_infos(cdb, concept, cui, cui_info_prop, code_prop, desc_prop, return model_clazz.objects.filter(code__in=codes) - def create_annotation(source_val: str, selection_occurrence_index: int, cui: str, user: User, project: ProjectAnnotateEntities, document: Document): text = document.text diff --git a/medcat-trainer/webapp/api/api/views.py b/medcat-trainer/webapp/api/api/views.py index 713938e52..ea8b75d69 100644 --- a/medcat-trainer/webapp/api/api/views.py +++ b/medcat-trainer/webapp/api/api/views.py @@ -435,30 +435,44 @@ def add_concept(request): cat = get_medcat(project=project) - if cui in cat.cdb.cui2names: - err_msg = f'Cannot add a concept "{name}" with cui:{cui}. CUI already linked to {cat.cdb.cui2names[cui]}' + if cui in cat.cdb.cui2info: + err_msg = f'Cannot add a concept "{name}" with cui:{cui}. CUI already linked to {cat.cdb.cui2info[cui]["preferred_name"]}' logger.error(err_msg) return Response({'err': err_msg}, 400) spacy_doc = cat(document.text) spacy_entity = None if source_val in spacy_doc.text: - start = spacy_doc.text.index(source_val) - end = start + len(source_val) - spacy_entity = [tkn for tkn in spacy_doc if tkn.idx >= start and tkn.idx <= end] - + # Find all occurrences of source_val in the text + all_occurrences_start_idxs = [] + idx = 0 + while idx != -1: + idx = spacy_doc.text.find(source_val, idx) + if idx != -1: + all_occurrences_start_idxs.append(idx) + idx += len(source_val) + + # Use selection_idx to get the correct occurrence + if sel_occur_idx < len(all_occurrences_start_idxs): + start = all_occurrences_start_idxs[sel_occur_idx] + end = start + len(source_val) + # Find tokens that overlap with the span [start, end) + # A token overlaps if: token_start < end AND token_end > start + spacy_entity = [tkn for tkn in spacy_doc if tkn.char_index < end and (tkn.char_index + len(tkn.text)) > start] + # if len(spacy_entity) == 0: + # spacy_entity = None cat.trainer.add_and_train_concept(cui=cui, name=name, name_status='P', mut_doc=spacy_doc, mut_entity=spacy_entity) + id = create_annotation(source_val=source_val, selection_occurrence_index=sel_occur_idx, cui=cui, user=user, project=project, - document=document, - cat=cat) + document=document) # ensure new concept detail is available in SOLR search service - ensure_concept_searchable(cui, cat.cdb, project.concept_db) + ensure_concept_searchable(cui, cat.cdb, project.cdb_search_filter.first()) # add to project cuis if required. if (project.cuis or project.cuis_file) and project.restrict_concept_lookup: diff --git a/medcat-trainer/webapp/frontend/src/components/anns/AddAnnotation.vue b/medcat-trainer/webapp/frontend/src/components/anns/AddAnnotation.vue index 4b00abf44..ac488c426 100644 --- a/medcat-trainer/webapp/frontend/src/components/anns/AddAnnotation.vue +++ b/medcat-trainer/webapp/frontend/src/components/anns/AddAnnotation.vue @@ -125,16 +125,14 @@ export default { cui: this.selectedCUI.cui } this.loading = true - this.$http.get(`/api/cache-model/${this.project.id}/`).then(_ => { + this.$http.post('/api/add-annotation/', payload).then(resp => { this.loading = false - this.$http.post('/api/add-annotation/', payload).then(resp => { - this.$emit('request:addAnnotationComplete', resp.data.id) - this.selectedCUI = null - }) + this.$emit('request:addAnnotationComplete', resp.data.id) + this.selectedCUI = null }).catch(err => { + this.loading = false this.errorMessage = err.response.data.message || 'Error loading model.' }) - }, cancel () { this.$emit('request:addAnnotationComplete') diff --git a/medcat-trainer/webapp/frontend/src/components/common/AddNewConcept.vue b/medcat-trainer/webapp/frontend/src/components/common/AddNewConcept.vue index 3c62b2b43..0ea1b12dd 100644 --- a/medcat-trainer/webapp/frontend/src/components/common/AddNewConcept.vue +++ b/medcat-trainer/webapp/frontend/src/components/common/AddNewConcept.vue @@ -41,8 +41,16 @@
+