|
@@ -5,6 +5,15 @@ import { IonCard, IonItem, IonLabel, IonList } from '@ionic/angular/standalone';
|
|
|
import { IonButton, IonButtons, IonContent, IonHeader, IonTitle, IonToolbar, ModalController } from '@ionic/angular/standalone';
|
|
|
import { CloudObject, CloudQuery } from 'src/lib/ncloud';
|
|
|
|
|
|
+// 定义 ContentItem 接口
|
|
|
+interface ContentItem {
|
|
|
+ role: string;
|
|
|
+ content: string;
|
|
|
+ createdAt?: string; // 可选属性
|
|
|
+ hidden?: boolean; // 可选属性
|
|
|
+ complete?: boolean; // 可选属性
|
|
|
+ cid?: string; // 可选属性
|
|
|
+}
|
|
|
@Component({
|
|
|
selector: 'app-report-modal',
|
|
|
templateUrl: './report-modal.component.html',
|
|
@@ -17,6 +26,7 @@ import { CloudObject, CloudQuery } from 'src/lib/ncloud';
|
|
|
export class ReportModalComponent implements OnInit {
|
|
|
|
|
|
constructor(private modalCtrl: ModalController,private router: Router) {}
|
|
|
+ userFeelings: Array<{ content: string, score: number, label: string }> = []; // 存储用户内容和情感分析结果
|
|
|
closeModal() {
|
|
|
this.router.navigate(['tabs/tab3']);
|
|
|
}
|
|
@@ -24,9 +34,69 @@ export class ReportModalComponent implements OnInit {
|
|
|
async loadChatRecordList(){
|
|
|
let query = new CloudQuery("ChatRecord");
|
|
|
this.chatrecordList = await query.find()
|
|
|
-
|
|
|
+ // 遍历 chatrecordList,去除每个记录中 user 的第一条 content
|
|
|
+ this.chatrecordList.forEach(chatrecord => {
|
|
|
+ const contentArray = chatrecord.get('content');
|
|
|
+ if (contentArray && contentArray.length > 0) {
|
|
|
+ // 找到第一条 role 为 user 的内容并移除
|
|
|
+ const userIndex = contentArray.findIndex((item: ContentItem) => item.role === 'user');
|
|
|
+ if (userIndex !== -1) {
|
|
|
+ contentArray.splice(userIndex, 1); // 移除第一条 user 内容
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ analyzeUserFeeling(content: string): { score: number; label: string } {
|
|
|
+ const moodScores = {
|
|
|
+ positive: {
|
|
|
+ words: ['开心', '高兴', '喜欢', '快乐', '棒', '兴奋', '满足', '美好'],
|
|
|
+ score: 2
|
|
|
+ },
|
|
|
+ neutral: {
|
|
|
+ words: ['一般', '还好', '可以', '无所谓', '正常'],
|
|
|
+ score: 0
|
|
|
+ },
|
|
|
+ negative: {
|
|
|
+ words: ['难过', '生气', '失望', '讨厌', '悲伤', '烦恼', '痛苦', '沮丧'],
|
|
|
+ score: -2
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ let totalScore = 0;
|
|
|
+
|
|
|
+ // 分析内容并计算分数
|
|
|
+ const contentWords = content.split(/\s+/);
|
|
|
+ contentWords.forEach(word => {
|
|
|
+ for (const mood in moodScores) {
|
|
|
+ if ((moodScores as any)[mood].words.includes(word)) {
|
|
|
+ totalScore += (moodScores as any)[mood].score;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ // 判断心情状态
|
|
|
+ let label = '中性';
|
|
|
+ if (totalScore > 0) {
|
|
|
+ label = '积极';
|
|
|
+ } else if (totalScore < 0) {
|
|
|
+ label = '消极';
|
|
|
+ }
|
|
|
+
|
|
|
+ return { score: totalScore, label };
|
|
|
}
|
|
|
- ngOnInit() {
|
|
|
- this.loadChatRecordList()
|
|
|
+
|
|
|
+
|
|
|
+ async ngOnInit() {
|
|
|
+ await this.loadChatRecordList();
|
|
|
+ // 在这里可以遍历 chatrecordList 中的用户内容并进行情感分析
|
|
|
+ this.chatrecordList.forEach(chatrecord => {
|
|
|
+ const contentArray = chatrecord.get('content');
|
|
|
+ contentArray.forEach((contentItem: ContentItem) => {
|
|
|
+ if (contentItem.role === 'user') {
|
|
|
+ const analysis = this.analyzeUserFeeling(contentItem.content);
|
|
|
+ this.userFeelings.push({ content: contentItem.content, score: analysis.score, label: analysis.label });
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
}
|
|
|
}
|