Skip to content

Commit fb04bde

Browse files
authored
1 parent 93175b8 commit fb04bde

File tree

1 file changed

+58
-30
lines changed

1 file changed

+58
-30
lines changed

writing-style.html

+58-30
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,52 @@ <h1>Writing Style Analyzer</h1>
109109
'understood', 'worn', 'won', 'written'
110110
]
111111

112+
// Helper function to get word positions with their original text
113+
function getWordPositions(text) {
114+
const words = []
115+
const regex = /\S+/g
116+
let match
117+
118+
while ((match = regex.exec(text)) !== null) {
119+
words.push({
120+
word: match[0],
121+
index: match.index,
122+
length: match[0].length
123+
})
124+
}
125+
126+
return words
127+
}
128+
129+
function 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+
}
141+
}
142+
143+
const end = Math.min(text.length, wordPosition.index + wordPosition.length + 50)
144+
const context = text.slice(start, end).trim()
145+
146+
return context
147+
}
148+
112149
function findWeaselWords(text) {
113150
const results = []
114-
const words = text.toLowerCase().split(/\b/)
151+
const wordPositions = getWordPositions(text)
115152

116-
words.forEach((word, index) => {
117-
if (weaselWords.includes(word.trim())) {
153+
wordPositions.forEach(pos => {
154+
if (weaselWords.includes(pos.word.toLowerCase())) {
118155
results.push({
119-
word: word.trim(),
120-
index: index,
121-
context: getContext(text, index)
156+
word: pos.word,
157+
context: getContext(text, pos)
122158
})
123159
}
124160
})
@@ -129,49 +165,41 @@ <h1>Writing Style Analyzer</h1>
129165
function findPassiveVoice(text) {
130166
const results = []
131167
const beVerbs = ['am', 'is', 'are', 'was', 'were', 'be', 'been', 'being']
132-
const words = text.toLowerCase().split(/\s+/)
168+
const wordPositions = getWordPositions(text)
133169

134-
words.forEach((word, index) => {
135-
if (beVerbs.includes(word)) {
136-
const nextWord = words[index + 1]
137-
if (nextWord && (
138-
nextWord.endsWith('ed') ||
139-
irregularVerbs.includes(nextWord)
140-
)) {
170+
for (let i = 0; i < wordPositions.length - 1; i++) {
171+
const currentWord = wordPositions[i].word.toLowerCase()
172+
const nextWord = wordPositions[i + 1].word.toLowerCase()
173+
174+
if (beVerbs.includes(currentWord)) {
175+
if (nextWord.endsWith('ed') || irregularVerbs.includes(nextWord)) {
141176
results.push({
142-
construction: `${word} ${nextWord}`,
143-
context: getContext(text, index)
177+
construction: `${currentWord} ${nextWord}`,
178+
context: getContext(text, wordPositions[i])
144179
})
145180
}
146181
}
147-
})
182+
}
148183

149184
return results
150185
}
151186

152187
function findDuplicateWords(text) {
153188
const results = []
154-
const words = text.toLowerCase().split(/\s+/)
189+
const wordPositions = getWordPositions(text)
155190

156-
words.forEach((word, index) => {
157-
if (index > 0 && word === words[index - 1]) {
191+
for (let i = 1; i < wordPositions.length; i++) {
192+
if (wordPositions[i].word.toLowerCase() === wordPositions[i - 1].word.toLowerCase()) {
158193
results.push({
159-
word: word,
160-
context: getContext(text, index)
194+
word: wordPositions[i].word,
195+
context: getContext(text, wordPositions[i])
161196
})
162197
}
163-
})
198+
}
164199

165200
return results
166201
}
167202

168-
function getContext(text, index) {
169-
const words = text.split(/\s+/)
170-
const start = Math.max(0, index - 3)
171-
const end = Math.min(words.length, index + 4)
172-
return words.slice(start, end).join(' ')
173-
}
174-
175203
function displayResults(weasels, passives, duplicates) {
176204
const resultsDiv = document.getElementById('results')
177205
resultsDiv.innerHTML = ''

0 commit comments

Comments
 (0)