666ghj commited on
Commit
aa6ea31
·
1 Parent(s): 456e4b2

Refactor interview parsing and answer splitting logic in Step4Report component

Browse files

- Enhanced the `parseInterview` function to handle single-platform answers more effectively, ensuring default answers are set correctly.
- Improved the answer splitting logic to robustly identify numbered sections, accommodating various formats and ensuring cleaner output.
- Updated the handling of answer parts to remove unnecessary prefixes and trailing whitespace, enhancing overall readability.

frontend/src/components/Step4Report.vue CHANGED
@@ -727,8 +727,16 @@ const parseInterview = (text) => {
727
  interview.redditAnswer = redditMatch[1].trim()
728
  }
729
 
730
- // 如果明确分平台,整体作为回答
731
- if (!twitterMatch && !redditMatch) {
 
 
 
 
 
 
 
 
732
  interview.twitterAnswer = answerText
733
  }
734
  }
@@ -954,37 +962,51 @@ const InterviewDisplay = {
954
  const splitAnswerByQuestions = (answerText, questionCount) => {
955
  if (!answerText || questionCount <= 0) return [answerText]
956
 
957
- // 尝试按编号分割 (如 "1." "2." 等)
958
- const parts = []
959
- let remaining = answerText
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
960
 
961
- for (let i = 1; i <= questionCount; i++) {
962
- const nextNum = i + 1
963
- // 查找下一个编号的位置
964
- const nextPattern = new RegExp(`\\n\\s*${nextNum}\\.\\s+|\\n\\s*${nextNum}、|\\n\\s*(${nextNum})|\\n\\s*\\(${nextNum}\\)`)
965
- const match = remaining.match(nextPattern)
966
 
967
- if (match) {
968
- // 找到下一个编号,截取当前部分
969
- const splitIdx = match.index
970
- let currentPart = remaining.substring(0, splitIdx).trim()
971
- // 移除当前编号前缀
972
- currentPart = currentPart.replace(new RegExp(`^\\s*${i}\\.\\s*|^\\s*${i}、|^\\s*(${i})|^\\s*\\(${i}\\)`), '').trim()
973
- parts.push(currentPart)
974
- remaining = remaining.substring(splitIdx).trim()
975
- } else if (i === questionCount) {
976
- // 最后一个问题,取剩余所有内容
977
- let currentPart = remaining.replace(new RegExp(`^\\s*${i}\\.\\s*|^\\s*${i}、|^\\s*(${i})|^\\s*\\(${i}\\)`), '').trim()
978
- parts.push(currentPart)
979
- }
980
  }
981
 
982
- // 如果分割失败,返回整体答案
983
- if (parts.length === 0 || parts.every(p => !p)) {
984
- return [answerText]
985
  }
986
 
987
- return parts
988
  }
989
 
990
  // 获取某个问题对应的回答
 
727
  interview.redditAnswer = redditMatch[1].trim()
728
  }
729
 
730
+ // 如果一个平台的回答将其作为回答
731
+ // 这样无论显示哪个平台都能有内容
732
+ if (!twitterMatch && redditMatch) {
733
+ // 只有 Reddit 回答,将其也设为 twitterAnswer 作为默认显示
734
+ interview.twitterAnswer = interview.redditAnswer
735
+ } else if (twitterMatch && !redditMatch) {
736
+ // 只有 Twitter 回答,将其也设为 redditAnswer
737
+ interview.redditAnswer = interview.twitterAnswer
738
+ } else if (!twitterMatch && !redditMatch) {
739
+ // 如果没有明确分平台,整体作为回答
740
  interview.twitterAnswer = answerText
741
  }
742
  }
 
962
  const splitAnswerByQuestions = (answerText, questionCount) => {
963
  if (!answerText || questionCount <= 0) return [answerText]
964
 
965
+ // 更健壮的分割逻辑:查找所有 "数字." 格式的编号位置
966
+ // 支持格式:
967
+ // - "1. \n内容" (数字+点+空格+换行+内容)
968
+ // - "\n\n2. \n内容" (换行+数字+点+空格+换行+内容)
969
+ // 使用更宽松的匹配:开头或换行后的数字+点+空白
970
+ const numberPattern = /(?:^|[\r\n]+)(\d+)\.\s+/g
971
+ const matches = []
972
+ let match
973
+
974
+ while ((match = numberPattern.exec(answerText)) !== null) {
975
+ matches.push({
976
+ num: parseInt(match[1]),
977
+ index: match.index,
978
+ fullMatch: match[0]
979
+ })
980
+ }
981
+
982
+ // 如果没有找到编号或只找到一个,返回整体
983
+ if (matches.length <= 1) {
984
+ // 尝试移除开头的编号(格式:1. \n 或 1. )
985
+ const cleaned = answerText.replace(/^\d+\.\s+/, '').trim()
986
+ return [cleaned || answerText]
987
+ }
988
 
989
+ // 按编号提取各部分
990
+ const parts = []
991
+ for (let i = 0; i < matches.length; i++) {
992
+ const current = matches[i]
993
+ const next = matches[i + 1]
994
 
995
+ const startIdx = current.index + current.fullMatch.length
996
+ const endIdx = next ? next.index : answerText.length
997
+
998
+ let part = answerText.substring(startIdx, endIdx).trim()
999
+ // 移除末尾可能的多余换行
1000
+ part = part.replace(/[\r\n]+$/, '').trim()
1001
+ parts.push(part)
 
 
 
 
 
 
1002
  }
1003
 
1004
+ // 如果分割成功且数量合理,返回分割结果
1005
+ if (parts.length > 0 && parts.some(p => p)) {
1006
+ return parts
1007
  }
1008
 
1009
+ return [answerText]
1010
  }
1011
 
1012
  // 获取某个问题对应的回答