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
// 自然语言处理高级(v49.0.0)
// 文本分类、命名实体识别、情感分析、文本摘要、机器翻译、问答系统
///|
/// NLP任务类型
pub(all) enum NLPTaskType {
TextClassification
NamedEntityRecognition
SentimentAnalysis
TextSummarization
MachineTranslation
QuestionAnswering
TextGeneration
LanguageModeling
Unknown
} derive(Eq)
///|
/// 文本分类模型类型
pub(all) enum TextClassificationModel {
NaiveBayes
SVM
LogisticRegression
CNN
RNN
LSTM
GRU
Transformer
BERT
RoBERTa
Unknown
} derive(Eq)
///|
/// NER实体类型
pub(all) enum NEREntityType {
Person
Organization
Location
Date
Time
Money
Percent
Product
Event
Unknown
} derive(Eq)
///|
/// 情感类型
pub(all) enum SentimentType {
VeryPositive
Positive
Neutral
Negative
VeryNegative
Unknown
} derive(Eq)
///|
/// 文本分类结果
pub(all) struct TextClassificationResult {
text : String
predicted_class : String
confidence : Double
all_classes : Array[String]
all_scores : Array[Double]
model_type : TextClassificationModel
num_classes : Int
}
///|
/// NER结果
pub(all) struct NERResult {
text : String
entities : Array[String]
entity_types : Array[NEREntityType]
entity_spans : Array[Array[Int]]
confidences : Array[Double]
num_entities : Int
}
///|
/// 情感分析结果
pub(all) struct SentimentAnalysisResult {
text : String
sentiment : SentimentType
sentiment_score : Double
confidence : Double
positive_score : Double
negative_score : Double
neutral_score : Double
aspect_sentiments : Array[String]
}
///|
/// 文本摘要结果
pub(all) struct TextSummarizationResult {
original_text : String
summary : String
compression_ratio : Double
original_length : Int
summary_length : Int
method_type : String
rouge_score : Double
}
///|
/// 机器翻译结果
pub(all) struct MachineTranslationResult {
source_text : String
target_text : String
source_language : String
target_language : String
confidence : Double
bleu_score : Double
method_type : String
}
///|
/// 问答系统结果
pub(all) struct QuestionAnsweringResult {
question : String
context : String
answer : String
confidence : Double
answer_start : Int
answer_end : Int
method_type : String
}
///|
/// 文本嵌入
pub(all) struct TextEmbedding {
text : String
embedding : Array[Double]
embedding_dim : Int
model_type : String
token_ids : Array[Int]
num_tokens : Int
}
///|
/// NLP配置
pub(all) struct NLPConfig {
task_type : NLPTaskType
classification_model : TextClassificationModel
max_length : Int
batch_size : Int
learning_rate : Double
num_epochs : Int
use_pretrained : Bool
pretrained_model : String
temperature : Double
top_p : Double
top_k : Int
num_beams : Int
}
///|
/// 创建默认NLP配置
pub fn create_default_nlp_config() -> NLPConfig {
NLPConfig::{
task_type: NLPTaskType::TextClassification,
classification_model: TextClassificationModel::BERT,
max_length: 512,
batch_size: 32,
learning_rate: 0.00002,
num_epochs: 3,
use_pretrained: true,
pretrained_model: "bert-base-uncased",
temperature: 1.0,
top_p: 1.0,
top_k: 50,
num_beams: 4,
}
}
///|
/// 文本分词(简化版)
pub fn tokenize(text : String) -> Array[String] {
let tokens : Array[String] = []
// 简化的分词:按空格分割
let parts = text.split(" ")
for part in parts {
let part_str = part.to_owned()
if part_str.length() > 0 {
tokens.push(part_str)
}
}
tokens
}
///|
/// 文本分类(简化版)
pub fn classify_text(
text : String,
classes : Array[String],
config : NLPConfig,
) -> TextClassificationResult {
let tokens = tokenize(text)
let all_scores : Array[Double] = []
// 简化的分类:基于关键词匹配
let keywords = [
["happy", "good", "great", "excellent", "love"],
["sad", "bad", "terrible", "awful", "hate"],
["neutral", "okay", "fine", "average", "normal"],
]
for class_idx in 0..<classes.length() {
let mut score = 0.0
for token in tokens {
let token_lower = token
if class_idx < keywords.length() {
for kw in keywords[class_idx] {
if token_lower.contains(kw) {
score += 1.0
}
}
}
}
if tokens.length() > 0 {
score /= tokens.length().to_double()
}
all_scores.push(score)
}
// 归一化
let mut total_score = 0.0
for s in all_scores {
total_score += s
}
if total_score > 0.0 {
for i in 0..<all_scores.length() {
all_scores[i] /= total_score
}
}
// 找到最佳类别
let mut best_class = classes[0]
let mut best_score = 0.0
for i in 0..<classes.length() {
if all_scores[i] > best_score {
best_score = all_scores[i]
best_class = classes[i]
}
}
TextClassificationResult::{
text,
predicted_class: best_class,
confidence: best_score,
all_classes: classes,
all_scores,
model_type: config.classification_model,
num_classes: classes.length(),
}
}
///|
/// 命名实体识别(简化版)
pub fn recognize_entities(text : String) -> NERResult {
let entities : Array[String] = []
let entity_types : Array[NEREntityType] = []
let entity_spans : Array[Array[Int]] = []
let confidences : Array[Double] = []
let tokens = tokenize(text)
let mut position = 0
for token in tokens {
let token_len = token.length()
let start = position
let end = position + token_len
// 简化的实体识别:基于首字母大写
let mut is_entity = false
let mut entity_type = NEREntityType::Unknown
if token.length() > 0 {
// 简化的首字母大写判断
if token.length() > 1 {
is_entity = true
// 简化的实体类型判断
if token.contains("Inc") ||
token.contains("Corp") ||
token.contains("Company") {
entity_type = NEREntityType::Organization
} else if token.contains("Street") ||
token.contains("Avenue") ||
token.contains("City") {
entity_type = NEREntityType::Location
} else {
entity_type = NEREntityType::Person
}
}
}
if is_entity {
entities.push(token)
entity_types.push(entity_type)
let span : Array[Int] = []
span.push(start)
span.push(end)
entity_spans.push(span)
confidences.push(0.7)
}
position = end + 1
}
NERResult::{
text,
entities,
entity_types,
entity_spans,
confidences,
num_entities: entities.length(),
}
}
///|
/// 情感分析(简化版)
pub fn analyze_sentiment(text : String) -> SentimentAnalysisResult {
let tokens = tokenize(text)
let positive_words = [
"good", "great", "excellent", "love", "happy", "amazing", "wonderful", "fantastic",
"best", "perfect",
]
let negative_words = [
"bad", "terrible", "awful", "hate", "sad", "horrible", "worst", "poor", "disappointing",
"ugly",
]
let mut positive_count = 0
let mut negative_count = 0
for token in tokens {
let token_lower = token
for pw in positive_words {
if token_lower.contains(pw) {
positive_count += 1
}
}
for nw in negative_words {
if token_lower.contains(nw) {
negative_count += 1
}
}
}
let total = positive_count + negative_count
let positive_score = if total > 0 {
positive_count.to_double() / total.to_double()
} else {
0.5
}
let negative_score = if total > 0 {
negative_count.to_double() / total.to_double()
} else {
0.5
}
let neutral_score = 1.0 - positive_score - negative_score
let sentiment_score = positive_score - negative_score
let sentiment = if sentiment_score > 0.5 {
SentimentType::VeryPositive
} else if sentiment_score > 0.1 {
SentimentType::Positive
} else if sentiment_score > -0.1 {
SentimentType::Neutral
} else if sentiment_score > -0.5 {
SentimentType::Negative
} else {
SentimentType::VeryNegative
}
let confidence = if total > 0 {
(positive_score - negative_score).abs()
} else {
0.3
}
SentimentAnalysisResult::{
text,
sentiment,
sentiment_score,
confidence,
positive_score,
negative_score,
neutral_score,
aspect_sentiments: [],
}
}
///|
/// 文本摘要(简化版:提取式摘要)
pub fn summarize_text(
text : String,
max_sentences : Int,
) -> TextSummarizationResult {
// 简化的句子分割:按句号分割
let sentences : Array[String] = []
let parts = text.split(".")
for part in parts {
let current_sentence = part.to_owned() + "."
if current_sentence.length() > 10 {
sentences.push(current_sentence)
}
}
// 简化的句子评分:基于长度
let scored_sentences : Array[(Double, String)] = []
for sentence in sentences {
let score = sentence.length().to_double()
scored_sentences.push((score, sentence))
}
// 选择Top-N句子
let num_selected = if max_sentences < sentences.length() {
max_sentences
} else {
sentences.length()
}
let mut summary = ""
for i in 0..<num_selected {
summary = summary + scored_sentences[i].1
}
let original_length = text.length()
let summary_length = summary.length()
let compression_ratio = if original_length > 0 {
1.0 - summary_length.to_double() / original_length.to_double()
} else {
0.0
}
TextSummarizationResult::{
original_text: text,
summary,
compression_ratio,
original_length,
summary_length,
method_type: "extractive",
rouge_score: 0.5,
}
}
///|
/// 生成文本嵌入(简化版)
pub fn embed_text(text : String, embedding_dim : Int) -> TextEmbedding {
let tokens = tokenize(text)
let embedding : Array[Double] = []
let token_ids : Array[Int] = []
let mut state = 42
for _ in 0..<embedding_dim {
state = (state * 11035 + 12345) % 1000000
embedding.push((state % 1000).to_double() / 1000.0 - 0.5)
}
for _ in tokens {
state = (state * 11035 + 12345) % 1000000
token_ids.push(state % 30000)
}
TextEmbedding::{
text,
embedding,
embedding_dim,
model_type: "word2vec",
token_ids,
num_tokens: tokens.length(),
}
}
///|
/// 生成NLP报告
pub fn nlp_report_to_string(
task_type : NLPTaskType,
config : NLPConfig,
result_description : String,
) -> String {
let mut report = "## 自然语言处理高级报告\n\n"
report = report + "### 任务信息\n\n"
report = report + "| 指标 | 值 |\n|------|------|\n"
report = report +
"| 任务类型 | " +
(match task_type {
NLPTaskType::TextClassification => "文本分类"
NLPTaskType::NamedEntityRecognition => "命名实体识别"
NLPTaskType::SentimentAnalysis => "情感分析"
NLPTaskType::TextSummarization => "文本摘要"
NLPTaskType::MachineTranslation => "机器翻译"
NLPTaskType::QuestionAnswering => "问答系统"
NLPTaskType::TextGeneration => "文本生成"
_ => "未知"
}) +
" |\n"
report = report +
"| 分类模型 | " +
(match config.classification_model {
TextClassificationModel::NaiveBayes => "朴素贝叶斯"
TextClassificationModel::SVM => "SVM"
TextClassificationModel::CNN => "CNN"
TextClassificationModel::RNN => "RNN"
TextClassificationModel::LSTM => "LSTM"
TextClassificationModel::Transformer => "Transformer"
TextClassificationModel::BERT => "BERT"
TextClassificationModel::RoBERTa => "RoBERTa"
_ => "未知"
}) +
" |\n"
report = report + "| 最大长度 | " + config.max_length.to_string() + " |\n"
report = report + "| 批次大小 | " + config.batch_size.to_string() + " |\n"
report = report + "| 学习率 | " + config.learning_rate.to_string() + " |\n"
report = report + "| 训练轮数 | " + config.num_epochs.to_string() + " |\n"
report = report + "| 预训练模型 | " + config.pretrained_model + " |\n"
report = report + "\n### 结果\n\n"
report = report + result_description + "\n"
report
}