-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordCounter.html
More file actions
371 lines (325 loc) · 12 KB
/
Copy pathWordCounter.html
File metadata and controls
371 lines (325 loc) · 12 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>单词计数器 - JustHTML 只需网页</title>
<script>
/**
* 动态加载(强制去除缓存) CSS 的函数
* @param {string} src - 要加载的 CSS 的 src 链接
* @returns {Promise} 返回一个 Promise 对象,用于处理异步加载
*/
function loadStyle(src) {
return new Promise((resolve, reject) => {
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = src + '?v=' + new Date().getTime(); // 使用时间戳避免缓存
link.async = true;
link.onload = resolve;
link.onerror = reject;
document.head.appendChild(link);
});
}
/**
* 动态加载(强制去除缓存) JS 的函数
* @param {string} src - 要加载的 JS 的 src 链接
* @returns {Promise} 返回一个 Promise 对象,用于处理异步加载
*/
function loadScript(src) {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = src + '?v=' + new Date().getTime(); // 使用时间戳避免缓存
script.async = true;
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
}
</script>
<script>
loadStyle('../assets/style.css')
let scriptArray = [
"../assets/AddHeader_Tools.js",
"../assets/AddFooter.js",
"../assets/AddIco.js",
"../assets/AutoRun.js",
]
scriptArray.forEach(script => {
loadScript(script);
});
</script>
<script src="../package/Vue/code/vue.global.prod.js"></script>
<script type="module" src="../package/Sober/code/main.js"></script>
<style>
.counter-stats {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
gap: 15px;
margin: 20px 0;
}
.stat-card {
background-color: var(--bg-tertiary);
padding: 15px;
border-radius: var(--border-radius);
text-align: center;
}
.stat-value {
font-size: 2em;
font-weight: bold;
color: var(--accent-color);
margin: 10px 0;
}
.stat-label {
font-size: 0.9em;
color: var(--text-secondary);
}
.controls {
display: flex;
flex-wrap: wrap;
gap: 10px;
margin: 15px 0;
}
.info-box {
background-color: var(--bg-tertiary);
padding: 10px;
border-radius: var(--border-radius);
margin: 10px 0;
}
.analysis-details {
margin-top: 20px;
padding: 15px;
background-color: var(--bg-tertiary);
border-radius: var(--border-radius);
}
.detail-item {
margin: 10px 0;
padding: 8px;
background-color: var(--bg-secondary);
border-radius: 4px;
}
.lang-selector {
margin: 15px 0;
}
.lang-selector label {
display: inline-block;
margin-right: 10px;
color: var(--accent-color);
}
.lang-selector select {
padding: 5px;
}
</style>
</head>
<body>
<div id="app">
<div class="container">
<a href="../index.html" class="back-link">← 返回首页</a>
<div class="tool-section">
<h2>单词计数器</h2>
<p>统计文本中的字符、单词、行数等信息</p>
<div class="info-box">
<p><strong>功能说明:</strong></p>
<p>• 统计字符数(含/不含空格)</p>
<p>• 统计单词数、行数</p>
<p>• 统计段落数、句子数</p>
<p>• 支持中文和英文文本统计</p>
</div>
<div class="lang-selector">
<label for="language">文本类型</label>
<select id="language" v-model="language" @change="analyzeText">
<option value="auto">自动检测</option>
<option value="english">英文</option>
<option value="chinese">中文</option>
</select>
</div>
<div>
<textarea
v-model="inputText"
placeholder="在此输入要统计的文本..."
rows="12"
@input="analyzeText"
></textarea>
<div class="counter-stats">
<div class="stat-card">
<div class="stat-label">字符数 (含空格)</div>
<div class="stat-value">{{ stats.charsWithSpaces }}</div>
</div>
<div class="stat-card">
<div class="stat-label">字符数 (不含空格)</div>
<div class="stat-value">{{ stats.charsWithoutSpaces }}</div>
</div>
<div class="stat-card">
<div class="stat-label">单词数</div>
<div class="stat-value">{{ stats.words }}</div>
</div>
<div class="stat-card">
<div class="stat-label">行数</div>
<div class="stat-value">{{ stats.lines }}</div>
</div>
<div class="stat-card">
<div class="stat-label">段落数</div>
<div class="stat-value">{{ stats.paragraphs }}</div>
</div>
<div class="stat-card">
<div class="stat-label">句子数</div>
<div class="stat-value">{{ stats.sentences }}</div>
</div>
</div>
<div class="controls">
<button @click="clearText">清空</button>
<button @click="sampleText">示例文本</button>
<button @click="copyStats">复制统计信息</button>
</div>
<div class="analysis-details">
<h3>详细分析</h3>
<div class="detail-item">
<p><strong>字符密度:</strong> 平均每行 {{ stats.lines > 0 ? (stats.charsWithSpaces / stats.lines).toFixed(2) : 0 }} 个字符</p>
</div>
<div class="detail-item">
<p><strong>单词密度:</strong> 平均每行 {{ stats.lines > 0 ? (stats.words / stats.lines).toFixed(2) : 0 }} 个单词</p>
</div>
<div class="detail-item" v-if="language === 'chinese' || (language === 'auto' && detectedLanguage === 'chinese')">
<p><strong>汉字数量:</strong> {{ stats.chineseChars }} 个</p>
</div>
<div class="detail-item">
<p><strong>可读性等级:</strong> {{ readabilityLevel }}</p>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
const { createApp, ref, computed } = Vue;
createApp({
setup() {
const inputText = ref('');
const language = ref('auto');
const detectedLanguage = ref('english');
// 统计信息
const stats = computed(() => {
const text = inputText.value || '';
// 字符数(含空格)
const charsWithSpaces = text.length;
// 字符数(不含空格)
const charsWithoutSpaces = text.replace(/\s/g, '').length;
// 行数
const lines = text ? text.split('\n').length : 0;
// 段落数
const paragraphs = text ? text.split(/\n\s*\n/).filter(p => p.trim() !== '').length : 0;
// 根据语言类型统计单词数
let words = 0;
let sentences = 0;
let chineseChars = 0;
if (language.value === 'chinese' || (language.value === 'auto' && detectLanguage(text) === 'chinese')) {
detectedLanguage.value = 'chinese';
// 中文文本:按词语和标点符号统计
// 统计汉字数量
chineseChars = (text.match(/[\u4e00-\u9fa5]/g) || []).length;
// 使用中文分词方法统计词语数量
// 这里简单地将中文文本按标点符号和空格分开,再统计词语
const chineseWords = text.replace(/[,。!?;:、\s]/g, ' ').trim().split(/\s+/).filter(w => w !== '');
words = chineseWords.length;
// 统计句子数(按中文标点符号)
sentences = (text.match(/[。!?]/g) || []).length;
} else {
detectedLanguage.value = 'english';
// 英文文本:按空格和标点符号统计
const englishWords = text.trim().split(/\s+/).filter(w => w !== '');
words = englishWords.length;
// 统计句子数(按英文标点符号)
sentences = (text.match(/[.!?]+/g) || []).length;
}
return {
charsWithSpaces,
charsWithoutSpaces,
words,
lines,
paragraphs,
sentences,
chineseChars
};
});
// 可读性等级
const readabilityLevel = computed(() => {
if (!inputText.value) return '无文本';
// 简化的可读性评估
const avgWordLength = stats.value.charsWithoutSpaces / (stats.value.words || 1);
const avgSentenceLength = stats.value.words / (stats.value.sentences || 1);
if (avgWordLength < 4 && avgSentenceLength < 10) {
return '小学水平';
} else if (avgWordLength < 5 && avgSentenceLength < 15) {
return '初中水平';
} else if (avgWordLength < 6 && avgSentenceLength < 20) {
return '高中水平';
} else {
return '大学及以上水平';
}
});
// 检测语言
function detectLanguage(text) {
// 简单的语言检测:如果中文字符占比超过20%,则认为是中文
const chineseChars = (text.match(/[\u4e00-\u9fa5]/g) || []).length;
const totalChars = text.length;
if (totalChars > 0 && chineseChars / totalChars > 0.2) {
return 'chinese';
}
return 'english';
}
// 分析文本
const analyzeText = () => {
// 当语言设置为自动检测时,检测文本语言
if (language.value === 'auto' && inputText.value) {
detectedLanguage.value = detectLanguage(inputText.value);
}
};
// 清空文本
const clearText = () => {
inputText.value = '';
};
// 示例文本
const sampleText = () => {
if (language.value === 'chinese' || (language.value === 'auto' && detectedLanguage.value === 'chinese')) {
inputText.value = `这是一个示例文本,用来演示单词计数器的功能。这个工具可以帮助您统计文本中的字符数、单词数、行数等信息。它支持中文和英文文本的统计,还提供了详细的分析功能。`;
} else {
inputText.value = `This is a sample text to demonstrate the word counter functionality. This tool can help you count characters, words, lines, and other information in your text. It supports both Chinese and English text statistics, and provides detailed analysis features.`;
}
};
// 复制统计信息
const copyStats = () => {
const info = `
字符数(含空格): ${stats.value.charsWithSpaces}
字符数(不含空格): ${stats.value.charsWithoutSpaces}
单词数: ${stats.value.words}
行数: ${stats.value.lines}
段落数: ${stats.value.paragraphs}
句子数: ${stats.value.sentences}
汉字数: ${stats.value.chineseChars}
可读性等级: ${readabilityLevel.value}
`.trim();
navigator.clipboard.writeText(info)
.then(() => {
alert('统计信息已复制到剪贴板!');
})
.catch(err => {
console.error('复制失败: ', err);
alert('复制失败');
});
};
return {
inputText,
language,
detectedLanguage,
stats,
readabilityLevel,
analyzeText,
clearText,
sampleText,
copyStats
};
}
}).mount('#app');
</script>
</body>
</html>