wujingjing
2024-10-21 50f811399f22b835fd2fc33375fe01ff8eb83dd8
src/components/chat/components/playBar/PlayBar.vue
@@ -39,8 +39,16 @@
                  @click="similarClick(item)"
               >
                  <span class="text-sm text-gray-500 pr-1.5">{{ index + 1 }}</span>
                  <span> {{ item?.question }} </span>
                  <!-- <span class="text-blue-400 font-bold cursor-pointer hover:underline" @click.stop="tipMetricsClick">测试是</span> -->
                  <template v-if="sentenceSplitMap?.[item.question]">
                     <template v-for="part in sentenceSplitMap[item.question]">
                        <span
                           v-if="part.isKeyword"
                           class="text-blue-400 font-bold cursor-pointer"
                           >{{ part.partStr }}</span
                        >
                        <span v-else>{{ part.partStr }}</span>
                     </template>
                  </template>
               </div>
            </div>
         </div>
@@ -78,12 +86,12 @@
import { ElMessage } from 'element-plus';
import getCaretCoordinates from 'textarea-caret';
import { computed, nextTick, ref } from 'vue';
import VoicePage from './voicePage/VoicePage.vue';
import { querySimilarityHistory } from '/@/api/ai/chat';
import { useClickOther } from '/@/hooks/useClickOther';
import InfoDetail from './InfoDetail.vue';
import VoicePage from './voicePage/VoicePage.vue';
import { getMetricsNames, querySimilarityHistory } from '/@/api/ai/chat';
import { onClickOutside } from '@vueuse/core';
import _ from 'lodash';
const emits = defineEmits(['sendClick']);
const props = defineProps(['isTalking', 'isHome']);
const voicePageIsShow = defineModel('voicePageIsShow', {
@@ -153,13 +161,25 @@
         popUpPosition.value.left = caret.left + leftOffset;
         popUpPosition.value.bottom = container.offsetHeight + bottomOffset;
         triggerShow.value = true;
         if (lastIsFinish) {
            querySimilarityApi(text);
         }
      }, 0);
   });
};
// 对 question 进行分割
const sentenceSplitMap = ref<
   Record<
      string,
      {
         partStr: string;
         startIndex: number;
         endIndex: number;
         isKeyword: boolean;
      }[]
   >
>({});
const similarList = ref([]);
let lastIsFinish = true;
const querySimilarityApi = async (text: string) => {
@@ -172,6 +192,12 @@
   const handleValues = res?.values ?? [];
   similarList.value = props.isHome ? handleValues.slice(0, 3) : handleValues;
   metricsNamesPromise.then((value) => {
      sentenceSplitMap.value = getSentenceMatchMap(
         similarList.value.map((item) => item.question),
         value as string[]
      );
   });
};
const audioChangeWord = () => {
   navigator.getUserMedia(
@@ -184,7 +210,161 @@
      }
   );
};
/**
 * 切分句子,匹配词用 isKeyword 标记
 * @param sentences
 * @param keywords
 */
const getSentenceMatchMap = (sentences: string[], keywords: any[]) => {
   if (!sentences || sentences.length === 0) return null;
   if (!keywords || keywords.length===0) {
      return sentences.map((item) => ({
         partStr: item,
         startIndex: 0,
         endIndex: item.length,
         isKeyword: false,
      }));
   }
   let sentenceMatchMap = {};
   sentences.map((sentence) => {
      if (!sentenceMatchMap[sentence]) {
         let sentenceMatchList = [];
         keywords.map((keyword) => {
            const matchList = [...sentence.matchAll(keyword)].map((item) => {
               return {
                  partStr: item[0],
                  startIndex: item.index,
                  endIndex: item.index + item[0].length,
               };
            });
            sentenceMatchList = sentenceMatchList.concat(matchList);
         });
         let nextIsMerge = false;
         const checkNextIsMerge = (value, index, array) => {
            nextIsMerge = false;
            if (index === array.length - 1) return;
            const nextValue = array[index + 1];
            // 通过 nextIsMerge 控制下一元素是否需要使用
            if (value.endIndex > nextValue.startIndex) {
               nextIsMerge = true;
            }
         };
         // 按 startIndex 排序,消除彼此之间重合元素
         sentenceMatchList = _.sortBy(sentenceMatchList, (item) => item.startIndex).filter((value, index, array) => {
            if (nextIsMerge) {
               checkNextIsMerge(value, index, array);
               return false;
            }
            checkNextIsMerge(value, index, array);
            return true;
         });
         sentenceMatchMap[sentence] = sentenceMatchList;
      }
   });
   for (const sentence of Object.keys(sentenceMatchMap)) {
      const matchList = sentenceMatchMap[sentence];
      const result = [];
      if (matchList.length === 0) {
         result.push({
            partStr: sentence,
            startIndex: 0,
            endIndex: sentence.length,
            isKeyword: false,
         });
         sentenceMatchMap[sentence] = result;
         continue;
      }
      matchList.forEach((value, index, array) => {
         // 匹配词恰好不是位于结束位置
         if (array.length - 1 === index && value.endIndex !== array.length) {
            result.push({
               ...value,
               isKeyword: true,
            });
            if (value.endIndex !== sentence.length) {
               result.push({
                  partStr: sentence.slice(value.endIndex, sentence.length),
                  startIndex: value.endIndex,
                  endIndex: sentence.length,
                  isKeyword: false,
               });
            }
            // 如果数组只有一个元素,前面的也需要加进去
            if (array.length === 1 && value.startIndex !== 0) {
               result.unshift({
                  partStr: sentence.slice(0, value.startIndex),
                  startIndex: 0,
                  endIndex: value.startIndex,
                  isKeyword: false,
               });
            }
            return;
         }
         // 匹配词恰好不是位于起始位置
         if (value.startIndex !== 0 && index === 0) {
            result.push({
               ...value,
               isKeyword: true,
            });
            result.unshift({
               partStr: sentence.slice(0, value.startIndex),
               startIndex: 0,
               endIndex: value.startIndex,
               isKeyword: false,
            });
            return;
         }
         // 恰好位于第一个
         if (index === 0) {
            result.push({
               ...value,
               isKeyword: true,
            });
            return;
         }
         // 中间有非关键词
         if (array[index - 1].endIndex !== value.startIndex) {
            result.push({
               partStr: sentence.slice(array[index - 1].endIndex, value.startIndex),
               startIndex: array[index - 1].endIndex,
               endIndex: value.startIndex,
               isKeyword: false,
            });
            result.push({
               ...value,
               isKeyword: true,
            });
         }
      });
      sentenceMatchMap[sentence] = result;
   }
   return sentenceMatchMap;
};
const metricsNamesPromise = new Promise(async (resolve, reject) => {
   const metricNames = (await getMetricsNames())?.values ?? [];
   resolve(metricNames);
});
//#region ======================  高亮指标点击======================
const infoDetailIsShow = ref(false);