Skip to content

Commit

Permalink
fixing to work for multiple sections
Browse files Browse the repository at this point in the history
  • Loading branch information
wildercb committed Jun 8, 2024
1 parent cb915a9 commit 426e6d8
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 55 deletions.
91 changes: 62 additions & 29 deletions src/components/ReviewPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,33 @@ export default {
console.error('Missing details for reverting block removal');
}
},
// Inside AnnotationPage.vue
// Original Inside AnnotationPage.vue
tokenizeCurrentSentence() {
this.currentSentence = this.inputSentences[this.currentIndex];
this.currentAnnotation = this.annotations[this.currentIndex];
let tokens, spans;
if (this.$store.state.annotationPrecision == "char") {
tokens = this.currentSentence.text.split('');
spans = [];
for (let i = 0; i < this.currentSentence.text.length; i++) {
spans.push([i, i + 1]);
}
} else {
tokens = this.tokenizer.tokenize(this.currentSentence.text);
spans = this.tokenizer.span_tokenize(this.currentSentence.text);
}
let combined = tokens.map((t, i) => [spans[i][0], spans[i][1], t]);
this.tm = new TokenManager(this.classes);
this.tm.setTokensAndAnnotation(combined, this.currentAnnotation);
// Call applyAnnotationHistory after setting up tokens and annotations
this.applyAnnotationHistory();
},
/*
applyAnnotationHistory() {
const annotationHistory = this.annotationHistory;
if (annotationHistory && annotationHistory.length > 0) {
Expand Down Expand Up @@ -357,39 +383,46 @@ export default {
}
});
}},
determineSymbolState(status) {
switch (status) {
case "Accepted": return 1;
case "Rejected": return 2;
case "Suggested": return 0;
default: return 0; // Default to suggested if unrecognized status
}
},
tokenizeCurrentSentence() {
this.currentSentence = this.inputSentences[this.currentIndex];
this.currentAnnotation = this.annotations[this.currentIndex];
let tokens, spans;
this.currentSentence = this.inputSentences[this.currentIndex];
this.currentAnnotation = this.annotations[this.currentIndex];
this.applyAnnotationHistory();
},*/
applyAnnotationHistory() {
const annotationHistory = this.annotationHistory[this.currentIndex];
if (annotationHistory && annotationHistory.length > 0) {
annotationHistory.forEach((annotation) => {
const [labelName, start, end, , name, status, ogNLP, types] = annotation;
const _class = this.classes.find(cls => cls.name === labelName); // Match class by label name
const isSymbolActive = this.determineSymbolState(status);
if (this.$store.state.annotationPrecision == "char") {
tokens = this.currentSentence.text.split('');
spans = [];
for (let i = 0; i < this.currentSentence.text.length; i++) {
spans.push([i, i + 1]);
if (_class) {
this.tm.addNewBlock(start, end, _class, true, ogNLP, true, name, status, types, false, isSymbolActive);
} else {
console.warn(`Label "${labelName}" not found in classes.`);
}
} else {
tokens = this.tokenizer.tokenize(this.currentSentence.text);
spans = this.tokenizer.span_tokenize(this.currentSentence.text);
}
});
let combined = tokens.map((t, i) => [spans[i][0], spans[i][1], t]);
this.tm.tokens.forEach(token => {
if (token.type === "token-block") {
const isNLP = annotationHistory.some(annotation => {
const [, start, end, , name] = annotation;
return name === "nlp" && token.start === start && token.end === end;
});
token.humanOpinion = !isNLP;
}
});
}
},
this.tm = new TokenManager(this.classes);
this.tm.setTokensAndAnnotation(combined, this.currentAnnotation);
// Call applyAnnotationHistory after setting up tokens and annotations
this.applyAnnotationHistory();
},
determineSymbolState(status) {
switch (status) {
case "Accepted": return 1;
case "Rejected": return 2;
case "Suggested": return 0;
default: return 0; // Default to suggested if unrecognized status
}
},
selectTokens() {
let selection = document.getSelection();
Expand Down
116 changes: 90 additions & 26 deletions src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export const mutations = {
state.currentClass = state.classes[0];
LocalStorage.set("tags", state.classes);
},

/*
setInputSentences(state, payload) {
try {
let jsonData;
Expand Down Expand Up @@ -115,16 +115,17 @@ export const mutations = {
console.error(`Error processing payload: ${error.message}`);
}
function processJsonData(jsonData) {
const defaultEntity = [0, 0, ["default"]]; // Define the default entity
const defaultEntity = [[
0,
0,
[["Created", "2023-11-05", "SEWDO", "default"]]]]; // Define the default entity
/*
Function to process data in input entities section and map to token metadata
Currenlty set to send the last annotation in the list of annotation history
as the information which gets loaded into review page on enter
*/
*/ /*
const processedTexts = jsonData.annotations.map(([annotationText, annotationEntities], i) => {
let annotationHistory = [];

// Ensure annotationEntities and entities are properly initialized
if (!annotationEntities || !Array.isArray(annotationEntities.entities)) {
annotationEntities = { entities: [] };
}
Expand All @@ -133,26 +134,6 @@ export const mutations = {
if (annotationEntities.entities.length === 0) {
annotationEntities.entities.push(defaultEntity);
} else {
// Set the current class for the preceding two indices of each entity
/* THIS IS FOR THEIR OLD FILE STRUCTURE
if (annotationClassIds.length > 0) {
annotationEntities.entities.forEach(entity => {
if (entity.length >= 3) {
const start = entity[0];
const end = entity[1];
// type = the block of information that contains the name, date label etc..
const type = entity[3];
const label = entity[2];
const name = type[0][3];
const status = type[0][4];
console.log("label: ",label, "start: ",start, "end: ",end, "type: ",type, "name: ", name, "status: ", status);
annotationHistory.push([label, start, end, type, name, status]);
const textSnippet = annotationText.slice(start, end);
const textIndices = [start - 1, start - 2]; // Adjust indices as needed
console.log("THIS CONSOLE.LOG", sstate, label, textSnippet, textIndices);
}
}); */
annotationEntities.entities.forEach(entity => {
if (entity.length >= 3) {
Expand Down Expand Up @@ -191,8 +172,91 @@ export const mutations = {
mutations.loadClasses(state, jsonData.classes);
}
}
},
}, */
setInputSentences(state, payload) {
try {
let jsonData;
if (typeof payload === 'string') {
try {
jsonData = JSON.parse(payload);
} catch (jsonError) {
jsonData = {
annotations: [[payload, { entities: [] }]],
classes: []
};
}
} else if (payload instanceof File) {
const fileReader = new FileReader();
fileReader.onload = function (event) {
try {
const fileContent = event.target.result;
jsonData = {
annotations: [[fileContent, { entities: [] }]],
classes: []
};

processJsonData(jsonData);
} catch (error) {
console.error(`Error processing text file: ${error.message}`);
}
};

fileReader.readAsText(payload);
return;
} else {
throw new Error("Invalid payload type");
}

processJsonData(jsonData);
} catch (error) {
console.error(`Error processing payload: ${error.message}`);
}

function processJsonData(jsonData) {
const defaultEntity = [
[0, 0, [["Created", "2023-11-05", "SEWDO", "default"]]]
]; // Define the default entity

const processedTexts = jsonData.annotations.map(([annotationText, annotationEntities], i) => {
let annotationHistory = [];
if (!annotationEntities || !Array.isArray(annotationEntities.entities)) {
annotationEntities = { entities: [] };
}

if (annotationEntities.entities.length === 0) {
annotationEntities.entities.push(defaultEntity);
} else {
annotationEntities.entities.forEach(entity => {
if (entity.length >= 3) {
const start = entity[0];
const end = entity[1];
const types = entity[2]; // This will store all blocks
const type = types[types.length - 1]; // This assigns only the last block to 'type'
const label = type[3]; // Extract just the label name for matching
const name = type[2];
const status = type[0];
const ogNLP = types.some(t => t[2] === 'nlp'); // Check if any type was NLP

annotationHistory.push([label, start, end, type, name, status, ogNLP, types]);
console.log("Loaded annotation history:", types);
}
});
}

// Assign annotation history for the current section without overriding others
state.annotationHistory[i] = annotationHistory.length ? annotationHistory : [defaultEntity];

return { id: i, text: annotationText, entities: annotationEntities.entities };
});

state.inputSentences = processedTexts;
state.originalText = processedTexts.map(item => item.text).join(state.separator);

if (jsonData.classes && Array.isArray(jsonData.classes)) {
mutations.loadClasses(state, jsonData.classes);
}
}
},

addClass(state, payload) {
// Check if the class already exists
Expand Down

0 comments on commit 426e6d8

Please sign in to comment.