-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
692 lines (599 loc) · 29.1 KB
/
Copy pathindex.html
File metadata and controls
692 lines (599 loc) · 29.1 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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>现代诗生成器</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Microsoft YaHei', sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
padding: 20px;
}
.container {
max-width: 1200px;
margin: 0 auto;
background: white;
border-radius: 20px;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
overflow: hidden;
}
.header {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 40px;
text-align: center;
}
.header h1 {
font-size: 2.5em;
margin-bottom: 10px;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);
}
.header p {
font-size: 1.1em;
opacity: 0.9;
}
.content {
padding: 40px;
}
.section {
margin-bottom: 40px;
}
.section h2 {
color: #333;
margin-bottom: 20px;
font-size: 1.5em;
border-bottom: 3px solid #667eea;
padding-bottom: 10px;
}
textarea {
width: 100%;
min-height: 150px;
padding: 15px;
border: 2px solid #e0e0e0;
border-radius: 10px;
font-size: 16px;
resize: vertical;
transition: border-color 0.3s;
}
textarea:focus {
outline: none;
border-color: #667eea;
}
.input-group {
margin-bottom: 20px;
}
.input-group label {
display: block;
margin-bottom: 8px;
color: #555;
font-weight: bold;
}
input[type="text"] {
width: 100%;
padding: 12px;
border: 2px solid #e0e0e0;
border-radius: 10px;
font-size: 16px;
transition: border-color 0.3s;
}
input[type="text"]:focus {
outline: none;
border-color: #667eea;
}
.button {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
padding: 15px 40px;
font-size: 18px;
border-radius: 30px;
cursor: pointer;
transition: transform 0.3s, box-shadow 0.3s;
box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
}
.button:hover {
transform: translateY(-2px);
box-shadow: 0 6px 20px rgba(102, 126, 234, 0.6);
}
.button:active {
transform: translateY(0);
}
.result-box {
background: #f8f9fa;
border-radius: 15px;
padding: 30px;
margin-top: 20px;
min-height: 200px;
white-space: pre-wrap;
line-height: 1.8;
font-size: 1.1em;
color: #333;
border-left: 5px solid #667eea;
}
.style-badge {
display: inline-block;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 8px 20px;
border-radius: 20px;
font-size: 14px;
margin-top: 10px;
}
.loading {
text-align: center;
color: #667eea;
font-size: 1.2em;
padding: 40px;
}
.loading::after {
content: '...';
animation: dots 1.5s steps(4, end) infinite;
}
@keyframes dots {
0%, 20% { content: '.'; }
40% { content: '..'; }
60%, 100% { content: '...'; }
}
.error {
background: #fee;
color: #c33;
padding: 15px;
border-radius: 10px;
margin-top: 20px;
border-left: 5px solid #c33;
}
.keyword-tags {
display: flex;
flex-wrap: wrap;
gap: 10px;
margin-top: 10px;
}
.keyword-tag {
background: #e0e7ff;
color: #4338ca;
padding: 8px 16px;
border-radius: 20px;
font-size: 14px;
display: flex;
align-items: center;
gap: 8px;
}
.keyword-tag .remove {
cursor: pointer;
font-weight: bold;
color: #6366f1;
}
.keyword-tag .remove:hover {
color: #4338ca;
}
@media (max-width: 768px) {
.header h1 {
font-size: 1.8em;
}
.content {
padding: 20px;
}
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>✨ 现代诗生成器</h1>
<p>上传文字识别风格,输入关键词生成诗歌</p>
</div>
<div class="content">
<div class="section">
<h2>📝 风格识别</h2>
<div class="input-group">
<label>上传参考文本(用于识别诗歌风格)</label>
<textarea id="referenceText" placeholder="在这里粘贴一段现代诗或文字,系统将分析其风格..."></textarea>
</div>
<button class="button" onclick="analyzeStyle()">识别风格</button>
<div id="styleResult"></div>
</div>
<div class="section">
<h2>🎨 诗歌生成</h2>
<div class="input-group">
<label>输入关键词(用逗号分隔)</label>
<input type="text" id="keywords" placeholder="例如:星空, 梦想, 远方" onkeypress="handleKeyPress(event)">
</div>
<div id="keywordTags" class="keyword-tags"></div>
<button class="button" onclick="generatePoem()">生成诗歌</button>
<div id="poemResult"></div>
</div>
</div>
</div>
<script>
let detectedStyle = '现代';
let keywords = [];
// 风格分析器
class PoetryStyleAnalyzer {
constructor() {
this.style_keywords = {
'浪漫': ['爱', '梦', '情', '心', '花', '月', '星', '恋', '思念', '温柔', '甜蜜', '浪漫'],
'忧郁': ['泪', '痛', '愁', '悲', '伤', '孤', '寂', '暗', '寒', '绝望', '哀愁', '凄凉'],
'哲理': ['生命', '时间', '存在', '意义', '真理', '永恒', '思考', '智慧', '感悟', '境界'],
'自然': ['山', '水', '风', '雨', '云', '海', '树', '草', '天空', '大地', '阳光', '四季'],
'现代': ['城市', '街道', '霓虹', '钢铁', '速度', '网络', '科技', '现代', '节奏', '喧嚣']
};
}
analyze_style(text) {
const words = this._simple_tokenize(text);
const style_scores = {};
for (const [style, keywords] of Object.entries(this.style_keywords)) {
const score = keywords.reduce((acc, keyword) => {
return acc + words.filter(word => word === keyword).length;
}, 0);
style_scores[style] = score;
}
let max_style = Object.keys(style_scores).reduce((a, b) =>
style_scores[a] > style_scores[b] ? a : b
);
if (style_scores[max_style] === 0) {
max_style = '现代';
}
return {
'style': max_style,
'scores': style_scores
};
}
_simple_tokenize(text) {
const words = [];
for (const char of text) {
if (char >= '\u4e00' && char <= '\u9fff') {
words.push(char);
}
}
return words;
}
}
// 诗歌生成器
class PoetryGenerator {
constructor() {
this.templates = {
'浪漫': [
'当{keyword1}温柔地漫过{keyword2},我在{keyword3}中找到了永恒的坐标',
'{keyword1}的呼吸是{keyword2}的私语,在{keyword3}的夜晚轻轻荡漾成涟漪',
'我将{keyword1}编织成{keyword2},挂在{keyword3}的窗前照亮每个黎明',
'{keyword1}是{keyword2}的眼睛,在{keyword3}的深海中闪烁着未说出口的秘密',
'当{keyword1}与{keyword2}相遇,{keyword3}便成了时光最美的注脚',
'我在{keyword1}的缝隙里,收藏着{keyword2}的{keyword3}碎片',
'{keyword1}的温度,是{keyword2}给{keyword3}写了一辈子的情书',
'在{keyword1}的温柔里,{keyword2}找到了{keyword3}的方向',
'当{keyword1}落进{keyword2}的怀抱,{keyword3}便成了唯一的信仰',
'{keyword1}是{keyword2}的翅膀,带着{keyword3}飞向未名的远方'
],
'忧郁': [
'{keyword1}的影子在{keyword2}里沉淀,{keyword3}是唯一穿透黑暗的光',
'当{keyword1}凋零在{keyword2}的角落,{keyword3}成了未完成的诗稿',
'我在{keyword1}的废墟里,寻找{keyword2}遗落的{keyword3}碎片',
'{keyword1}是{keyword2}的伤口,{keyword3}是愈合时撒下的盐',
'当{keyword1}漫过{keyword2}的边界,{keyword3}成了最远的距离',
'我将{keyword1}写成{keyword2},却读不懂{keyword3}的结局',
'{keyword1}的夜晚,{keyword2}是{keyword3}的回声在空谷里回荡',
'在{keyword1}的雨季里,{keyword2}成了{keyword3}的墓碑',
'当{keyword1}熄灭在{keyword2}的掌心里,{keyword3}成了最后的余烬',
'{keyword1}是{keyword2}的枷锁,困住了{keyword3}的自由'
],
'哲理': [
'{keyword1}不是终点,而是{keyword2}的另一种{keyword3}形式',
'当{keyword1}穿过{keyword2}的裂缝,{keyword3}便有了重量',
'我在{keyword1}的碎片里,拼凑{keyword2}的{keyword3}拼图',
'{keyword1}是{keyword2}的镜子,照见{keyword3}的本相',
'当{keyword1}与{keyword2}对饮,{keyword3}便成了下酒菜和话题',
'我将{keyword1}折叠成{keyword2},藏在{keyword3}的褶皱里',
'{keyword1}的意义,在于{keyword2}对{keyword3}的诠释',
'在{keyword1}的长河里,{keyword2}是{keyword3}的标点',
'当{keyword1}沉淀为{keyword2},{keyword3}便成了智慧的结晶',
'{keyword1}是{keyword2}的钥匙,打开{keyword3}的门'
],
'自然': [
'{keyword1}在{keyword2}的怀里醒来,{keyword3}是它的第一个吻',
'当{keyword1}与{keyword2}对话,{keyword3}便成了最忠实的旁听者',
'我在{keyword1}的呼吸里,触摸{keyword2}的{keyword3}脉动',
'{keyword1}是{keyword2}的画笔,在{keyword3}的画布上涂鸦出四季',
'当{keyword1}漫过{keyword2}的河床,{keyword3}便成了流动的诗',
'我将{keyword1}的声音,藏在{keyword2}的{keyword3}里',
'{keyword1}的节奏,是{keyword2}给{keyword3}的心跳',
'在{keyword1}的季节里,{keyword2}是{keyword3}的使者',
'当{keyword1}拥抱{keyword2},{keyword3}便充满了生机',
'{keyword1}是{keyword2}的摇篮,摇晃着{keyword3}的梦境'
],
'现代': [
'{keyword1}在{keyword2}的屏幕里闪烁,{keyword3}是未读的消息',
'当{keyword1}穿过{keyword2}的街道,{keyword3}成了城市的注脚',
'我在{keyword1}的代码里,寻找{keyword2}的{keyword3}算法',
'{keyword1}是{keyword2}的滤镜,在{keyword3}的镜头里失真成艺术',
'当{keyword1}与{keyword2}碰撞,{keyword3}便成了新的语言',
'我将{keyword1}打包成{keyword2},发送到{keyword3}的服务器',
'{keyword1}的速度,是{keyword2}对{keyword3}的注解',
'在{keyword1}的算法里,{keyword2}是{keyword3}的变量',
'当{keyword1}连接{keyword2},{keyword3}便成了网络的节点',
'{keyword1}是{keyword2}的像素,构成{keyword3}的虚拟世界'
]
};
this.modifiers = {
'浪漫': ['温柔的', '甜蜜的', '梦幻的', '朦胧的', '炽热的', '柔软的', '深情的', '轻盈的', '璀璨的', '温暖的'],
'忧郁': ['孤独的', '深邃的', '沉默的', '沉重的', '潮湿的', '破碎的', '暗淡的', '漫长的', '苍凉的', '忧伤的'],
'哲理': ['深刻的', '智慧的', '永恒的', '微妙的', '清晰的', '抽象的', '本质的', '真实的', '理性的', '思辨的'],
'自然': ['清新的', '生机的', '纯净的', '辽阔的', '宁静的', '自由的', '和谐的', '原始的', '灵动的', '自然的'],
'现代': ['快速的', '虚拟的', '数字的', '碎片的', '流动的', '多元的', '科技的', '都市的', '智能的', '互联的']
};
this.structures = [
'stanzas',
'free_verse',
'haiku_style',
'prose_poem',
'sonnet_style'
];
this.openings = {
'浪漫': ['在某个月光漫洒的夜晚,', '当风穿过指尖的瞬间,', '我站在时间的渡口,', '在梦与现实的交界处,'],
'忧郁': ['雨打在窗棂的时刻,', '当最后一片叶子坠落,', '我在记忆的迷宫里,', '深夜的街头空无一人,'],
'哲理': ['站在人生的十字路口,', '当思考成为一种习惯,', '我在时光的长河里,', '透过现象看本质,'],
'自然': ['清晨的第一缕阳光,', '当鸟儿在枝头欢唱,', '我漫步在林间小道,', '大自然的呼吸里,'],
'现代': ['城市的霓虹开始闪烁,', '当网络连接整个世界,', '我在数字的海洋里,', '科技改变生活的瞬间,']
};
this.endings = {
'浪漫': ['爱,是永恒的主题。', '在彼此的眼中,找到回家的路。', '时光不老,我们不散。', '爱是最美丽的诗。'],
'忧郁': ['也许,这就是人生。', '孤独,是最忠实的伴侣。', '有些故事,注定没有结局。', '雨停了,心却还在潮湿。'],
'哲理': ['答案,在每个人的心中。', '思考,让生命更有意义。', '真理,往往藏在平凡中。', '智慧,是时间的礼物。'],
'自然': ['大自然,是最好的老师。', '在自然中,找到内心的宁静。', '生命,在循环中永恒。', '自然的美,无需雕琢。'],
'现代': ['科技,让生活更精彩。', '在数字时代,寻找真实的自己。', '网络连接你我,世界不再遥远。', '现代生活,是一首快节奏的诗。']
};
}
generate(style, keywords) {
if (!this.templates[style]) {
style = '现代';
}
const structure = this.structures[Math.floor(Math.random() * this.structures.length)];
if (structure === 'haiku_style') {
return this._generate_haiku(style, keywords);
} else if (structure === 'prose_poem') {
return this._generate_prose_poem(style, keywords);
} else if (structure === 'sonnet_style') {
return this._generate_sonnet(style, keywords);
} else {
return this._generate_regular_poem(style, keywords);
}
}
_generate_regular_poem(style, keywords) {
const templates = this.templates[style];
const lines = [];
if (Math.random() > 0.3 && this.openings[style]) {
const opening = this.openings[style][Math.floor(Math.random() * this.openings[style].length)];
lines.push(opening.trim());
}
if (Math.random() > 0.5) {
const stanzas = Math.floor(Math.random() * 2) + 2;
const lines_per_stanza = Math.floor(Math.random() * 3) + 2;
for (let i = 0; i < stanzas; i++) {
const stanza_lines = [];
for (let j = 0; j < lines_per_stanza; j++) {
const line = this._generate_line(style, keywords, templates);
stanza_lines.push(line.trim());
}
lines.push(...stanza_lines);
lines.push('');
}
} else {
const num_lines = Math.floor(Math.random() * 5) + 4;
for (let i = 0; i < num_lines; i++) {
const line = this._generate_line(style, keywords, templates);
lines.push(line.trim());
}
}
if (Math.random() > 0.3 && this.endings[style]) {
const ending = this.endings[style][Math.floor(Math.random() * this.endings[style].length)];
lines.push(ending.trim());
}
return lines.filter(Boolean).join('\n');
}
_generate_haiku(style, keywords) {
const templates = this.templates[style];
const lines = [];
const line1 = this._generate_line(style, keywords, templates);
lines.push((line1.length > 12 ? line1.substring(0, 12) + '...' : line1).trim());
const line2 = this._generate_line(style, keywords, templates);
lines.push((line2.length > 18 ? line2.substring(0, 18) + '...' : line2).trim());
const line3 = this._generate_line(style, keywords, templates);
lines.push((line3.length > 12 ? line3.substring(0, 12) + '...' : line3).trim());
return lines.join('\n');
}
_generate_prose_poem(style, keywords) {
const templates = this.templates[style];
const sentences = [];
if (this.openings[style]) {
const opening = this.openings[style][Math.floor(Math.random() * this.openings[style].length)];
sentences.push(opening.trim());
}
const num_sentences = Math.floor(Math.random() * 3) + 4;
for (let i = 0; i < num_sentences; i++) {
const sentence = this._generate_line(style, keywords, templates);
sentences.push(sentence.trim());
}
if (this.endings[style]) {
const ending = this.endings[style][Math.floor(Math.random() * this.endings[style].length)];
sentences.push(ending.trim());
}
let prose = sentences.join(' ');
if (!prose.endsWith('。')) {
prose += '。';
}
return prose.trim();
}
_generate_sonnet(style, keywords) {
const templates = this.templates[style];
const lines = [];
for (let i = 0; i < 8; i++) {
const line = this._generate_line(style, keywords, templates);
lines.push(line.trim());
}
lines.push('');
for (let i = 0; i < 6; i++) {
const line = this._generate_line(style, keywords, templates);
lines.push(line.trim());
}
if (this.endings[style]) {
const ending = this.endings[style][Math.floor(Math.random() * this.endings[style].length)];
lines.push(ending.trim());
}
return lines.filter(Boolean).join('\n');
}
_generate_line(style, keywords, templates) {
const template = templates[Math.floor(Math.random() * templates.length)];
let available_keywords = [...keywords];
available_keywords.sort(() => Math.random() - 0.5);
// 当关键词不足时,使用相关词汇补充
const related_words = {
'浪漫': ['爱情', '梦想', '星空', '温柔', '时光', '思念', '心跳', '呼吸', '月光', '花朵'],
'忧郁': ['雨', '孤独', '记忆', '夜晚', '影子', '废墟', '伤口', '余烬', '沉默', '苍凉'],
'哲理': ['时间', '生命', '意义', '真理', '永恒', '思考', '智慧', '本质', '存在', '价值'],
'自然': ['山', '水', '风', '云', '海', '树', '草', '天空', '大地', '阳光'],
'现代': ['城市', '网络', '科技', '屏幕', '街道', '代码', '速度', '算法', '像素', '连接']
};
// 确保有足够的关键词,避免重复使用同一个词
while (available_keywords.length < 3) {
if (related_words[style]) {
const related = related_words[style];
const random_word = related[Math.floor(Math.random() * related.length)];
if (!available_keywords.includes(random_word)) {
available_keywords.push(random_word);
}
} else {
// 如果没有相关词汇,使用通用词汇
const common_words = ['世界', '时光', '梦想', '生命', '希望', '未来', '回忆', '希望'];
const random_word = common_words[Math.floor(Math.random() * common_words.length)];
if (!available_keywords.includes(random_word)) {
available_keywords.push(random_word);
}
}
}
let keyword1 = available_keywords[0] || '世界';
let keyword2 = available_keywords[1] || '时光';
let keyword3 = available_keywords[2] || '梦想';
// 确保关键词不重复
if (keyword1 === keyword2) {
keyword2 = related_words[style] ? related_words[style][Math.floor(Math.random() * related_words[style].length)] : '时光';
}
if (keyword1 === keyword3 || keyword2 === keyword3) {
keyword3 = related_words[style] ? related_words[style][Math.floor(Math.random() * related_words[style].length)] : '梦想';
}
if (Math.random() > 0.4 && this.modifiers[style]) {
if (Math.random() > 0.5) {
const modifier1 = this.modifiers[style][Math.floor(Math.random() * this.modifiers[style].length)];
keyword1 = modifier1 + keyword1;
}
if (Math.random() > 0.5) {
const modifier2 = this.modifiers[style][Math.floor(Math.random() * this.modifiers[style].length)];
keyword2 = modifier2 + keyword2;
}
if (Math.random() > 0.5) {
const modifier3 = this.modifiers[style][Math.floor(Math.random() * this.modifiers[style].length)];
keyword3 = modifier3 + keyword3;
}
}
return template.replace('{keyword1}', keyword1)
.replace('{keyword2}', keyword2)
.replace('{keyword3}', keyword3);
}
}
// 初始化分析器和生成器
const analyzer = new PoetryStyleAnalyzer();
const generator = new PoetryGenerator();
async function analyzeStyle() {
const text = document.getElementById('referenceText').value.trim();
const resultDiv = document.getElementById('styleResult');
if (!text) {
resultDiv.innerHTML = '<div class="error">请输入参考文本</div>';
return;
}
resultDiv.innerHTML = '<div class="loading">正在分析风格</div>';
try {
const result = analyzer.analyze_style(text);
detectedStyle = result.style;
resultDiv.innerHTML = `
<div class="result-box">
<strong>识别结果:</strong><br><br>
检测到的风格:<span class="style-badge">${result.style}</span><br><br>
各风格得分:<br>
${Object.entries(result.scores).map(([style, score]) =>
`${style}:${score}分`
).join('<br>')}
</div>
`;
} catch (error) {
resultDiv.innerHTML = `<div class="error">分析失败:${error.message}</div>`;
}
}
function handleKeyPress(event) {
if (event.key === 'Enter') {
event.preventDefault();
addKeyword();
}
}
function addKeyword() {
const input = document.getElementById('keywords');
const keyword = input.value.trim();
if (keyword && !keywords.includes(keyword)) {
keywords.push(keyword);
updateKeywordTags();
}
input.value = '';
}
function removeKeyword(index) {
keywords.splice(index, 1);
updateKeywordTags();
}
function updateKeywordTags() {
const tagsDiv = document.getElementById('keywordTags');
tagsDiv.innerHTML = keywords.map((keyword, index) =>
`<div class="keyword-tag">
${keyword}
<span class="remove" onclick="removeKeyword(${index})">×</span>
</div>`
).join('');
}
async function generatePoem() {
const resultDiv = document.getElementById('poemResult');
// 先处理输入框中的关键词
const input = document.getElementById('keywords');
const inputText = input.value.trim();
if (inputText) {
// 支持逗号分隔的多个关键词
const inputKeywords = inputText.split(',').map(k => k.trim()).filter(k => k);
for (const keyword of inputKeywords) {
if (!keywords.includes(keyword)) {
keywords.push(keyword);
}
}
updateKeywordTags();
input.value = '';
}
if (keywords.length === 0) {
resultDiv.innerHTML = '<div class="error">请至少输入一个关键词</div>';
return;
}
resultDiv.innerHTML = '<div class="loading">正在生成诗歌</div>';
try {
const poem = generator.generate(detectedStyle, keywords);
resultDiv.innerHTML = `
<div class="result-box">
<strong>生成风格:</strong><span class="style-badge">${detectedStyle}</span><br><br>
${poem}
</div>
`;
} catch (error) {
resultDiv.innerHTML = `<div class="error">生成失败:${error.message}</div>`;
}
}
</script>
</body>
</html>