@@ -92,7 +92,7 @@ <h1>Writing Style Analyzer</h1>
9292 'mostly' , 'largely' , 'huge' , 'tiny' , 'excellent' , 'interestingly' ,
9393 'significantly' , 'substantially' , 'clearly' , 'vast' , 'relatively' ,
9494 'completely'
95- ]
95+ ] ;
9696
9797// Common irregular verbs for passive voice detection
9898const irregularVerbs = [
@@ -107,106 +107,106 @@ <h1>Writing Style Analyzer</h1>
107107 'shown' , 'shut' , 'sung' , 'sat' , 'slept' , 'spoken' , 'spent' ,
108108 'stood' , 'taken' , 'taught' , 'told' , 'thought' , 'thrown' ,
109109 'understood' , 'worn' , 'won' , 'written'
110- ]
110+ ] ;
111111
112112// Helper function to get word positions with their original text
113113function getWordPositions ( text ) {
114- const words = [ ]
115- const regex = / \S + / g
116- let match
114+ const words = [ ] ;
115+ const regex = / \S + / g;
116+ let match ;
117117
118118 while ( ( match = regex . exec ( text ) ) !== null ) {
119119 words . push ( {
120120 word : match [ 0 ] ,
121121 index : match . index ,
122122 length : match [ 0 ] . length
123- } )
123+ } ) ;
124124 }
125125
126- return words
126+ return words ;
127127}
128128
129129function getContext ( text , wordPosition , prevWords = 3 ) {
130- // Find start position for previous words
131- let start = wordPosition . index
132- let wordCount = 0
133- for ( let i = start - 1 ; i >= 0 && wordCount < prevWords ; i -- ) {
134- if ( i === 0 || text [ i - 1 ] === ' ' ) {
135- wordCount ++
136- if ( wordCount === prevWords ) {
137- start = i
138- break
139- }
140- }
130+ const allWords = text . split ( / \s + / ) ;
131+
132+ // Find which word index we're at
133+ let currentWordIndex = 0 ;
134+ let currentPos = 0 ;
135+ while ( currentPos < wordPosition . index && currentWordIndex < allWords . length ) {
136+ currentPos += allWords [ currentWordIndex ] . length ;
137+ // Account for the space after the word
138+ if ( currentPos < text . length ) currentPos ++ ;
139+ currentWordIndex ++ ;
141140 }
142141
143- const end = Math . min ( text . length , wordPosition . index + wordPosition . length + 50 )
144- const context = text . slice ( start , end ) . trim ( )
142+ // Get the previous N words and next few words
143+ const start = Math . max ( 0 , currentWordIndex - prevWords ) ;
144+ const end = Math . min ( allWords . length , currentWordIndex + 4 ) ;
145145
146- return context
146+ return allWords . slice ( start , end ) . join ( ' ' ) ;
147147}
148148
149149function findWeaselWords ( text ) {
150- const results = [ ]
151- const wordPositions = getWordPositions ( text )
150+ const results = [ ] ;
151+ const wordPositions = getWordPositions ( text ) ;
152152
153153 wordPositions . forEach ( pos => {
154154 if ( weaselWords . includes ( pos . word . toLowerCase ( ) ) ) {
155155 results . push ( {
156156 word : pos . word ,
157157 context : getContext ( text , pos )
158- } )
158+ } ) ;
159159 }
160- } )
160+ } ) ;
161161
162- return results
162+ return results ;
163163}
164164
165165function findPassiveVoice ( text ) {
166- const results = [ ]
167- const beVerbs = [ 'am' , 'is' , 'are' , 'was' , 'were' , 'be' , 'been' , 'being' ]
168- const wordPositions = getWordPositions ( text )
166+ const results = [ ] ;
167+ const beVerbs = [ 'am' , 'is' , 'are' , 'was' , 'were' , 'be' , 'been' , 'being' ] ;
168+ const wordPositions = getWordPositions ( text ) ;
169169
170170 for ( let i = 0 ; i < wordPositions . length - 1 ; i ++ ) {
171- const currentWord = wordPositions [ i ] . word . toLowerCase ( )
172- const nextWord = wordPositions [ i + 1 ] . word . toLowerCase ( )
171+ const currentWord = wordPositions [ i ] . word . toLowerCase ( ) ;
172+ const nextWord = wordPositions [ i + 1 ] . word . toLowerCase ( ) ;
173173
174174 if ( beVerbs . includes ( currentWord ) ) {
175175 if ( nextWord . endsWith ( 'ed' ) || irregularVerbs . includes ( nextWord ) ) {
176176 results . push ( {
177177 construction : `${ currentWord } ${ nextWord } ` ,
178178 context : getContext ( text , wordPositions [ i ] )
179- } )
179+ } ) ;
180180 }
181181 }
182182 }
183183
184- return results
184+ return results ;
185185}
186186
187187function findDuplicateWords ( text ) {
188- const results = [ ]
189- const wordPositions = getWordPositions ( text )
188+ const results = [ ] ;
189+ const wordPositions = getWordPositions ( text ) ;
190190
191191 for ( let i = 1 ; i < wordPositions . length ; i ++ ) {
192192 if ( wordPositions [ i ] . word . toLowerCase ( ) === wordPositions [ i - 1 ] . word . toLowerCase ( ) ) {
193193 results . push ( {
194194 word : wordPositions [ i ] . word ,
195195 context : getContext ( text , wordPositions [ i ] )
196- } )
196+ } ) ;
197197 }
198198 }
199199
200- return results
200+ return results ;
201201}
202202
203203function displayResults ( weasels , passives , duplicates ) {
204- const resultsDiv = document . getElementById ( 'results' )
205- resultsDiv . innerHTML = ''
204+ const resultsDiv = document . getElementById ( 'results' ) ;
205+ resultsDiv . innerHTML = '' ;
206206
207207 // Weasel Words
208- const weaselDiv = document . createElement ( 'div' )
209- weaselDiv . className = 'category'
208+ const weaselDiv = document . createElement ( 'div' ) ;
209+ weaselDiv . className = 'category' ;
210210 weaselDiv . innerHTML = `
211211 <h2>Weasel Words</h2>
212212 ${ weasels . length === 0 ? 'No weasel words found.' :
@@ -215,12 +215,12 @@ <h2>Weasel Words</h2>
215215 Found "<span class="highlight">${ w . word } </span>" in: "${ w . context } "
216216 </div>
217217 ` ) . join ( '' ) }
218- `
219- resultsDiv . appendChild ( weaselDiv )
218+ ` ;
219+ resultsDiv . appendChild ( weaselDiv ) ;
220220
221221 // Passive Voice
222- const passiveDiv = document . createElement ( 'div' )
223- passiveDiv . className = 'category'
222+ const passiveDiv = document . createElement ( 'div' ) ;
223+ passiveDiv . className = 'category' ;
224224 passiveDiv . innerHTML = `
225225 <h2>Passive Voice</h2>
226226 ${ passives . length === 0 ? 'No passive voice constructions found.' :
@@ -229,12 +229,12 @@ <h2>Passive Voice</h2>
229229 Found passive voice "<span class="highlight">${ p . construction } </span>" in: "${ p . context } "
230230 </div>
231231 ` ) . join ( '' ) }
232- `
233- resultsDiv . appendChild ( passiveDiv )
232+ ` ;
233+ resultsDiv . appendChild ( passiveDiv ) ;
234234
235235 // Duplicate Words
236- const duplicateDiv = document . createElement ( 'div' )
237- duplicateDiv . className = 'category'
236+ const duplicateDiv = document . createElement ( 'div' ) ;
237+ duplicateDiv . className = 'category' ;
238238 duplicateDiv . innerHTML = `
239239 <h2>Duplicate Words</h2>
240240 ${ duplicates . length === 0 ? 'No duplicate words found.' :
@@ -243,18 +243,18 @@ <h2>Duplicate Words</h2>
243243 Found duplicate word "<span class="highlight">${ d . word } </span>" in: "${ d . context } "
244244 </div>
245245 ` ) . join ( '' ) }
246- `
247- resultsDiv . appendChild ( duplicateDiv )
246+ ` ;
247+ resultsDiv . appendChild ( duplicateDiv ) ;
248248}
249249
250250// Set up event listener
251251document . getElementById ( 'input' ) . addEventListener ( 'input' , ( e ) => {
252- const text = e . target . value
253- const weasels = findWeaselWords ( text )
254- const passives = findPassiveVoice ( text )
255- const duplicates = findDuplicateWords ( text )
256- displayResults ( weasels , passives , duplicates )
257- } )
252+ const text = e . target . value ;
253+ const weasels = findWeaselWords ( text ) ;
254+ const passives = findPassiveVoice ( text ) ;
255+ const duplicates = findDuplicateWords ( text ) ;
256+ displayResults ( weasels , passives , duplicates ) ;
257+ } ) ;
258258</ script >
259259</ body >
260260</ html >
0 commit comments