-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtextgrid-spec.js
More file actions
185 lines (156 loc) · 7.76 KB
/
textgrid-spec.js
File metadata and controls
185 lines (156 loc) · 7.76 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
'use strict';
var textgrid = require("../lib/textgrid.js").TextGrid;
var fs = require("fs");
/*
======== A Handy Little Jasmine Reference ========
https://github.com/pivotal/jasmine/wiki/Matchers
Spec matchers:
expect(x).toEqual(y); compares objects or primitives x and y and passes if they are equivalent
expect(x).toBe(y); compares objects or primitives x and y and passes if they are the same object
expect(x).toMatch(pattern); compares x to string or regular expression pattern and passes if they match
expect(x).toBeDefined(); passes if x is not undefined
expect(x).toBeUndefined(); passes if x is undefined
expect(x).toBeNull(); passes if x is null
expect(x).toBeTruthy(); passes if x evaluates to true
expect(x).toBeFalsy(); passes if x evaluates to false
expect(x).toContain(y); passes if array or string x contains y
expect(x).toBeLessThan(y); passes if x is less than y
expect(x).toBeGreaterThan(y); passes if x is greater than y
expect(function(){fn();}).toThrow(e); passes if function fn throws exception e when executed
Every matcher"s criteria can be inverted by prepending .not:
expect(x).not.toEqual(y); compares objects or primitives x and y and passes if they are not equivalent
Custom matchers help to document the intent of your specs, and can help to remove code duplication in your specs.
beforeEach(function() {
this.addMatchers({
toBeLessThan: function(expected) {
var actual = this.actual;
var notText = this.isNot ? " not" : ";
this.message = function () {
return "Expected " + actual + notText + " to be less than " + expected;
}
return actual < expected;
}
});
});
*/
describe("lib/textgrid", function() {
it("should init", function() {
expect(textgrid.init).toBeDefined();
});
it("should read sample data", function() {
var sampleUtterancesTextGrid = fs.readFileSync(__dirname + "/../data/sampleUtterances.TextGrid", {
encoding: "UTF-8"
});
expect(sampleUtterancesTextGrid).toBeDefined();
});
it("should convert sampleUtterancesTextGrid textgrid into json", function() {
var sampleUtterancesTextGrid = fs.readFileSync(__dirname + "/../data/sampleUtterances.TextGrid", {
encoding: "UTF-8"
});
var json = textgrid.textgridToJSON(sampleUtterancesTextGrid);
// console.log(JSON.stringify(json, null, 2));
expect(typeof json).toBe("object");
for (var itemIndex = 0; itemIndex < json.items.length; itemIndex++) {
console.log("Sanity check for tier " + itemIndex);
if (json.items[itemIndex].points) {
expect(json.items[itemIndex].points.length).toEqual(json.items[itemIndex].points_size);
}
if (json.items[itemIndex].intervals) {
expect(json.items[itemIndex].intervals.length).toEqual(json.items[itemIndex].intervals_size);
}
}
});
it("should discover IGT relationships between aligned tiers", function() {
var sampleUtterancesTextGrid = fs.readFileSync(__dirname + "/../data/sample_elan.TextGrid", {
encoding: "UTF-8"
});
var json = textgrid.textgridToIGT(sampleUtterancesTextGrid);
expect(typeof json).toBe("object");
// console.log(JSON.stringify(json.intervalsByXmin, null, 2));
expect(typeof json.intervalsByXmin).toBe("object");
expect(typeof json.intervalsByText).toBe("object");
expect(json.isIGTNestedOrAlignedOrBySpeaker.probablyAligned).toBeTruthy();
});
it("should discover speakers in Elan exported tiers", function() {
var sampleUtterancesTextGrid = fs.readFileSync(__dirname + "/../data/sample_elan.TextGrid", {
encoding: "UTF-8"
});
var json = textgrid.textgridToIGT(sampleUtterancesTextGrid);
// console.log(JSON.stringify(json, null, 2));
expect(json.isIGTNestedOrAlignedOrBySpeaker.probablyBySpeaker).toBeTruthy();
});
it("should discover be possible to get start and end times for segments in", function() {
var sampleUtterancesTextGrid = fs.readFileSync(__dirname + "/../data/sampleNested.TextGrid", {
encoding: "UTF-8"
});
var json = textgrid.textgridToIGT(sampleUtterancesTextGrid);
// console.log(JSON.stringify(json, null, 2));
expect(typeof json).toBe("object");
console.log(JSON.stringify(json.isIGTNestedOrAlignedOrBySpeaker, null, 2));
});
it("should discover not IGT relationships between speaker tiers", function() {
var sampleMultipleSpeakersTiers = fs.readFileSync(__dirname + "/../data/sampleMultipleSpeakers.TextGrid", {
encoding: "UTF-8"
});
var json = textgrid.textgridToIGT(sampleMultipleSpeakersTiers, "speakersaretiernames");
// console.log(JSON.stringify(json, null, 2));
expect(typeof json).toBe("object");
console.log(json.items.length);
console.log(JSON.stringify(json.isIGTNestedOrAlignedOrBySpeaker, null, 2));
expect(json.isIGTNestedOrAlignedOrBySpeaker.probablyAligned).toBeFalsy();
expect(json.isIGTNestedOrAlignedOrBySpeaker.probablyBySpeaker).toBeTruthy();
});
it("should accept multiple TextGrids from different files with optionally inserted file names", function() {
var sampleMultipleTextGrids = "\n\nFile name = sampleMultipleSpeakers\n" + fs.readFileSync(__dirname + "/../data/sampleMultipleSpeakers.TextGrid", {
encoding: "UTF-8"
});
sampleMultipleTextGrids = sampleMultipleTextGrids + "\n\n" + fs.readFileSync(__dirname + "/../data/sample_elan.TextGrid", {
encoding: "UTF-8"
});
sampleMultipleTextGrids = sampleMultipleTextGrids + "\n\nFile name = sampleNested\n" + fs.readFileSync(__dirname + "/../data/sampleNested.TextGrid", {
encoding: "UTF-8"
});
var json = textgrid.textgridToIGT(sampleMultipleTextGrids);
// console.log(JSON.stringify(json, null, 2));
expect(typeof json).toBe("object");
console.log(json.items.length);
expect(json.items.length).toEqual(16);
expect(json.items[0].fileName).toEqual("sampleMultipleSpeakers");
expect(json.items[2].fileName).toEqual("Unknown");
// console.log(JSON.stringify(json.items[json.items.length-1].fileName, null, 2));
expect(json.items[json.items.length - 1].fileName).toEqual("sampleNested");
expect(json.fileNames).toEqual([ 'sampleMultipleSpeakers', 'sampleNested' ]);
});
it("should put the filename as the text of an interval if there is only 1 or two intervals and the interval text is utterance", function() {
var sampleEmptyTextGrid = "\n\nFile name = sampleEmpty\n" + fs.readFileSync(__dirname + "/../data/sampleEmpty.TextGrid", {
encoding: "UTF-8"
});
var json = textgrid.textgridToIGT(sampleEmptyTextGrid);
// console.log(JSON.stringify(json, null, 2));
expect(typeof json).toBe("object");
// console.log(json.items.length);
expect(json.items.length).toEqual(1);
expect(json.items[0].intervals[0].text).toEqual("sampleEmpty");
});
xit("should create one interval if there were no intervals in the TextGrid", function() {
var sampleEmptyTextGrid = fs.readFileSync(__dirname + "/../data/sampleEmpty.TextGrid", {
encoding: "UTF-8"
});
var json = textgrid.textgridToIGT(sampleEmptyTextGrid);
// console.log(JSON.stringify(json, null, 2));
expect(typeof json).toBe("object");
console.log(json.items.length);
expect(json.items.length).toEqual(1);
});
it("TextGrid to json and json to TextGrid", function() {
var sampleUtterancesTextGrid = fs.readFileSync(__dirname + "/../data/sampleUtterances.TextGrid", {
encoding: "UTF-8"
});
var json = textgrid.textgridToJSON(sampleUtterancesTextGrid);
var str = textgrid.jsonToTextgrid(json);
expect(typeof str).toBe("string");
var json2 = textgrid.textgridToJSON(sampleUtterancesTextGrid);
expect(typeof json2).toBe("object");
console.log("json 2 success yes!!!! this is my first commit in github");
})
});