Spaces:
Running
Running
File size: 2,322 Bytes
5b4fd78 6556bb5 5b4fd78 6556bb5 5b4fd78 001aa75 6556bb5 b784e3e 6556bb5 5b4fd78 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
declare type MentionType= "PRONOMINAL" | "NOMINAL" | "PROPER" | "LIST";
declare interface Mention {
index: number;
start: number;
end: number;
utterance: number;
type: MentionType;
text: string;
}
declare interface Coreference {
original: string;
resolved: string;
}
declare interface Response {
cleanedText: string;
corefResText: string;
coreferences: Coreference[];
mentions: Mention[];
singleScores: { [id: number]: number | null }; /// Is this mention likely to be a single mention (w/o any corefs). `id` is a Mention's `index`
pairScores: { [id: number]: { [id: number]: number } }; /// Pair-wise score, in `{ from: { to: ... } }` format. Non-directed arcs.
/// Single scores are to be compared to the set of pairScores (for the same mention).
/// If it's higher than every pair score, it's a single mention.
cleanedContext: string; /// Cleaned version of the context.
isResolved: boolean;
}
class Coref {
endpoint: string;
onStart = () => {};
onSuccess = () => {};
container?: HTMLElement;
constructor(endpoint: string, opts: any) {
this.endpoint = endpoint;
if (opts.onStart) {
(<any>this).onStart = opts.onStart;
}
if (opts.onSuccess) {
(<any>this).onSuccess = opts.onSuccess;
}
}
parse(text: string) {
this.onStart();
const path = `${this.endpoint}?text=${encodeURIComponent(text)}`;
const request = new XMLHttpRequest();
request.open('GET', path);
request.onload = () => {
if (request.status >= 200 && request.status < 400) {
this.onSuccess();
const res: Response = JSON.parse(request.responseText);
this.render(res);
}
else {
console.error('Error', request);
}
};
request.send();
}
render(res: Response) {
const mentions = (<any>res).mentions; // We will sort them in Displacy
for (const m of mentions) {
// Let's add each mention's singleScore
m.singleScore = res.singleScores[m.index] || undefined;
}
const markup = Displacy.render(res.cleanedText, mentions);
if (!this.container) { return ; }
// console.log(markup); // todo remove
this.container.innerHTML = `<div class="text">${markup}</div>`;
}
}
|