Skip to content

Commit

Permalink
Return added Communication-level references for TS users. (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
ccmaymay authored Oct 17, 2023
1 parent 8016487 commit d6919a0
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 8 deletions.
14 changes: 11 additions & 3 deletions src_nodejs/communication_fu.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import './communication_types';
import {UUID} from './uuid_types';
import {Entity, EntityMentionSet} from './entities_types';
import {Entity, EntityMention, EntityMentionSet} from './entities_types';
import {Section, Sentence, Tokenization, Token} from './structure_types';
import {SituationMention} from './situations_types';
import {Situation, SituationMention} from './situations_types';

declare module "./communication_types" {
interface Communication {
addInternalReferences(): void;
addInternalReferences(): {
entityForUUID: {[index: string]: Entity},
entityMentionForUUID: {[index: string]: EntityMention},
sectionForUUID: {[index: string]: Section},
sentenceForUUID: {[index: string]: Sentence},
situationForUUID: {[index: string]: Situation},
situationMentionForUUID: {[index: string]: SituationMention},
tokenizationForUUID: {[index: string]: Tokenization},
};
getEntityForEntityMentionUUID(uuid: UUID): Entity?;
getEntityMentionSetWithToolname(toolname: string): EntityMentionSet;
getEntityMentionWithUUID(uuid: UUID): EntityMention?;
Expand Down
24 changes: 19 additions & 5 deletions src_nodejs/communication_fu.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const {Communication} = require('./communication_types');
const {Thrift} = require('thrift');
const {TBufferedTransport, TJSONProtocol} = require('thrift');

/**
* Adds internal references between data structures contained in Communication.
Expand Down Expand Up @@ -42,6 +42,10 @@ const {Thrift} = require('thrift');
* - tokenization: reference to the Tokenization corresponding to tokenizationId
*
* @function concrete.communication.Communication.prototype.addInternalReferences
* @returns {object} Object with references to Communication-level maps (entityForUUID, entityMentionForUUID,
* sectionForUUID, sentenceForUUID, situationForUUID, situationMentionForUUID, tokenizationForUUID),
* mainly intended for TypeScript consumers. JavaScript consumers can access these maps directly
* on the Communication.
*/
Communication.prototype.addInternalReferences = function() {
this.entityForUUID = {};
Expand Down Expand Up @@ -144,6 +148,16 @@ Communication.prototype.addInternalReferences = function() {
situation.situationSet = situationSet;
});
});

return {
entityForUUID: this.entityForUUID,
entityMentionForUUID: this.entityMentionForUUID,
sectionForUUID: this.sectionForUUID,
sentenceForUUID: this.sentenceForUUID,
situationForUUID: this.situationForUUID,
situationMentionForUUID: this.situationMentionForUUID,
tokenizationForUUID: this.tokenizationForUUID,
};
};


Expand Down Expand Up @@ -488,8 +502,8 @@ Communication.prototype.initFromTJSONProtocolObject = function(commJSONObject) {
*/
Communication.prototype.initFromTJSONProtocolString = function(commJSONString) {
var commJSONObject = JSON.parse(commJSONString);
var transport = new Thrift.Transport();
var protocol = new Thrift.TJSONProtocol(transport);
var transport = new TBufferedTransport();
var protocol = new TJSONProtocol(transport);

// The values for these protocol object fields was determined by
// mucking around with the JavaScript debugger to figure out how
Expand Down Expand Up @@ -524,8 +538,8 @@ Communication.prototype.toTJSONProtocolObject = function() {
* @returns {string}
*/
Communication.prototype.toTJSONProtocolString = function() {
var transport = new Thrift.Transport();
var protocol = new Thrift.TJSONProtocol(transport);
var transport = new TBufferedTransport();
var protocol = new TJSONProtocol(transport);
protocol.tpos = [];
protocol.tstack = [];
this.write(protocol);
Expand Down
48 changes: 48 additions & 0 deletions test_nodejs/communication_fu_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const expect = require("chai").expect;

const concrete = require("../dist_nodejs");
const commJSONData = require('../test/fixtures/dog-bites-man.concrete.json');

describe("addInternalReferences", function() {
it("Returns Communication-level maps", function() {
const comm = new concrete.communication.Communication();
comm.initFromTJSONProtocolObject(commJSONData);
const refs = comm.addInternalReferences();
expect(refs).to.have.all.keys([
"entityForUUID",
"entityMentionForUUID",
"sectionForUUID",
"sentenceForUUID",
"situationForUUID",
"situationMentionForUUID",
"tokenizationForUUID"
]);
comm.sectionList.forEach((section) => {
expect(refs.sectionForUUID[section.uuid.uuidString]).to.equal(section);
section.sentenceList.forEach((sentence) => {
expect(refs.sentenceForUUID[sentence.uuid.uuidString]).to.equal(sentence);
expect(refs.tokenizationForUUID[sentence.tokenization.uuid.uuidString]).to.equal(sentence.tokenization);
});
});
comm.entitySetList.forEach((entitySet) =>
entitySet.entityList.forEach((entity) =>
expect(refs.entityForUUID[entity.uuid.uuidString]).to.equal(entity)
)
);
comm.entityMentionSetList.forEach((entityMentionSet) =>
entityMentionSet.mentionList.forEach((mention) =>
expect(refs.entityMentionForUUID[mention.uuid.uuidString]).to.equal(mention)
)
);
comm.situationSetList.forEach((situationSet) =>
situationSet.situationList.forEach((situation) =>
expect(refs.situationForUUID[situation.uuid.uuidString]).to.equal(situation)
)
);
comm.situationMentionSetList.forEach((situationMentionSet) =>
situationMentionSet.mentionList.forEach((mention) =>
expect(refs.situationMentionForUUID[mention.uuid.uuidString]).to.equal(mention)
)
);
});
});

0 comments on commit d6919a0

Please sign in to comment.