@@ -18,214 +18,214 @@ const specialWords = ["Chemistry", "Mathematics", "Geology", "Prosody", "Heraldr
18
18
19
19
20
20
function run ( input , parameters ) {
21
- const wholeText = input [ 0 ] ;
22
- const word = getWord ( wholeText ) ;
23
- const definition = getDefinition ( word , wholeText ) ;
24
- const result = getResult ( word , definition ) ;
25
- return result ;
21
+ const wholeText = input [ 0 ] ;
22
+ const word = getWord ( wholeText ) ;
23
+ const definition = getDefinition ( word , wholeText ) ;
24
+ const result = getResult ( word , definition ) ;
25
+ return result ;
26
26
}
27
27
28
28
29
29
30
30
// --------------------------------------------------------------------------------------
31
31
// Getting word. ------------------------------------------------------------------------
32
32
const getWord = ( text ) => {
33
- text = text . replaceAll ( "·" , "" ) ;
34
- text = text . split ( " " ) [ 0 ] ;
35
- text = removeWordException ( text ) ;
36
- return text ;
33
+ text = text . replaceAll ( "·" , "" ) ;
34
+ text = text . split ( " " ) [ 0 ] ;
35
+ text = removeWordException ( text ) ;
36
+ return text ;
37
37
}
38
38
39
39
const removeWordException = ( word ) => {
40
- word = removeHomonymNumberFromWord ( word ) ; // If word has multiple groups of definitions (like bat), remove 1 after word.
41
- word = removePartOfSpeechFromWord ( word ) ; // There are some words that part of seech follows right after word.
42
- return word ;
40
+ word = removeHomonymNumberFromWord ( word ) ; // If word has multiple groups of definitions (like bat), remove 1 after word.
41
+ word = removePartOfSpeechFromWord ( word ) ; // There are some words that part of seech follows right after word.
42
+ return word ;
43
43
}
44
44
45
45
const removeHomonymNumberFromWord = ( word ) => {
46
- if ( word . charAt ( word . length - 1 ) == "1" || word . charAt ( word . length - 1 ) == "2" ) {
47
- word = word . substring ( 0 , word . length - 1 ) ;
48
- }
49
- return word ;
46
+ if ( word . charAt ( word . length - 1 ) == "1" || word . charAt ( word . length - 1 ) == "2" ) {
47
+ word = word . substring ( 0 , word . length - 1 ) ;
48
+ }
49
+ return word ;
50
50
}
51
51
52
52
const removePartOfSpeechFromWord = ( word ) => {
53
- partOfSpeech . forEach ( ( element ) => {
54
- word = word . replace ( `${ element } ` , "" ) ;
55
- } )
56
- return word ;
53
+ partOfSpeech . forEach ( ( element ) => {
54
+ word = word . replace ( `${ element } ` , "" ) ;
55
+ } )
56
+ return word ;
57
57
}
58
58
59
59
60
60
// -----------------------------------------------------------------------------------
61
61
// Getting definition.----------------------------------------------------------------
62
62
const getDefinition = ( word , text ) => {
63
- text = pruneText ( text ) ;
64
- text = formatText ( text ) ;
65
- if ( hidingWordsFromDefinition === true ) {
66
- text = hideWordsFromDefinition ( word , text ) ;
67
- }
68
- return text ;
63
+ text = pruneText ( text ) ;
64
+ text = formatText ( text ) ;
65
+ if ( hidingWordsFromDefinition === true ) {
66
+ text = hideWordsFromDefinition ( word , text ) ;
67
+ }
68
+ return text ;
69
69
}
70
70
71
71
// pruning-------------------------------------------------------------------------------
72
72
const pruneText = ( text ) => {
73
- text = removeAdditionalInformation ( text ) ;
74
- text = removeWordAndPronounciation ( text ) ;
75
- if ( removingDotInformation === true ) {
76
- text = removeDotInformation ( text ) ;
77
- }
78
- return text ;
73
+ text = removeAdditionalInformation ( text ) ;
74
+ text = removeWordAndPronounciation ( text ) ;
75
+ if ( removingDotInformation === true ) {
76
+ text = removeDotInformation ( text ) ;
77
+ }
78
+ return text ;
79
79
}
80
80
81
81
const removeAdditionalInformation = ( text ) => {
82
- extraInformation . forEach ( ( element ) => {
83
- if ( text . includes ( `${ element } ` ) ) {
84
- text = text . substring ( 0 , text . indexOf ( `${ element } ` ) ) ;
85
- }
86
- } )
87
- return text ;
82
+ extraInformation . forEach ( ( element ) => {
83
+ if ( text . includes ( `${ element } ` ) ) {
84
+ text = text . substring ( 0 , text . indexOf ( `${ element } ` ) ) ;
85
+ }
86
+ } )
87
+ return text ;
88
88
}
89
89
90
90
const removeWordAndPronounciation = ( text ) => {
91
- let indexs = [ ] ;
92
- partOfSpeech . forEach ( ( element ) => {
93
- if ( text . includes ( `${ element } ` ) ) {
94
- indexs . push ( text . indexOf ( `${ element } ` ) ) ;
95
- }
96
- } )
97
- let minIndex = Math . min ( ...indexs ) ;
98
- if ( text [ minIndex - 1 ] === " " ) {
99
- text = text . substring ( minIndex - 1 ) ;
100
- } else {
101
- text = text . substring ( minIndex ) ;
102
- }
103
- return text ;
91
+ let indexs = [ ] ;
92
+ partOfSpeech . forEach ( ( element ) => {
93
+ if ( text . includes ( `${ element } ` ) ) {
94
+ indexs . push ( text . indexOf ( `${ element } ` ) ) ;
95
+ }
96
+ } )
97
+ let minIndex = Math . min ( ...indexs ) ;
98
+ if ( text [ minIndex - 1 ] === " " ) {
99
+ text = text . substring ( minIndex - 1 ) ;
100
+ } else {
101
+ text = text . substring ( minIndex ) ;
102
+ }
103
+ return text ;
104
104
}
105
105
106
106
const removeDotInformation = ( text ) => {
107
- while ( text . includes ( "•" ) ) {
108
- const startIndex = text . indexOf ( "•" ) - 1 ;
109
- const endIndex = text . indexOf ( ". " , startIndex ) ;
110
- text = text . substring ( 0 , startIndex ) + text . substring ( endIndex + 1 ) ;
111
- }
112
- return text ;
107
+ while ( text . includes ( "•" ) ) {
108
+ const startIndex = text . indexOf ( "•" ) - 1 ;
109
+ const endIndex = text . indexOf ( ". " , startIndex ) ;
110
+ text = text . substring ( 0 , startIndex ) + text . substring ( endIndex + 1 ) ;
111
+ }
112
+ return text ;
113
113
}
114
114
115
115
116
116
117
117
// formatting--------------------------------------------------------------------------
118
118
const formatText = ( text ) => {
119
- text = formatByNumbers ( text ) ;
120
- text = formatByPartOfSpeech ( text ) ;
121
- text = breakLineProperly ( text ) ;
122
- if ( htmlFormatting === true ) {
123
- text = formatByHtml ( text ) ;
124
- }
125
- text = text . trim ( ) ;
126
- return text ;
119
+ text = formatByNumbers ( text ) ;
120
+ text = formatByPartOfSpeech ( text ) ;
121
+ text = breakLineProperly ( text ) ;
122
+ if ( htmlFormatting === true ) {
123
+ text = formatByHtml ( text ) ;
124
+ }
125
+ text = text . trim ( ) ;
126
+ return text ;
127
127
}
128
128
129
129
const formatByNumbers = ( text ) => {
130
- for ( i = 2 ; i < 10 ; i ++ ) {
131
- text = text . replaceAll ( ` ${ i } ` , linebreak + `${ i } ` ) ;
132
- }
133
- text = text . replaceAll ( " •" , linebreak + "• " ) ;
134
- return text ;
130
+ for ( i = 2 ; i < 10 ; i ++ ) {
131
+ text = text . replaceAll ( ` ${ i } ` , linebreak + `${ i } ` ) ;
132
+ }
133
+ text = text . replaceAll ( " •" , linebreak + "• " ) ;
134
+ return text ;
135
135
}
136
136
137
137
const formatByPartOfSpeech = ( text ) => {
138
- partOfSpeech . forEach ( ( element ) => {
139
- text = text . replace ( `${ element } ` , `${ linebreak . repeat ( 2 ) } ${ element } ${ linebreak } ` ) ;
140
- let regexForPartOfSpeechesInsideParentheses = new RegExp ( `(\\([a-z ]*?)\\<br\\>\\<br\\>(${ element } )\\<br\\>([a-z ]*?\\))` ) ;
141
- text = text . replace ( regexForPartOfSpeechesInsideParentheses , "$1 $2 $3" ) ; // cancel line break part of speech inside defitition like "(as adecjtive pelleted)".
142
- } )
143
- text = text . replace ( `${ linebreak } ${ linebreak } ` , "" ) ;
144
- return text ;
138
+ partOfSpeech . forEach ( ( element ) => {
139
+ text = text . replace ( `${ element } ` , `${ linebreak . repeat ( 2 ) } ${ element } ${ linebreak } ` ) ;
140
+ let regexForPartOfSpeechesInsideParentheses = new RegExp ( `(\\([a-z ]*?)\\<br\\>\\<br\\>(${ element } )\\<br\\>([a-z ]*?\\))` ) ;
141
+ text = text . replace ( regexForPartOfSpeechesInsideParentheses , "$1 $2 $3" ) ; // cancel line break part of speech inside defitition like "(as adecjtive pelleted)".
142
+ } )
143
+ text = text . replace ( `${ linebreak } ${ linebreak } ` , "" ) ;
144
+ return text ;
145
145
}
146
146
147
147
const breakLineProperly = ( text ) => {
148
- partOfSpeech . forEach ( ( element ) => {
149
- let regexWithSquareBrackets = new RegExp ( `(${ element } )\\<br\\>(\\(?[^\\<\\d]*?\\)?) ?(\\[.*?\\]) ` , 'g' ) ;
150
- text = text . replace ( regexWithSquareBrackets , `$1 $2 $3${ linebreak } ` ) ;
151
- let regexWithoutSquareBrackets = new RegExp ( `(${ element } )\\<br\\> *(\\([^\\<\\d]*?\\)) ` ) ;
152
- text = text . replace ( regexWithoutSquareBrackets , `$1 $2${ linebreak } ` ) ;
153
- } )
154
- return text ;
148
+ partOfSpeech . forEach ( ( element ) => {
149
+ let regexWithSquareBrackets = new RegExp ( `(${ element } )\\<br\\>(\\(?[^\\<\\d]*?\\)?) ?(\\[.*?\\]) ` , 'g' ) ;
150
+ text = text . replace ( regexWithSquareBrackets , `$1 $2 $3${ linebreak } ` ) ;
151
+ let regexWithoutSquareBrackets = new RegExp ( `(${ element } )\\<br\\> *(\\([^\\<\\d]*?\\)) ` ) ;
152
+ text = text . replace ( regexWithoutSquareBrackets , `$1 $2${ linebreak } ` ) ;
153
+ } )
154
+ return text ;
155
155
}
156
156
157
157
const formatByHtml = ( text ) => {
158
- text = italicizeExampleSentences ( text ) ;
159
- text = italicizeSquareBracketWords ( text ) ;
160
- text = italicizeSpecialWords ( text ) ;
161
- text = changeItalicizedTextColorToDarkgrey ( text ) ;
162
- return text ;
158
+ text = italicizeExampleSentences ( text ) ;
159
+ text = italicizeSquareBracketWords ( text ) ;
160
+ text = italicizeSpecialWords ( text ) ;
161
+ text = changeItalicizedTextColorToDarkgrey ( text ) ;
162
+ return text ;
163
163
}
164
164
165
165
166
166
const italicizeExampleSentences = ( text ) => {
167
- while ( text . includes ( ": " ) ) {
168
- const indexOfColon = text . indexOf ( ": " ) ;
169
- const indexOfPeriod = text . indexOf ( "." , indexOfColon ) ;
170
- text = text . substring ( 0 , indexOfPeriod + 1 ) + "</i>" + text . substring ( indexOfPeriod + 1 ) ;
171
- text = text . replace ( ": " , ":<i> " ) ;
172
- }
173
- return text ;
167
+ while ( text . includes ( ": " ) ) {
168
+ const indexOfColon = text . indexOf ( ": " ) ;
169
+ const indexOfPeriod = text . indexOf ( "." , indexOfColon ) ;
170
+ text = text . substring ( 0 , indexOfPeriod + 1 ) + "</i>" + text . substring ( indexOfPeriod + 1 ) ;
171
+ text = text . replace ( ": " , ":<i> " ) ;
172
+ }
173
+ return text ;
174
174
}
175
175
176
176
const italicizeSquareBracketWords = ( text ) => {
177
- text = text . replaceAll ( "[" , "<i>[" ) ;
178
- text = text . replaceAll ( "]" , "]</i>" ) ;
179
- return text ;
177
+ text = text . replaceAll ( "[" , "<i>[" ) ;
178
+ text = text . replaceAll ( "]" , "]</i>" ) ;
179
+ return text ;
180
180
}
181
181
182
182
const italicizeSpecialWords = ( text ) => {
183
- specialWords . forEach ( ( element ) => {
184
- text = text . replaceAll ( `${ element } ` , `<i>${ element } </i>` ) ;
185
- } )
186
- return text ;
183
+ specialWords . forEach ( ( element ) => {
184
+ text = text . replaceAll ( `${ element } ` , `<i>${ element } </i>` ) ;
185
+ } )
186
+ return text ;
187
187
}
188
188
189
189
const changeItalicizedTextColorToDarkgrey = ( text ) => {
190
- text = text . replaceAll ( '<i>' , `<span style="color:${ exampleSentencesFontColor } "><i>` ) ;
191
- text = text . replaceAll ( '</i>' , '</i></span>' ) ;
192
- return text ;
190
+ text = text . replaceAll ( '<i>' , `<span style="color:${ exampleSentencesFontColor } "><i>` ) ;
191
+ text = text . replaceAll ( '</i>' , '</i></span>' ) ;
192
+ return text ;
193
193
}
194
194
195
195
196
196
197
197
// hiding -----------------------------------------------------------------------------
198
198
const hideWordsFromDefinition = ( word , text ) => {
199
- const lastCharacterRemovedWord = word . substring ( 0 , word . length - 1 ) ;
200
- const lastCharacter = word . substring ( word . length - 1 ) ;
201
-
202
- const startingVariations = [ " " , "(" , '“' ] ;
203
- const wordForms = [ "" , "d" , "ed" , "s" , "es" , "ing" , "er" , "est" , `${ lastCharacter } ed` , `${ lastCharacter } ing` ] ;
204
- const wordFormsWithY = [ "ing" , "ied" , "ies" , "ier" , "iest" ] ;
205
- const endingVariations = [ " " , "." , "," , ":" , ";" , ")" , '”' ] ;
206
-
207
- startingVariations . forEach ( ( startingVariation ) => {
208
- wordForms . forEach ( ( wordForm ) => {
209
- endingVariations . forEach ( ( endingVariation ) => {
210
- text = text . replaceAll ( `${ startingVariation } ${ word } ${ wordForm } ${ endingVariation } ` , `${ startingVariation } ${ blank } ${ wordForm } ${ endingVariation } ` ) ;
211
- } )
212
- } )
213
- wordFormsWithY . forEach ( ( wordFormWithY ) => {
214
- endingVariations . forEach ( ( endingVariation ) => {
215
- text = text . replaceAll ( `${ startingVariation } ${ lastCharacterRemovedWord } ${ wordFormWithY } ${ endingVariation } ` , `${ startingVariation } ${ blank } ${ wordFormWithY } ${ endingVariation } ` ) ;
216
- } )
217
- } )
218
- } )
219
- return text ;
199
+ const lastCharacterRemovedWord = word . substring ( 0 , word . length - 1 ) ;
200
+ const lastCharacter = word . substring ( word . length - 1 ) ;
201
+
202
+ const startingVariations = [ " " , "(" , '“' ] ;
203
+ const wordForms = [ "" , "d" , "ed" , "s" , "es" , "ing" , "er" , "est" , `${ lastCharacter } ed` , `${ lastCharacter } ing` ] ;
204
+ const wordFormsWithY = [ "ing" , "ied" , "ies" , "ier" , "iest" ] ;
205
+ const endingVariations = [ " " , "." , "," , ":" , ";" , ")" , '”' ] ;
206
+
207
+ startingVariations . forEach ( ( startingVariation ) => {
208
+ wordForms . forEach ( ( wordForm ) => {
209
+ endingVariations . forEach ( ( endingVariation ) => {
210
+ text = text . replaceAll ( `${ startingVariation } ${ word } ${ wordForm } ${ endingVariation } ` , `${ startingVariation } ${ blank } ${ wordForm } ${ endingVariation } ` ) ;
211
+ } )
212
+ } )
213
+ wordFormsWithY . forEach ( ( wordFormWithY ) => {
214
+ endingVariations . forEach ( ( endingVariation ) => {
215
+ text = text . replaceAll ( `${ startingVariation } ${ lastCharacterRemovedWord } ${ wordFormWithY } ${ endingVariation } ` , `${ startingVariation } ${ blank } ${ wordFormWithY } ${ endingVariation } ` ) ;
216
+ } )
217
+ } )
218
+ } )
219
+ return text ;
220
220
}
221
221
222
222
// -------------------------------------------------------------------------------------------
223
223
// Getting result-----------------------------------------------------------------------------
224
224
const getResult = ( word , definition ) => {
225
- if ( wordFirst === true ) {
226
- result = `${ word } ${ dividerBetweenWordAndDefinition } ${ definition } ${ endingCharacter } ` ;
227
- } else {
228
- result = `${ definition } ${ dividerBetweenWordAndDefinition } ${ word } ${ endingCharacter } ` ;
229
- }
230
- return result ;
225
+ if ( wordFirst === true ) {
226
+ result = `${ word } ${ dividerBetweenWordAndDefinition } ${ definition } ${ endingCharacter } ` ;
227
+ } else {
228
+ result = `${ definition } ${ dividerBetweenWordAndDefinition } ${ word } ${ endingCharacter } ` ;
229
+ }
230
+ return result ;
231
231
}
0 commit comments