This repository has been archived by the owner on Dec 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
162 lines (138 loc) · 4.34 KB
/
index.js
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
const _ = require('lodash')
function nullArguments (args, wordsLength) {
let lastWord = 0
const newAdditions = []
_.forEach(args, arg => {
if (arg.start > lastWord) {
newAdditions.push({start: lastWord, end: arg.start})
}
newAdditions.push(arg)
lastWord = arg.end
})
if (wordsLength > lastWord) {
newAdditions.push({start: lastWord, end: wordsLength})
}
return newAdditions
}
function groupArguments (output) {
const groupString = _.chain(output.arguments)
.map(({start, end, value}) => {
const joinedWords = _.chain(output.words)
.slice(start, end)
.map(word => word.placeholder ? '\uFFFC' : word.text)
.join('')
.value()
return `\uFFF9${joinedWords}\uFFFA${value || ''}\uFFFB`
})
.join('')
.value()
return groupString
}
function groupQualifiers (output) {
const groupString = _.chain(output.words)
.map(word => word.placeholder ? '\uFFFC' : word.text)
.map((text, i) => {
const qualifiersEndingHere = _.chain(output.qualifiers)
.filter(qualifier => qualifier.end === i + 1)
.map('value')
.value()
return `${text}\uFFF9${qualifiersEndingHere.join('\uFFFA')}\uFFFB`
})
.join('')
.value()
return groupString
}
function compareQualifiers (a, b) {
return a.value === b.value && a.end === b.end
}
function removeUnnecessaryQualifiers (outputGroup) {
const commonQualifiers = _.chain(outputGroup)
.map('qualifiers')
.thru(qualifierGroup => _.intersectionWith(...qualifierGroup, compareQualifiers))
.value()
// Remove the common qualifiers, and take the first unique qualifier for each
// end position, so as not to overload with information
return _.map(outputGroup, output => {
const qualifiers = _.chain(output.qualifiers)
.differenceWith(commonQualifiers, compareQualifiers)
.uniqBy('end')
.value()
return _.assign({}, output, {qualifiers})
})
}
function mapQualifiers (argumentGroup) {
// if there is only one in the argumentGroup, no need to worry about quals
if (argumentGroup.length === 1) {
argumentGroup = [_.assign({}, argumentGroup[0], {qualifiers: []})]
}
return _.chain(argumentGroup)
.groupBy(output => groupQualifiers(output))
.map(mapPlaceholderGroups)
.thru(removeUnnecessaryQualifiers)
.value()
}
function mapPlaceholderGroups (qualifierGroup) {
// these have the same qualifiers - no need to even specify
// if (qualifierGroup.length > 1) {
// qualifierGroup[0].qualifiers = []
// }
const placeholders = _.chain(qualifierGroup)
.map(output => {
return _.chain(output.words)
.map(word => word.placeholder ? word.label : undefined)
.value()
})
.thru(descriptorLists => _.zip(...descriptorLists))
.map(x => _.uniq(x))
.map(x => _.filter(x))
.value()
const output = qualifierGroup[0]
const newWords = _.map(output.words, (word, index) => {
if (word.placeholder) {
// no way to delete w/o cloning
const newWord = _.clone(word)
newWord.placeholderTexts = placeholders[index]
delete newWord.label
return newWord
} else {
return word
}
})
return _.assign({}, output, {words: newWords})
}
function doTopLevelArguments (output) {
// If any words don't have a placeholder label, use its argument instead
const newWords = _.map(output.words, (word, index) => {
if (word.placeholder && !word.label) {
const firstArgument = _.find(output.arguments, argument => {
return argument.start === index && argument.end === index + 1
})
if (firstArgument) {
return _.assign({}, word, {label: firstArgument.value})
}
}
return word
})
let lastWord = 0
const topLevelArguments = _.filter(output.arguments, ({start, end}) => {
if (end > lastWord) {
if (start === end - 1 && output.words[start].placeholder) {
return false
}
lastWord = end
return true
}
return false
})
const nullTopLevels = nullArguments(topLevelArguments, output.words.length)
return _.assign({}, output, {arguments: nullTopLevels, words: newWords})
}
exports.combinePlaceholders = function (outputs, limit = 100) {
return _.chain(outputs)
.map(doTopLevelArguments)
.groupBy(groupArguments)
.flatMap(mapQualifiers)
.sortBy(option => -option.score)
.take(limit)
.value()
}