-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfollowerfreq-analysis-stream.js
More file actions
114 lines (99 loc) · 2.73 KB
/
Copy pathfollowerfreq-analysis-stream.js
File metadata and controls
114 lines (99 loc) · 2.73 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
// It's a stream that analyzes phoneme groups you write to it, then returns the
// analysis via the callback when the end is reached.
var Writable = require('stream').Writable;
var pluck = require('lodash.pluck');
function createAnalyzeFollowerStream(opts) {
var followerFreqsForPhonemes = {};
// Must use object mode.
if (!opts.objectMode) {
opts.objectMode = true;
}
var analyzeStream = new Writable(opts);
analyzeStream._write = function writeToStream(group, encoding, callback) {
recordPhonemeGroupFollowFrequencies(group);
callback();
};
analyzeStream.end = function end() {
if (opts.done) {
opts.done(null, followerFreqsForPhonemes);
}
};
// Incoming phoneme groups will look like this:
// {
// "word": "ABALONE",
// "phonemes": [
// {"phoneme":"AE","stress":2},
// {"phoneme":"B","stress":-1},
// {"phoneme":"AH","stress":0},
// {"phoneme":"L","stress":-1},
// {"phoneme":"OW","stress":1},
// {"phoneme":"N","stress":-1},
// {"phoneme":"IY","stress":0}
// ]
//
// }
//
// They may also have this optional syllables array:
// "syllables": [
// [
// "AE"
// ],
// [
// "B",
// "AH"
// ],
// [
// "L",
// "OW"
// ],
// [
// "N",
// "IY"
// ]
// ]
function recordPhonemeGroupFollowFrequencies(group) {
var phonemeSequence;
if (opts.analyzeInSyllables) {
group.syllables.forEach(recordPhonemeSequenceFrequencies);
} else {
phonemeSequence = pluck(group.phonemes, 'phoneme');
recordPhonemeSequenceFrequencies(phonemeSequence);
}
}
function recordPhonemeSequenceFrequencies(sequence) {
var phonemeSequence;
var previousPhoneme;
if (opts.reverse) {
phonemeSequence = ['START'].concat(sequence);
previousPhoneme = 'END';
} else {
phonemeSequence = sequence.concat('END');
previousPhoneme = 'START';
}
for (var i = 0; i < phonemeSequence.length; ++i) {
var phoneme;
if (opts.reverse) {
phoneme = phonemeSequence[phonemeSequence.length - 1 - i];
} else {
phoneme = phonemeSequence[i];
}
if (previousPhoneme) {
var freqs = {};
if (previousPhoneme in followerFreqsForPhonemes) {
freqs = followerFreqsForPhonemes[previousPhoneme];
} else {
followerFreqsForPhonemes[previousPhoneme] = freqs;
}
var frequency = 0;
if (phoneme in freqs) {
frequency = freqs[phoneme];
}
frequency += 1;
freqs[phoneme] = frequency;
}
previousPhoneme = phoneme;
}
}
return analyzeStream;
}
module.exports = createAnalyzeFollowerStream;