Skip to content

Commit

Permalink
fix(annotation): converters doesn't read segment labels correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
julianpoemp committed Sep 19, 2023
1 parent 3535df3 commit 5d8ce29
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 23 deletions.
2 changes: 1 addition & 1 deletion libs/annotation/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@octra/annotation",
"version": "0.1.3",
"version": "0.1.4",
"type": "module",
"peerDependencies": {
"x2js": "^3.4.4"
Expand Down
1 change: 0 additions & 1 deletion libs/annotation/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"targets": {
"build": {
"executor": "@nrwl/rollup:rollup",
"dependsOn": ["media"],
"outputs": [
"{options.outputPath}"
],
Expand Down
16 changes: 12 additions & 4 deletions libs/annotation/src/lib/annotjson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class OAnnotJSON
}

static deserialize(jsonObject: IAnnotJSON): OAnnotJSON | undefined {
if(jsonObject) {
if (jsonObject) {
return new OAnnotJSON(
jsonObject.annotates,
jsonObject.name,
Expand All @@ -135,7 +135,7 @@ export class OAnnotJSON
jsonObject.links
);
}
return undefined
return undefined;
}

deserialize(jsonObject: IAnnotJSON): OAnnotJSON | undefined {
Expand All @@ -155,7 +155,11 @@ export class OLevel<T extends OItem> implements ILevel {
}

clone() {
return new OLevel(this.name, this.type, this.items.map(a => a.clone()));
return new OLevel(
this.name,
this.type,
this.items.map((a) => a.clone())
);
}
}

Expand Down Expand Up @@ -280,6 +284,10 @@ export class OItem implements IItem, Serializable<IItem, OItem> {
return new OItem(jsonObject.id, jsonObject.labels);
}

getFirstLabelWithoutName(notName: string) {
return this.labels?.find((a) => a.name !== notName);
}

clone(id?: number) {
return new OItem(id ?? this.id, [...this.labels]);
}
Expand Down Expand Up @@ -413,7 +421,7 @@ export class OLink implements ILink, Serializable<ILink, OLink> {
return new OLink(jsonObject.fromID, jsonObject.toID);
}

clone(){
clone() {
return new OLink(this.fromID, this.toID);
}
}
Expand Down
3 changes: 2 additions & 1 deletion libs/annotation/src/lib/converters/CTMConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ export class CTMConverter extends Converter {
const level = annotation.levels[levelnum];

for (const levelItem of level.items as OSegment[]) {
const transcript = levelItem.labels[0].value;
const transcript =
levelItem.getFirstLabelWithoutName('Speaker')?.value ?? '';
const start =
Math.round((levelItem.sampleStart! / audiofile.sampleRate) * 100) / 100;
const duration =
Expand Down
2 changes: 1 addition & 1 deletion libs/annotation/src/lib/converters/ELANConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export class ELANConverter extends Converter {
ALIGNABLE_ANNOTATION: {
_ANNOTATION_ID: `a${aidCounter}`,
ANNOTATION_VALUE:
segment.labels.find((a) => a.name !== 'Speaker')?.value ?? '',
segment.getFirstLabelWithoutName('Speaker')?.value ?? '',
_TIME_SLOT_REF1: `ts${tsidCounter - 1}`,
_TIME_SLOT_REF2: `ts${tsidCounter}`,
},
Expand Down
8 changes: 4 additions & 4 deletions libs/annotation/src/lib/converters/PartiturConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ LBD:\n`;
let ortCounter = 0;

for (const item of annotation.levels[levelnum].items as OSegment[]) {
const words = item.labels
.filter((a) => a.name !== 'Speaker')[0]
.value.split(' ');
const words = (
item.getFirstLabelWithoutName('Speaker')?.value ?? ''
).split(' ');
ort = ort.concat(words);
let trnLine = `TRN: ${item.sampleStart} ${item.sampleDur} `;

Expand All @@ -84,7 +84,7 @@ LBD:\n`;
}
ortCounter += words.length;
trnLine += ` ${
item.labels.filter((a) => a.name !== 'Speaker')[0].value
item.getFirstLabelWithoutName('Speaker')?.value ?? ''
}\n`;
trn.push(trnLine);
}
Expand Down
12 changes: 5 additions & 7 deletions libs/annotation/src/lib/converters/PraatTableConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
OctraAnnotationFormatType,
} from './Converter';
import {
ISegment,
OAnnotJSON,
OAnyLevel,
OLabel,
Expand Down Expand Up @@ -47,14 +46,13 @@ export class PraatTableConverter extends Converter {
const addEntry = (
res: string,
level: OAnyLevel<OSegment>,
segment: ISegment
segment: OSegment
) => {
const tmin = segment.sampleStart / annotation.sampleRate;
const tmax =
(segment.sampleStart + segment.sampleDur) / annotation.sampleRate;
const transcript = segment.labels.find(
(a) => a.name !== 'Speaker'
)?.value;
const transcript =
segment.getFirstLabelWithoutName('Speaker')?.value ?? '';

return `${res}${tmin}\t${level.name}\t${transcript}\t${tmax}\n`;
};
Expand All @@ -64,8 +62,8 @@ export class PraatTableConverter extends Converter {
for (const level of annotation.levels) {
// export segments only
if (level.type === 'SEGMENT') {
for (const segment of level.items) {
result = addEntry(result, level, segment as ISegment);
for (const segment of level.items as OSegment[]) {
result = addEntry(result, level, segment);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class PraatTextgridConverter extends Converter {
` xmin = ${secondsStart} \n` +
` xmax = ${secondsEnd} \n` +
` text = "${
segment.labels.find((a) => a.name !== 'Speaker')?.value
segment.getFirstLabelWithoutName('Speaker')?.value ?? ''
}" \n`;
}
}
Expand Down
3 changes: 2 additions & 1 deletion libs/annotation/src/lib/converters/SRTConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ export class SRTConverter extends Converter {
let counter = 1;
if (level.type === 'SEGMENT') {
for (const item of level.items as OSegment[]) {
const transcript = item.labels[0].value;
const transcript =
item.getFirstLabelWithoutName('Speaker')?.value ?? '';
const start = this.getTimeStringFromSamples(
item.sampleStart,
annotation.sampleRate
Expand Down
3 changes: 2 additions & 1 deletion libs/annotation/src/lib/converters/TextConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ export class TextConverter extends Converter {
if (level.type === 'SEGMENT') {
for (let j = 0; j < level.items.length; j++) {
const item = level.items[j] as OSegment;
const transcript = item.labels[0].value;
const transcript =
item.getFirstLabelWithoutName('Speaker')?.value ?? '';

result += transcript;
if (j < level.items.length - 1) {
Expand Down
4 changes: 3 additions & 1 deletion libs/annotation/src/lib/converters/WebVTTConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ export class WebVTTConverter extends Converter {
if (level.type === 'SEGMENT') {
for (let j = 0; j < level.items.length; j++) {
const item = level.items[j] as OSegment;
const transcript = item.labels[0].value
const transcript = (
item.getFirstLabelWithoutName('Speaker')?.value ?? ''
)
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;');
const start = this.getTimeStringFromSamples(
Expand Down
4 changes: 4 additions & 0 deletions libs/annotation/src/lib/octraAnnotationSegment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export class OctraAnnotationEvent
...this.labels,
]);
}

getFirstLabelWithoutName(notName: string) {
return this.labels?.find((a) => a.name !== notName);
}
}

export class OctraAnnotationSegment<T extends ASRContext = ASRContext>
Expand Down

0 comments on commit 5d8ce29

Please sign in to comment.