report-modal.component.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { CommonModule } from '@angular/common';
  2. import { Component, OnInit } from '@angular/core';
  3. import { Router } from '@angular/router';
  4. import { IonCard, IonItem, IonLabel, IonList } from '@ionic/angular/standalone';
  5. import { IonButton, IonButtons, IonContent, IonHeader, IonTitle, IonToolbar, ModalController } from '@ionic/angular/standalone';
  6. import { CloudObject, CloudQuery, CloudUser } from 'src/lib/ncloud';
  7. // 定义 ContentItem 接口
  8. interface ContentItem {
  9. role: string;
  10. content: string;
  11. createdAt?: string; // 可选属性
  12. hidden?: boolean; // 可选属性
  13. complete?: boolean; // 可选属性
  14. cid?: string; // 可选属性
  15. }
  16. @Component({
  17. selector: 'app-report-modal',
  18. templateUrl: './report-modal.component.html',
  19. styleUrls: ['./report-modal.component.scss'],
  20. standalone: true,
  21. imports: [IonHeader,IonToolbar,IonTitle,CommonModule,
  22. IonButtons,IonButton,IonContent,IonCard,IonList,IonItem,IonLabel
  23. ],
  24. })
  25. export class ReportModalComponent implements OnInit {
  26. constructor(private modalCtrl: ModalController,private router: Router) {}
  27. userFeelings: Array<{ content: string, score: number, label: string }> = []; // 存储用户内容和情感分析结果
  28. closeModal() {
  29. this.router.navigate(['tabs/tab3']);
  30. }
  31. chatrecordList:Array<CloudObject>=[]
  32. async loadChatRecordList() {
  33. // 获取当前用户的 ObjectId
  34. const currentUser = new CloudUser();
  35. await currentUser.current(); // 确保用户信息已加载
  36. const userId = currentUser.id; // 获取当前用户的 ObjectId
  37. // 创建查询并添加过滤条件
  38. let query = new CloudQuery("ChatRecord");
  39. query.equalTo("user", currentUser.toPointer()); // 过滤条件,确保只获取当前用户的记录
  40. this.chatrecordList = await query.find();
  41. // 遍历 chatrecordList,去除每个记录中 user 的第一条 content
  42. this.chatrecordList.forEach(chatrecord => {
  43. const contentArray = chatrecord.get('content');
  44. if (contentArray && contentArray.length > 0) {
  45. // 找到第一条 role 为 user 的内容并移除
  46. const userIndex = contentArray.findIndex((item: ContentItem) => item.role === 'user');
  47. if (userIndex !== -1) {
  48. contentArray.splice(userIndex, 1); // 移除第一条 user 内容
  49. }
  50. }
  51. });
  52. }
  53. analyzeUserFeeling(content: string): { score: number; label: string } {
  54. const moodScores = {
  55. positive: {
  56. words: ['开心', '高兴', '喜欢', '快乐', '棒', '兴奋', '满足', '美好','喜悦'],
  57. score: 1
  58. },
  59. neutral: {
  60. words: ['一般', '还好', '可以', '无所谓', '正常'],
  61. score: 0
  62. },
  63. negative: {
  64. words: ['难过', '生气', '失望', '讨厌', '悲伤', '烦恼', '痛苦', '沮丧'],
  65. score: -1
  66. }
  67. };
  68. let totalScore = 0;
  69. // 分析内容并计算分数
  70. const contentWords = content.split(/\s+/);
  71. contentWords.forEach(word => {
  72. for (const mood in moodScores) {
  73. if ((moodScores as any)[mood].words.includes(word)) {
  74. totalScore += (moodScores as any)[mood].score;
  75. }
  76. }
  77. });
  78. // 判断心情状态
  79. let label = '中性';
  80. if (totalScore > 0) {
  81. label = '积极';
  82. } else if (totalScore < 0) {
  83. label = '消极';
  84. }
  85. return { score: totalScore, label };
  86. }
  87. async ngOnInit() {
  88. await this.loadChatRecordList();
  89. // 在这里可以遍历 chatrecordList 中的用户内容并进行情感分析
  90. this.chatrecordList.forEach(chatrecord => {
  91. const contentArray = chatrecord.get('content');
  92. contentArray.forEach((contentItem: ContentItem) => {
  93. if (contentItem.role === 'user') {
  94. const analysis = this.analyzeUserFeeling(contentItem.content);
  95. this.userFeelings.push({ content: contentItem.content, score: analysis.score, label: analysis.label });
  96. }
  97. });
  98. });
  99. }
  100. }