From 20df0d9671143f019b77230587b3fd92523026c1 Mon Sep 17 00:00:00 2001
From: Ruben Bimberg <ruben.bimberg@mni.thm.de>
Date: Sat, 19 Jun 2021 00:07:42 +0200
Subject: [PATCH] Change endpoint to spacy

Related to https://git.thm.de/rbmb29/spacy-api-docker/-/commit/a9ec007db16dac56956b4fb77dd078a221641da9
---
 proxy.conf.json                               |  2 +-
 .../spacy-dialog/spacy-dialog.component.ts    |  5 +-
 src/app/services/http/spacy.service.ts        | 63 +++++--------------
 3 files changed, 20 insertions(+), 50 deletions(-)

diff --git a/proxy.conf.json b/proxy.conf.json
index 603e23b9e..9e32ed540 100644
--- a/proxy.conf.json
+++ b/proxy.conf.json
@@ -9,7 +9,7 @@
     "logLevel": "debug"
   },
   "/spacy": {
-    "target": "https://spacy.frag.jetzt/noun",
+    "target": "https://spacy.frag.jetzt/spacy",
     "secure": true,
     "changeOrigin": true,
     "pathRewrite": {
diff --git a/src/app/components/shared/_dialogs/spacy-dialog/spacy-dialog.component.ts b/src/app/components/shared/_dialogs/spacy-dialog/spacy-dialog.component.ts
index c77fddc5e..37233b4fd 100644
--- a/src/app/components/shared/_dialogs/spacy-dialog/spacy-dialog.component.ts
+++ b/src/app/components/shared/_dialogs/spacy-dialog/spacy-dialog.component.ts
@@ -72,10 +72,9 @@ export class SpacyDialogComponent implements OnInit, AfterContentInit {
       .subscribe(words => {
         const keywords: Keyword[] = [];
         for (const word of words) {
-          const newWord = word.trim();
-          if (keywords.findIndex(item => item.word === newWord) < 0) {
+          if (keywords.findIndex(item => item.word === word) < 0) {
             keywords.push({
-              word: newWord,
+              word,
               completed: false,
               editing: false,
               selected: false
diff --git a/src/app/services/http/spacy.service.ts b/src/app/services/http/spacy.service.ts
index f8965a97a..5e04619a0 100644
--- a/src/app/services/http/spacy.service.ts
+++ b/src/app/services/http/spacy.service.ts
@@ -6,22 +6,24 @@ import { catchError, map, tap } from 'rxjs/operators';
 
 export type Model = 'de' | 'en' | 'fr' | 'es' | 'it' | 'nl' | 'pt' | 'auto';
 
-//[B]egin, [I]nside, [O]utside or unset
-type EntityPosition = 'B' | 'I' | 'O' | '';
+type KeywordType = 'entity' | 'noun';
+
+interface NounKeyword {
+  type: KeywordType;
+  lemma: string;
+  text: string;
+  dep: string;
+  tag: string;
+  pos: string;
+}
 
-interface NounToken {
-  dep: string; // dependency inside the sentence
-  // eslint-disable-next-line @typescript-eslint/naming-convention
-  entity_pos: EntityPosition; // entity position
-  // eslint-disable-next-line @typescript-eslint/naming-convention
-  entity_type: string; // entity type
-  lemma: string; // lemma of token
-  tag: string; // tag of token
-  text: string; // text of token
+interface EntityKeyword extends NounKeyword {
+  entityType: string;
 }
 
-type NounCompound = NounToken[];
-type NounCompoundList = NounCompound[];
+type AbstractKeyword = NounKeyword | EntityKeyword;
+
+type KeywordList = AbstractKeyword[];
 
 const httpOptions = {
   // eslint-disable-next-line @typescript-eslint/naming-convention
@@ -37,44 +39,13 @@ export class SpacyService extends BaseHttpService {
     super();
   }
 
-  private static processCompound(result: string[], data: NounCompound) {
-    let isInEntity = false;
-    let start = 0;
-    const pushNew = (i: number) => {
-      if (start < i) {
-        result.push(data.slice(start, i).reduce((acc, current) => acc + ' ' + current.lemma, ''));
-        start = i;
-      }
-    };
-    data.forEach((noun, i) => {
-      if (noun.entity_pos === 'B' || (noun.entity_pos === 'I' && !isInEntity)) {
-        // entity begins
-        pushNew(i);
-        isInEntity = true;
-      } else if (isInEntity) {
-        if (noun.entity_pos === '' || noun.entity_pos === 'O') {
-          // entity ends
-          pushNew(i);
-          isInEntity = false;
-        }
-      }
-    });
-    pushNew(data.length);
-  }
-
   getKeywords(text: string, model: Model): Observable<string[]> {
     const url = '/spacy';
-    return this.http.post<NounCompoundList>(url, {text, model}, httpOptions)
+    return this.http.post<KeywordList>(url, {text, model}, httpOptions)
       .pipe(
         tap(_ => ''),
         catchError(this.handleError<any>('getKeywords')),
-        map((result: NounCompoundList) => {
-          const filteredNouns: string[] = [];
-          result.forEach(compound => {
-            SpacyService.processCompound(filteredNouns, compound);
-          });
-          return filteredNouns;
-        })
+        map((result: KeywordList) => result.map(e => e.type === 'entity' ? e.text.trim() : e.lemma.trim()))
       );
   }
 }
-- 
GitLab