diff --git a/proxy.conf.json b/proxy.conf.json index 603e23b9ea844bcf7f57f2590ea71217ea63268e..9e32ed540c3c24e8829e7a41db74fbe91642c9ca 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 cd133545067017ceca5b94000c546f0910ad29f0..23ef9c3ebb2260255822b95ff9cb8277fe1f5f4d 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 @@ -5,6 +5,7 @@ import { CreateCommentComponent } from '../create-comment/create-comment.compone import { SpacyService, Model } from '../../../../services/http/spacy.service'; import { LanguageService } from '../../../../services/util/language.service'; import { Comment } from '../../../../models/comment'; +import { map } from 'rxjs/operators'; export interface Keyword { word: string; @@ -44,8 +45,7 @@ export class SpacyDialogComponent implements OnInit, AfterContentInit { } ngAfterContentInit(): void { - if(this.langSupported) { - this.commentBodyChecked = this.commentBodyChecked.replace(/[#*_]+/g ,''); + if (this.langSupported) { this.evalInput(this.commentLang); } } @@ -70,23 +70,21 @@ export class SpacyDialogComponent implements OnInit, AfterContentInit { // N at first pos = all Nouns(NN de/en) including singular(NN, NNP en), plural (NNPS, NNS en), proper Noun(NNE, NE de) this.spacyService.getKeywords(this.commentBodyChecked, model) + .pipe( + map(keywords => keywords.map(keyword => ({ + word: keyword, + completed: false, + editing: false, + selected: false + } as Keyword))) + ) .subscribe(words => { - const keywords: Keyword[] = []; - for (const word of words) { - const newWord = word.trim(); - if (keywords.findIndex(item => item.word === newWord) < 0) { - keywords.push({ - word: newWord, - completed: false, - editing: false, - selected: false - }); - } + this.keywords = words; + //deep copy + this.keywordsOriginal = [...words]; + for (let i = 0; i < this.keywordsOriginal.length; i++) { + this.keywordsOriginal[i] = {...this.keywordsOriginal[i]}; } - - // Deep copy - this.keywords = keywords; - this.keywordsOriginal = JSON.parse(JSON.stringify(keywords)); }, () => { this.keywords = []; this.keywordsOriginal = []; @@ -122,17 +120,17 @@ export class SpacyDialogComponent implements OnInit, AfterContentInit { } allKeywordsSelected(): boolean { - for(const kw of this.keywords) { - if(!kw.selected) { + for (const kw of this.keywords) { + if (!kw.selected) { return false; } } return true; } - manualKeywordsToKeywords(){ - const tempKeywords = this.manualKeywords.replace(/\s/g,''); - if(tempKeywords.length) { + manualKeywordsToKeywords() { + const tempKeywords = this.manualKeywords.replace(/\s/g, ''); + if (tempKeywords.length) { this.keywords = tempKeywords.split(',').map((keyword) => ( { word: keyword, diff --git a/src/app/services/http/spacy.service.ts b/src/app/services/http/spacy.service.ts index f8965a97af82353795b7a9a5a8b739e3b23f32b4..5ca7574bd2f0b932b12a33d065a924ec782028a7 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,43 +39,21 @@ 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); + map((result: KeywordList) => { + const keywords = []; + result.forEach(e => { + const keyword = e.type === 'entity' ? e.text.trim() : e.lemma.trim(); + if (keywords.findIndex(word => word === keyword) < 0) { + keywords.push(keyword); + } }); - return filteredNouns; + return keywords; }) ); }