nopaque/app/static/js/CorpusAnalysis/CQiClient.js

701 lines
18 KiB
JavaScript
Raw Normal View History

2021-11-16 14:23:57 +00:00
class CQiClient {
constructor(corpusId) {
this.socket = io(
'/corpora/corpus/corpus_analysis',
2021-12-01 15:03:55 +00:00
{
auth: {corpus_id: corpusId},
transports: ['websocket'],
upgrade: false
}
2021-11-16 14:23:57 +00:00
);
this.connected = false;
this.corpora = new CQiCorpusCollection(this.socket);
}
connect() {
return new Promise((resolve, reject) => {
this.socket.emit('cqi.connect', response => {
if (response.code === 200) {
this.connected = true;
resolve(response.payload);
} else {
reject(response);
}
});
});
}
disconnect() {
return new Promise((resolve, reject) => {
this.socket.emit('cqi.disconnect', response => {
if (response.code === 200) {
this.connected = false;
resolve(response.payload);
} else {
reject(response);
}
});
});
}
ping() {
return new Promise((resolve, reject) => {
this.socket.emit('cqi.ping', response => {
if (response.code === 200) {
resolve(response.payload);
} else {
reject(response);
}
});
});
}
}
class CQiCorpusCollection {
constructor(socket) {
this.socket = socket;
}
get(corpusName) {
return new Promise((resolve, reject) => {
2021-12-01 15:03:55 +00:00
const args = {corpus_name: corpusName};
2021-11-16 14:23:57 +00:00
this.socket.emit('cqi.corpora.get', args, response => {
if (response.code === 200) {
resolve(new CQiCorpus(this.socket, response.payload));
} else {
reject(response);
}
});
});
}
list() {
return new Promise((resolve, reject) => {
this.socket.emit('cqi.corpora.list', response => {
if (response.code === 200) {
resolve(response.payload.map(x => {return new CQiSubcorpus(this.socket, x);}));
} else {
reject(response);
}
});
});
}
}
class CQiCorpus {
constructor(socket, attrs) {
this.socket = socket;
this.charset = attrs.charset;
this.name = attrs.name;
this.properties = attrs.properties;
this.size = attrs.size;
this.alignmentAttributes = new CQiAlignmentAttributeCollection(this.socket, this);
this.positionalAttributes = new CQiPositionalAttributeCollection(this.socket, this);
this.structuralAttributes = new CQiStructuralAttributeCollection(this.socket, this);
this.subcorpora = new CQiSubcorpusCollection(this.socket, this);
}
getVisualizationData() {
return new Promise((resolve, reject) => {
const args = {corpus_name: this.name};
this.socket.emit('cqi.corpora.corpus.get_visualization_data', args, response => {
if (response.code === 200) {
resolve(response.payload);
} else {
reject(response);
}
});
});
}
getCorpusData() {
return new Promise((resolve, reject) => {
2023-06-13 13:41:34 +00:00
const dummyData = {
2023-06-19 11:41:56 +00:00
"corpus": {
"bounds": [1, 689],
"counts": {
"token": 743,
"ent": 321,
"s": 234
},
"freqs": {
"word": {
"1": 876,
"2": 234,
"3": 657
2023-06-13 13:41:34 +00:00
},
2023-06-19 11:41:56 +00:00
"lemma": {
"1": 543,
"2": 876,
"3": 321
2023-06-13 13:41:34 +00:00
},
2023-06-19 11:41:56 +00:00
"pos": {
"1": 456,
"2": 789,
"3": 234
},
"simple_pos": {
"1": 987,
"2": 876,
"3": 543
},
"ent": {
"1": 654,
"2": 321,
"3": 987
}
}
},
"text": {
"1": {
"bounds": [0, 435],
"counts": {
"token": 345,
"ent_type": 123,
"s": 89
},
"freqs": {
"word": {
"1": 25,
"2": 90,
"3": 200
},
"lemma": {
"1": 654,
"2": 321,
"3": 987
},
"pos": {
"1": 543,
"2": 876,
"3": 234
},
"simple_pos": {
"1": 987,
"2": 654,
"3": 321
},
"ent_type": {
"1": 234,
"2": 789,
"3": 543
}
},
"values": {
"author": 1,
"publishing_year":1950,
"title": 1
}
2023-06-13 15:18:00 +00:00
},
2023-06-19 11:41:56 +00:00
"2": {
"bounds": [435, 689],
"counts": {
"token": 389,
"ent_type": 198,
"s": 145
},
"freqs": {
"word": {
"1": 60,
"2": 70,
"3": 100
},
"lemma": {
"1": 654,
"2": 321,
"3": 987
},
"pos": {
"1": 543,
"2": 876,
"3": 234
},
"simple_pos": {
"1": 987,
"2": 654,
"3": 321
},
"ent_type": {
"1": 234,
"2": 789,
"3": 543
}
},
"values": {
"author": 2,
"publishing_year":1951,
"title": 2
}
}
},
"s": {
"1": {
"bounds": [345, 678]
}
},
"ent": {
"1": {
"bounds": [567, 890],
"values": {
"type": 789
}
}
},
"token": {
"310": {
"values": {
"word": 1,
"lemma": 2,
"pos": 1,
"simple_pos": 1
}
}
},
"value_lookups": {
"text": {
"author": {
"1": "John Doe",
"2": "Jane Smith"
},
"title": {
"1": "Test Title 1",
"2": "Test Title 2"
}
2023-06-13 15:18:00 +00:00
},
2023-06-19 11:41:56 +00:00
"ent": {
"type": {
"1": "Person",
"2": "Organization"
}
2023-06-13 15:18:00 +00:00
},
2023-06-19 11:41:56 +00:00
"token": {
"word": {
"1": "apple",
"2": "banana",
"3": "orange"
},
"lemma": {
"1": "run",
"2": "walk",
"3": "jump"
},
"pos": {
"1": "noun",
"2": "verb",
"3": "adjective"
},
"simple_pos": {
"1": "subject",
"2": "object",
"3": "predicate"
}
2023-06-13 13:41:34 +00:00
}
2023-06-19 11:41:56 +00:00
}
}
resolve(dummyData);
/*
const args = {corpus_name: this.name};
this.socket.emit('cqi.corpora.corpus.getCorpusData', args, response => {
if (response.code === 200) {
resolve(response.payload);
} else {
reject(response);
}
});
*/
})
}
2021-11-16 14:23:57 +00:00
drop() {
return new Promise((resolve, reject) => {
2021-12-01 15:03:55 +00:00
const args = {corpus_name: this.name};
2021-11-16 14:23:57 +00:00
this.socket.emit('cqi.corpora.corpus.drop', args, response => {
if (response.code === 200) {
resolve(response.payload);
} else {
reject(response);
}
});
});
}
query(subcorpus_name, queryString) {
return new Promise((resolve, reject) => {
2021-12-01 15:03:55 +00:00
const args = {
2021-11-16 14:23:57 +00:00
corpus_name: this.name,
subcorpus_name: subcorpus_name,
query: queryString
};
2021-12-01 15:03:55 +00:00
2021-11-16 14:23:57 +00:00
this.socket.emit('cqi.corpora.corpus.query', args, response => {
if (response.code === 200) {
resolve(response.payload);
} else {
reject(response);
}
});
});
}
// nopaque specific CQi extension
paginate(page=1, perPage=20) {
return new Promise((resolve, reject) => {
2021-12-01 15:03:55 +00:00
const args = {corpus_name: this.name, page: page, per_page: perPage};
2021-11-16 14:23:57 +00:00
this.socket.emit('cqi.corpora.corpus.paginate', args, response => {
if (response.code === 200) {
resolve(response.payload);
} else {
reject(response);
}
});
});
}
updateDb() {
2021-12-01 15:03:55 +00:00
const args = {corpus_name: this.name};
2021-11-16 14:23:57 +00:00
this.socket.emit('cqi.corpora.corpus.update_db', args);
}
}
class CQiAlignmentAttributeCollection {
constructor(socket, corpus) {
this.corpus = corpus;
this.socket = socket;
}
get(alignmentAttributeName) {
return new Promise((resolve, reject) => {
2021-12-01 15:03:55 +00:00
const args = {
corpus_name: this.corpus.name,
alignment_attribute_name: alignmentAttributeName
};
2021-11-16 14:23:57 +00:00
this.socket.emit('cqi.corpora.corpus.alignment_attributes.get', args, response => {
if (response.code === 200) {
resolve(new CQiAlignmentAttribute(this.socket, this.corpus, response.payload));
} else {
reject(response);
}
});
});
}
list() {
return new Promise((resolve, reject) => {
2021-12-01 15:03:55 +00:00
const args = {corpus_name: this.corpus.name};
2021-11-16 14:23:57 +00:00
this.socket.emit('cqi.corpus.alignment_attributes.list', args, response => {
if (response.code === 200) {
resolve(response.payload.map(x => {return new CQiAlignmentAttribute(this.socket, this.corpus, x);}));
} else {
reject(response);
}
});
});
}
}
class CQiAlignmentAttribute {
constructor(socket, corpus, attrs) {
this.socket = socket;
this.corpus = corpus;
this.name = attrs.name;
this.size = attrs.size;
}
}
class CQiPositionalAttributeCollection {
constructor(socket, corpus) {
this.corpus = corpus;
this.socket = socket;
}
get(positionalAttributeName) {
return new Promise((resolve, reject) => {
2021-12-01 15:03:55 +00:00
const args = {
2021-11-16 14:23:57 +00:00
corpus_name: this.corpus.name,
positional_attribute_name: positionalAttributeName
};
2021-12-01 15:03:55 +00:00
2021-11-16 14:23:57 +00:00
this.socket.emit('cqi.corpora.corpus.positional_attributes.get', args, response => {
if (response.code === 200) {
resolve(new CQiPositionalAttribute(this.socket, this.corpus, response.payload));
} else {
reject(response);
}
});
});
}
list() {
return new Promise((resolve, reject) => {
2021-12-01 15:03:55 +00:00
const args = {corpus_name: this.corpus.name};
2021-11-16 14:23:57 +00:00
this.socket.emit('cqi.corpus.positional_attributes.list', args, response => {
if (response.code === 200) {
resolve(response.payload.map(x => {return new CQiPositionalAttribute(this.socket, this.corpus, x);}));
} else {
reject(response);
}
});
});
}
}
class CQiPositionalAttribute {
constructor(socket, corpus, attrs) {
this.socket = socket;
this.corpus = corpus;
this.lexiconSize = attrs.lexicon_size;
this.name = attrs.name;
this.size = attrs.size;
}
}
class CQiStructuralAttributeCollection {
constructor(socket, corpus) {
this.corpus = corpus;
this.socket = socket;
}
get(structuralAttributeName) {
return new Promise((resolve, reject) => {
2021-12-01 15:03:55 +00:00
const args = {
2021-11-16 14:23:57 +00:00
corpus_name: this.corpus.name,
structural_attribute_name: structuralAttributeName
};
2021-12-01 15:03:55 +00:00
2021-11-16 14:23:57 +00:00
this.socket.emit('cqi.corpora.corpus.structural_attributes.get', args, response => {
if (response.code === 200) {
resolve(new CQiStructuralAttribute(this.socket, this.corpus, response.payload));
} else {
reject(response);
}
});
});
}
list() {
return new Promise((resolve, reject) => {
2021-12-01 15:03:55 +00:00
const args = {corpus_name: this.corpus.name};
2021-11-16 14:23:57 +00:00
this.socket.emit('cqi.corpus.structural_attributes.list', args, response => {
if (response.code === 200) {
resolve(response.payload.map(x => {return new CQiStructuralAttribute(this.socket, this.corpus, x);}));
} else {
reject(response);
}
});
});
}
}
class CQiStructuralAttribute {
constructor(socket, corpus, attrs) {
this.socket = socket;
this.corpus = corpus;
this.hasValues = attrs.has_values;
this.name = attrs.name;
this.size = attrs.size;
}
}
class CQiSubcorpusCollection {
constructor(socket, corpus) {
this.corpus = corpus;
this.socket = socket;
}
get(subcorpusName) {
return new Promise((resolve, reject) => {
2021-12-01 15:03:55 +00:00
const args = {
corpus_name: this.corpus.name,
subcorpus_name: subcorpusName
};
2021-11-16 14:23:57 +00:00
this.socket.emit('cqi.corpora.corpus.subcorpora.get', args, response => {
if (response.code === 200) {
resolve(new CQiSubcorpus(this.socket, this.corpus, response.payload));
} else {
reject(response);
}
});
});
}
list() {
return new Promise((resolve, reject) => {
2021-12-01 15:03:55 +00:00
const args = {corpus_name: this.corpus.name};
2021-11-16 14:23:57 +00:00
this.socket.emit('cqi.corpora.corpus.subcorpora.list', args, response => {
if (response.code === 200) {
resolve(response.payload.map(x => {return new CQiSubcorpus(this.socket, this.corpus, x);}));
} else {
reject(response);
}
});
});
}
}
class CQiSubcorpus {
constructor(socket, corpus, attrs) {
this.socket = socket;
this.corpus = corpus;
this.fields = attrs.fields;
this.name = attrs.name;
this.size = attrs.size;
}
drop() {
return new Promise((resolve, reject) => {
2021-12-01 15:03:55 +00:00
const args = {corpus_name: this.corpus.name, subcorpus_name: this.name};
2021-11-16 14:23:57 +00:00
this.socket.emit('cqi.corpora.corpus.subcorpora.subcorpus.drop', args, response => {
if (response.code === 200) {
resolve(response.payload);
} else {
reject(response);
}
});
});
}
dump(field, first, last) {
return new Promise((resolve, reject) => {
2021-12-01 15:03:55 +00:00
const args = {
2021-11-16 14:23:57 +00:00
corpus_name: this.corpus.name,
subcorpus_name: this.name,
field: field,
first: first,
last: last
};
2021-12-01 15:03:55 +00:00
2021-11-16 14:23:57 +00:00
this.socket.emit('cqi.corpora.corpus.subcorpora.subcorpus.dump', args, response => {
if (response.code === 200) {
resolve(response.payload);
} else {
reject(response);
}
});
});
}
export(context=50) {
return new Promise((resolve, reject) => {
2021-12-01 15:03:55 +00:00
const args = {
2021-11-16 14:23:57 +00:00
corpus_name: this.corpus.name,
subcorpus_name: this.name,
context: context
};
2021-12-01 15:03:55 +00:00
2021-11-16 14:23:57 +00:00
this.socket.emit('cqi.corpora.corpus.subcorpora.subcorpus.export', args, response => {
if (response.code === 200) {
resolve(response.payload);
} else {
reject(response);
}
});
});
}
2023-01-19 13:59:09 +00:00
partial_export(matchIdList, context=50) {
return new Promise((resolve, reject) => {
const args = {
corpus_name: this.corpus.name,
subcorpus_name: this.name,
match_id_list: matchIdList,
context: context
};
this.socket.emit('cqi.corpora.corpus.subcorpora.subcorpus.partial_export', args, response => {
if (response.code === 200) {
resolve(response.payload);
} else {
reject(response);
}
});
});
}
2021-11-16 14:23:57 +00:00
fdst_1(cutoff, field, attribute) {
return new Promise((resolve, reject) => {
2021-12-01 15:03:55 +00:00
const args = {
2021-11-16 14:23:57 +00:00
corpus_name: this.corpus.name,
subcorpus_name: this.name,
cutoff: cutoff,
field: field,
attribute: attribute
};
2021-12-01 15:03:55 +00:00
2021-11-16 14:23:57 +00:00
this.socket.emit('cqi.corpora.corpus.subcorpora.subcorpus.fdist_1', args, response => {
if (response.code === 200) {
resolve(response.payload);
} else {
reject(response);
}
});
});
}
fdst_2(cutoff, field1, attribute1, field2, attribute2) {
return new Promise((resolve, reject) => {
2021-12-01 15:03:55 +00:00
const args = {
2021-11-16 14:23:57 +00:00
corpus_name: this.corpus.name,
subcorpus_name: this.name,
cutoff: cutoff,
field1: field1,
attribute1: attribute1,
field2: field2,
attribute2: attribute2
};
2021-12-01 15:03:55 +00:00
2021-11-16 14:23:57 +00:00
this.socket.emit('cqi.corpora.corpus.subcorpora.subcorpus.fdist_1', args, response => {
if (response.code === 200) {
resolve(response.payload);
} else {
reject(response);
}
});
});
}
// nopaque specific CQi extension
paginate(page=1, perPage=20, context=50) {
return new Promise((resolve, reject) => {
2021-12-01 15:03:55 +00:00
const args = {
2021-11-16 14:23:57 +00:00
corpus_name: this.corpus.name,
subcorpus_name: this.name,
page: page,
per_page: perPage,
context: context
};
2021-12-01 15:03:55 +00:00
2021-11-16 14:23:57 +00:00
this.socket.emit('cqi.corpora.corpus.subcorpora.subcorpus.paginate', args, response => {
if (response.code === 200) {
resolve(response.payload);
} else {
reject(response);
}
});
});
}
}