compromise-speech 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,107 @@
1
+ const starts_with_single_vowel_combos = /^(eu)/i
2
+ const joining_consonant_vowel = /^[^aeiou]e([^d]|$)/
3
+ const cvcv_same_consonant = /^([^aeiouy])[aeiouy]\1[aeiouy]/
4
+ const cvcv_same_vowel = /^[^aeiouy]([aeiouy])[^aeiouy]\1/
5
+ const cvcv_known_consonants = /^([tg][aeiouy]){2}/
6
+ const only_one_or_more_c = /^[^aeiouy]+$/
7
+
8
+ const ends_with_vowel = /[aeiouy]$/
9
+ const starts_with_consonant_vowel = /^[^aeiouy]h?[aeiouy]/
10
+
11
+ const ones = [
12
+ /^[^aeiou]?ion/,
13
+ /^[^aeiou]?ised/,
14
+ /^[^aeiou]?iled/,
15
+
16
+ // -ing, -ent
17
+ /[aeiou]n[gt]$/,
18
+
19
+ // -ate, -age
20
+ /\wa[gt]e$/,
21
+ ]
22
+
23
+ //suffix fixes
24
+ const postprocess = function (arr) {
25
+ //trim whitespace
26
+ arr = arr.map(function (w) {
27
+ return w.trim()
28
+ })
29
+ arr = arr.filter(function (w) {
30
+ return w !== ''
31
+ })
32
+ // if (arr.length > 2) {
33
+ // return arr;
34
+ // }
35
+ let l = arr.length
36
+ if (l > 1) {
37
+ let suffix = arr[l - 2] + arr[l - 1]
38
+ for (let i = 0; i < ones.length; i++) {
39
+ if (suffix.match(ones[i])) {
40
+ arr[l - 2] = arr[l - 2] + arr[l - 1]
41
+ arr.pop()
42
+ }
43
+ }
44
+ }
45
+
46
+ // since the open syllable detection is overzealous,
47
+ // sometimes need to rejoin incorrect splits
48
+ if (arr.length > 1) {
49
+ let first_is_open =
50
+ (arr[0].length === 1 || arr[0].match(starts_with_consonant_vowel)) &&
51
+ arr[0].match(ends_with_vowel)
52
+ let second_is_joining = arr[1].match(joining_consonant_vowel)
53
+
54
+ if (first_is_open && second_is_joining) {
55
+ let possible_combination = arr[0] + arr[1]
56
+ let probably_separate_syllables =
57
+ possible_combination.match(cvcv_same_consonant) ||
58
+ possible_combination.match(cvcv_same_vowel) ||
59
+ possible_combination.match(cvcv_known_consonants)
60
+
61
+ if (!probably_separate_syllables) {
62
+ arr[0] = arr[0] + arr[1]
63
+ arr.splice(1, 1)
64
+ }
65
+ }
66
+ }
67
+
68
+ if (arr.length > 1) {
69
+ let second_to_last_is_open =
70
+ arr[arr.length - 2].match(starts_with_consonant_vowel) &&
71
+ arr[arr.length - 2].match(ends_with_vowel)
72
+ let last_is_joining =
73
+ arr[arr.length - 1].match(joining_consonant_vowel) &&
74
+ ones.every(re => !arr[arr.length - 1].match(re))
75
+
76
+ if (second_to_last_is_open && last_is_joining) {
77
+ let possible_combination = arr[arr.length - 2] + arr[arr.length - 1]
78
+ let probably_separate_syllables =
79
+ possible_combination.match(cvcv_same_consonant) ||
80
+ possible_combination.match(cvcv_same_vowel) ||
81
+ possible_combination.match(cvcv_known_consonants)
82
+
83
+ if (!probably_separate_syllables) {
84
+ arr[arr.length - 2] = arr[arr.length - 2] + arr[arr.length - 1]
85
+ arr.splice(arr.length - 1, 1)
86
+ }
87
+ }
88
+ }
89
+
90
+ if (arr.length > 1) {
91
+ let single = arr[0] + arr[1]
92
+ if (single.match(starts_with_single_vowel_combos)) {
93
+ arr[0] = single
94
+ arr.splice(1, 1)
95
+ }
96
+ }
97
+
98
+ if (arr.length > 1) {
99
+ if (arr[arr.length - 1].match(only_one_or_more_c)) {
100
+ arr[arr.length - 2] = arr[arr.length - 2] + arr[arr.length - 1]
101
+ arr.splice(arr.length - 1, 1)
102
+ }
103
+ }
104
+
105
+ return arr
106
+ }
107
+ export default postprocess
@@ -0,0 +1,90 @@
1
+ //chop a string into pronounced syllables
2
+ import postProcess from './postProcess.js'
3
+
4
+ const all_spaces = / +/g
5
+ const ends_with_vowel = /[aeiouy]$/
6
+ const starts_with_consonant_vowel = /^[^aeiouy]h?[aeiouy]/
7
+ const starts_with_e_then_specials = /^e[sm]/
8
+ const starts_with_e = /^e/
9
+ const ends_with_noisy_vowel_combos = /(eo|eu|ia|oa|ua|ui)$/i
10
+ const aiouy = /[aiouy]/
11
+ const ends_with_ee = /ee$/
12
+ // const whitespace_dash = /\s\-/
13
+
14
+ //method is nested because it's called recursively
15
+ const doWord = function (w) {
16
+ let all = []
17
+ let chars = w.split('')
18
+ let before = ''
19
+ let after = ''
20
+ let current = ''
21
+ for (let i = 0; i < chars.length; i++) {
22
+ before = chars.slice(0, i).join('')
23
+ current = chars[i]
24
+ after = chars.slice(i + 1, chars.length).join('')
25
+ let candidate = before + chars[i]
26
+
27
+ //it's a consonant that comes after a vowel
28
+ if (before.match(ends_with_vowel) && !current.match(ends_with_vowel)) {
29
+ if (after.match(starts_with_e_then_specials)) {
30
+ candidate += 'e'
31
+ after = after.replace(starts_with_e, '')
32
+ }
33
+ all.push(candidate)
34
+ return all.concat(doWord(after))
35
+ }
36
+
37
+ //unblended vowels ('noisy' vowel combinations)
38
+ if (candidate.match(ends_with_noisy_vowel_combos)) {
39
+ //'io' is noisy, not in 'ion'
40
+ all.push(before)
41
+ all.push(current)
42
+ return all.concat(doWord(after)) //recursion
43
+ }
44
+
45
+ // if candidate is followed by a CV, assume consecutive open syllables
46
+ if (candidate.match(ends_with_vowel) && after.match(starts_with_consonant_vowel)) {
47
+ all.push(candidate)
48
+ return all.concat(doWord(after))
49
+ }
50
+ }
51
+ //if still running, end last syllable
52
+ if (w.match(aiouy) || w.match(ends_with_ee)) {
53
+ //allow silent trailing e
54
+ all.push(w)
55
+ } else if (w) {
56
+ let last = all.length - 1
57
+ if (last < 0) {
58
+ last = 0
59
+ }
60
+ all[last] = (all[last] || '') + w //append it to the last one
61
+ }
62
+ return all
63
+ }
64
+
65
+ let syllables = function (str) {
66
+ let all = []
67
+ if (!str) {
68
+ return all
69
+ }
70
+ str = str.replace(/[.,?]/g, '')
71
+ str.split(all_spaces).map(s => {
72
+ all = all.concat(doWord(s))
73
+ })
74
+
75
+ // str.split(whitespace_dash).forEach(doWord)
76
+ all = postProcess(all)
77
+
78
+ //for words like 'tree' and 'free'
79
+ if (all.length === 0) {
80
+ all = [str]
81
+ }
82
+ //filter blanks
83
+ all = all.filter(s => s)
84
+
85
+ return all
86
+ }
87
+
88
+ // console.log(syllables('civilised'))
89
+
90
+ export default syllables
package/src/plugin.js ADDED
@@ -0,0 +1,7 @@
1
+ import compute from './compute/index.js'
2
+ import api from './api.js'
3
+
4
+ export default {
5
+ api,
6
+ compute
7
+ }
package/tests/_lib.js ADDED
@@ -0,0 +1,13 @@
1
+ /* eslint-disable no-console */
2
+ import build from '../../../builds/one/compromise-one.mjs'
3
+ import src from '../../../src/one.js'
4
+ let nlp = src
5
+ import speech from '../src/plugin.js'
6
+
7
+ // import dateBuild from '../builds/compromise-speech.js'
8
+ if (process.env.TESTENV === 'prod') {
9
+ console.warn('== production build test 🚀 ==')
10
+ nlp = build
11
+ }
12
+ nlp.plugin(speech)
13
+ export default nlp
@@ -0,0 +1,21 @@
1
+ import test from 'tape'
2
+ import nlp from './_lib.js'
3
+
4
+ test('soundsLike-tests', function (t) {
5
+ let arr = [
6
+ ['phil collins', 'fil kolins'],
7
+ ['Philadelphia freedom', 'filatelfia fretom'],
8
+ ['Shine on me', 'shine on me'],
9
+ ['peace of mind', 'pease of mint'],
10
+ ['Yes I do', 'yes i to'],
11
+ ['Zapped me', 'sapet me'],
12
+ ['Right between the eyes', 'rit betwen the eyes'],
13
+ // ['But the times have changed', 'but the times hafe kshanjet'],
14
+ ]
15
+ arr.forEach((a) => {
16
+ let doc = nlp(a[0])
17
+ let str = doc.soundsLike({})[0].join(' ')
18
+ t.equal(str, a[1], a[0])
19
+ })
20
+ t.end()
21
+ })
@@ -0,0 +1,73 @@
1
+ import test from 'tape'
2
+ import nlp from './_lib.js'
3
+
4
+ const words = [
5
+ 'sud den ly',
6
+ 'con sti pa tion',
7
+ 'di a bo lic',
8
+ 'fa ted',
9
+ 'ge ne tic',
10
+ 'hy giene',
11
+ 'o ma ha',
12
+ 'i mi ta ted',
13
+ 'tree',
14
+ 'ci vi lised',
15
+ 'went',
16
+ 'to ge ther',
17
+ 'tog gle',
18
+ 'move ment',
19
+ 'mo ment',
20
+ 'do nate',
21
+ 'vi king',
22
+ 'wat tage',
23
+ 'con gre gate',
24
+ 'some thing',
25
+ 'sales man',
26
+ 're sour ces',
27
+ 'eve ry thing',
28
+ 'eve ry bo dy',
29
+ 'hole',
30
+ 'ho ly',
31
+ 'sec ret',
32
+ 'cause',
33
+ 'fate',
34
+ 'fates',
35
+ 'eu lo gy',
36
+ 'e on',
37
+ 'rea sons',
38
+ 'and',
39
+ 'hoc key',
40
+ 'wild',
41
+ 'fun',
42
+ 'fun ny',
43
+ 'hor ses',
44
+ 'cal ga ry',
45
+ 'tor na do',
46
+ 'pen',
47
+ 'stu dy',
48
+ 'ja mai ca',
49
+ 'tri ni dad',
50
+ 'ca na da',
51
+ 'i ran',
52
+ 'i raq',
53
+ 'sau di a ra bi a',
54
+ 'clin ton',
55
+ 'hub ris',
56
+ 'tur key',
57
+ 'cor si ca',
58
+ 'dev iled',
59
+ // 'horse',
60
+ // 'chance',
61
+ // 'peo ple'
62
+ ]
63
+
64
+ test('test length', function (t) {
65
+ words.forEach(sep => {
66
+ let str = sep.replace(/ /g, '')
67
+ let doc = nlp(str)
68
+ let have = doc.syllables()[0]
69
+ let want = sep.split(/ /g)
70
+ t.equal(have.length, want.length, str)
71
+ })
72
+ t.end()
73
+ })