feat:save

This commit is contained in:
zyronon
2025-07-09 15:30:27 +08:00
parent 57a1a33752
commit e1cf6ce98f
12 changed files with 27 additions and 336 deletions

View File

@@ -2,8 +2,8 @@
{
"title": "A private conversation!",
"titleTranslate": "私人谈话!",
"text": "Last week I went to the theatre. I had a very good seat. The play was very interesting. I did not enjoy it. A young man and a young woman were sitting behind me. They were talking loudly. I got very angry. I could not hear the actors. I turned round. I looked at the man and the woman angrily. They did not pay any attention. In the end, I could not bear it. I turned round again. 'I can't hear a word!' I said angrily.\n\n 'It's none of your business,' the young man said rudely. 'This is a private conversation!'",
"textTranslate": "上星期我去看戏。\n我的座位很好\n戏很有意思\n但我却无法欣赏。\n一青年男子与一青年女子坐在我的身后\n大声地说着话。\n我非常生气\n因为我听不见演员在说什么。\n我回过头去\n怒视着那一男一女\n他们却毫不理会。\n最后我忍不住了\n又一次回过头去\n生气地说“我一个字也听不见了”\n\n“不关你的事”那男的毫不客气地说\n“这是私人间的谈话”",
"text": "Last week I went to the theatre. \nI had a very good seat. \nThe play was very interesting. \nI did not enjoy it. \nA young man and a young woman were sitting behind me. \nThey were talking loudly. \nI got very angry. \nI could not hear the actors. \nI turned round. \nI looked at the man and the woman angrily. \nThey did not pay any attention. \nIn the end, I could not bear it. \nI turned round again. \n'I can't hear a word!' I said angrily. \n\n'It's none of your business,' the young man said rudely. \n'This is a private conversation!'",
"textTranslate": "上星期我去看戏。 \n我的座位很好 \n戏很有意思 \n但我却无法欣赏。 \n一青年男子与一青年女子坐在我的身后 \n大声地说着话。 \n我非常生气 \n因为我听不见演员在说什么。 \n我回过头去 \n怒视着那一男一女 \n他们却毫不理会。 \n最后我忍不住了 \n又一次回过头去 \n生气地说“我一个字也听不见了 \n\n“不关你的事”那男的毫不客气地说 \n“这是私人间的谈话”",
"newWords": [],
"id": "HmlGhw",
"audioSrc": "/public/sound/article/nce2-1/01A Private Conversation.mp3",

View File

@@ -1,9 +1,7 @@
import {Article, ArticleWord, DefaultArticleWord, DictType, Sentence, TranslateType} from "@/types.ts";
import {Article, ArticleWord, DefaultArticleWord, DictType, Sentence} from "@/types.ts";
import {cloneDeep} from "lodash-es";
import nlp from "compromise/one";
import {split} from "sentence-splitter";
import {usePlayWordAudio} from "@/hooks/sound.ts";
import {translate} from "element-plus";
import {getSentenceAllText, getSentenceAllTranslateText} from "@/hooks/translate.ts";
interface KeyboardMap {
@@ -32,269 +30,6 @@ export const EnKeyboardMap: KeyboardMap = {
QuoteRight: `'`,
}
//TODO 废弃
export function splitEnArticle(text: string): { sections: Sentence[][], newText: string } {
console.log('splitEnArticle')
//将中文符号替换
text = text.replaceAll('', "'")
text = text.replaceAll('—', "-")
text = text.replaceAll('”', '"')
text = text.replaceAll('“', '"')
// console.time()
let keyboardMap = EnKeyboardMap
let sections: Sentence[][] = []
text && text.trim().split('\n\n').filter(v => v).map((sectionText, i) => {
let section: Sentence[] = []
sections.push(section)
sectionText = sectionText.trim()
let doc = nlp(sectionText)
let sentenceNlpList = []
// console.log('ss', sentenceNlpList)
doc.json().map(item => {
//如果整句大于15个单词以上检测是否有 逗号子句
if (item.terms.length > 15) {
//正则匹配“逗号加and|but|so|because"
let list = item.text.split(/,\s(?=(and|but|so|because)\b)/).filter(_ => {
//匹配完之后会把and|but|so|because也提出来这里不需要重复的直接筛选掉
if (_ && !['and', 'but', 'so', 'because'].includes(_)) return _
})
if (list.length === 1) {
sentenceNlpList.push(item)
} else {
list.map((text, i) => {
//分割后每句都没有逗号了,所以除了最后一句外需要加回来
sentenceNlpList = sentenceNlpList.concat(nlp(text + (i !== list.length - 1 ? ',' : '')).json())
})
}
} else {
sentenceNlpList.push(item)
}
})
sentenceNlpList.map(item => {
let sentence: Sentence = cloneDeep({
//他没有空格,导致修改一行一行的数据时,汇总时全没有空格了,库无法正常断句
text: item.text + ' ',
// text: '',
translate: '',
words: [],
audioPosition: [0, 0],
})
section.push(sentence)
const checkQuote = (pre: string, index?: number) => {
let nearSymbolPosition = null
if (index === 0) {
nearSymbolPosition = 'end'
} else {
//TODO 可以优化成for+break
section.toReversed().map((sentenceItem, b) => {
sentenceItem.words.toReversed().map((wordItem, c) => {
if (wordItem.symbolPosition !== '' && nearSymbolPosition === null) {
nearSymbolPosition = wordItem.symbolPosition
}
})
})
}
let word3: ArticleWord = {
...DefaultArticleWord,
word: pre,
nextSpace: false,
isSymbol: true,
symbolPosition: ''
};
// console.log('rrr', item)
// console.log('nearSymbolPosition', nearSymbolPosition)
if (nearSymbolPosition === 'end' || nearSymbolPosition === null) {
word3.symbolPosition = 'start'
sentence.words.push(word3)
} else {
sentence.words[sentence.words.length - 1].nextSpace = false
word3.symbolPosition = 'end'
word3.nextSpace = true
let addCurrent = false
sentence.words.toReversed().map((wordItem, c) => {
if (wordItem.symbolPosition === 'start' && !addCurrent) {
addCurrent = true
}
})
if (addCurrent) {
sentence.words.push(word3)
} else {
// 'Do you always get up so late? It'LICENSE one o'clock!' 会被断成两句
let lastSentence = section[section.length - 2]
lastSentence.words = lastSentence.words.concat(sentence.words)
lastSentence.words.push(word3)
sentence.words = []
//这里还不能直接删除sentence因为后面还有一个 sentence.words = sentence.words.filter(v => v.word !== 'placeholder') 的判断
// section.pop()
}
}
}
const checkSymbol = (post: string, nextSpace: boolean = true) => {
switch (post) {
case keyboardMap.Period:
case keyboardMap.Comma:
case keyboardMap.Slash:
case keyboardMap.Exclamation:
sentence.words[sentence.words.length - 1].nextSpace = false
let word2 = cloneDeep({
...DefaultArticleWord,
word: post,
isSymbol: true,
nextSpace
});
sentence.words.push(word2)
break
case keyboardMap.QuoteLeft:
case ')':
checkQuote(post)
break
case `.'`:
case `!'`:
case `?'`:
case `,'`:
case `*'`:
post.split('').map(v => {
checkSymbol(v, false)
})
break
//类似于这种的“' -- ”的。需要保留空格用了一个占位符才处理因为每个符号都会把前面的那个字符的nextSpace改为false
case ' ':
// console.log('sentence', sentence)
//遇到“The clock has stopped!' I looked at my watch.”
//检测到stopped!' 的'时如果前引号不在当前句会把当前句的word合并到前一句。那么当前句的word就为空了会报错
//所以需要检测一下
if (sentence.words.length) {
sentence.words[sentence.words.length - 1].nextSpace = true
let word3 = cloneDeep({
...DefaultArticleWord,
word: 'placeholder',
isSymbol: true,
nextSpace: false,
});
sentence.words.push(word3)
}
break
default:
// console.log('post', post)
//这里多半是一些奇怪的连接符之类的
if (post.length > 1) {
post.split('').map(v => {
checkSymbol(v, false)
})
} else {
sentence.words[sentence.words.length - 1].nextSpace = false
let word3 = cloneDeep({
...DefaultArticleWord,
word: post,
isSymbol: true,
nextSpace: false,
});
sentence.words.push(word3)
}
break
}
}
item.terms.map((v, index: number) => {
// console.log('v', v)
if (v.text) {
let pre: string = v.pre.trim()
if (pre) {
checkQuote(pre, index)
}
let word = cloneDeep({...DefaultArticleWord, word: v.text, nextSpace: true});
sentence.words.push(word)
let post: string = v.post
//判断是不是等于空,因为正常的词后面都会有个空格。这种不需要处理。
if (post && post !== ' ') {
checkSymbol(post.trim())
}
}
})
//去除空格占位符
sentence.words = sentence.words.filter(v => v.word !== 'placeholder')
//如果是空的,直接去掉
if (!sentence.words.length) {
section.pop()
}
})
// console.log(sentenceNlpList)
})
sections = sections.filter(sectionItem => sectionItem.length)
sections.map((sectionItem, a) => {
sectionItem.map((sentenceItem, b) => {
sentenceItem.text = sentenceItem.words.reduce((previousValue: string, currentValue) => {
previousValue += currentValue.word + (currentValue.nextSpace ? ' ' : '')
return previousValue
}, '')
})
})
// console.log(sections)
text = sections.map(v => v.map(s => s.text.trim()).join('\n')).join('\n\n');
// console.log('s',text)
return {
//s.text.trim()的trim()不能去掉,因为这个方法会重复执行,要保证句子后面只有一个\n不trim() \n就会累加
newText: text,
sections
}
}
//TODO 废弃
export function splitCNArticle(text: string): Sentence[][] {
// text = "飞机误点了,侦探们在机场等了整整一上午。他们正期待从南非来的一个装着钻石的贵重包裹。数小时以前,有人向警方报告,说有人企图偷走这些钻石。当飞机到达时,一些侦探等候在主楼内,另一些侦探则守候在停机坪上。有两个人把包裹拿下飞机,进了海关。这时两个侦探把住门口,另外两个侦探打开了包裹。令他们吃惊的是,那珍贵的包裹里面装的全是石头和沙子!"
// text = "那是 4.4 个星期天?而在星期天我是从来不早起的,有时我要一直躺到吃午饭的时候。上个星期天,我起得很晚。我望望窗外,外面一片昏暗。“鬼天气!”我想,“又下雨了。”正在这时,电话铃响了。是我姑母露西打来的。“我刚下火车,”她说,“我这就来看你。”\n “但我还在吃早饭,”我说。\n “你在干什么?”她问道。\n “我正在吃早饭,”我又说了一遍。\n “天啊”她说“你总是起得这么晚吗现在已经1点钟了”"
//去除头和尾部的空格
text = text.trim()
let sections: Sentence[][] = []
text && text.split('\n\n').map((rowSection, i) => {
let section: Sentence[] = []
sections.push(section)
let list = rowSection.trim().split('\n')
list.map(sentenceText => {
let sentences = split(sentenceText)
sentences.map(sentenceRow => {
let row = sentenceRow.raw
let sentence: Sentence = {
text: row,
translate: '',
words: [],
audioPosition: [0, 0],
}
section.push(sentence)
if (row) {
//sentence-splitter 这个库总是会把反引号给断句到下一行
if (row[0] === "”") {
sentence.text = row.substr(1)
let lastSentence = section[section.length - 2]
lastSentence.text += "”"
if (!sentence.text) {
section.pop()
}
}
}
// console.log('sentence', sentenceRow)
})
})
})
// console.log('sections', sections)
return sections
}
//生成文章段落数据
export function genArticleSectionData(article: Article): number {
@@ -308,6 +43,8 @@ export function genArticleSectionData(article: Article): number {
return
}
console.log('genArticleSectionData',text)
let keyboardMap = EnKeyboardMap
let sections: Sentence[][] = []
let sectionTextList = text.split('\n\n')
@@ -805,21 +542,6 @@ export function splitCNArticle2(text: string): string {
return s
}
//todo 废弃
export function getSplitTranslateText(article: string) {
let sections = splitCNArticle(article)
let str = ''
if (sections.length) {
sections.map((sectionItem) => {
sectionItem.map((sentenceItem) => {
str += sentenceItem.text + (sentenceItem.text.endsWith("\n") ? '' : '\n')
})
str += str.endsWith("\n\n") ? '' : '\n'
})
}
return str.trim()
}
export function isArticle(type: DictType): boolean {
return [
DictType.article,

View File

@@ -1,9 +1,9 @@
import {Article, Sentence, TranslateEngine} from "@/types.ts";
import Baidu from "@opentranslate/baidu";
import {axiosInstance} from "@/utils/http.ts";
import {splitEnArticle} from "@/hooks/article.ts";
import {Translator} from "@opentranslate/translator/src/translator.ts";
//todo feiqi
export function renewSectionTranslates(article: Article, translate: string) {
let failCount = 0
let articleTranslate = translate.split('\n')
@@ -176,18 +176,3 @@ export async function getNetworkTranslate(
}
}
}
export function renewSectionTexts(article: Article) {
let {newText, sections} = splitEnArticle(article.text)
article.text = newText
article.sections = sections
let count = 0
if (article.lrcPosition.length) {
article.sections.map((v, i) => {
v.map((w, j) => {
w.audioPosition = article.lrcPosition[count]
count++
})
})
}
}

View File

@@ -1,10 +1,15 @@
<script setup lang="ts">
import EditArticle2 from "@/pages/pc/components/article/EditArticle2.vue";
import BaseIcon from "@/components/BaseIcon.vue";
</script>
<template>
<div class="h-screen">
<BaseIcon
title="返回"
@click="$router.back"
icon="formkit:left"/>
<EditArticle2 class="vue"></EditArticle2>
</div>

View File

@@ -4,13 +4,7 @@ import {Article, DefaultArticle, Sentence, TranslateEngine} from "@/types.ts";
import BaseButton from "@/components/BaseButton.vue";
import EditAbleText from "@/pages/pc/components/EditAbleText.vue";
import {Icon} from "@iconify/vue";
import {
getNetworkTranslate,
getSentenceAllText,
getSentenceAllTranslateText,
renewSectionTexts,
renewSectionTranslates
} from "@/hooks/translate.ts";
import {getNetworkTranslate, getSentenceAllText, getSentenceAllTranslateText} from "@/hooks/translate.ts";
import {genArticleSectionData, splitCNArticle2, splitEnArticle2, usePlaySentenceAudio} from "@/hooks/article.ts";
import {cloneDeep, last} from "lodash-es";
import {watch} from "vue";
@@ -52,7 +46,6 @@ watch(() => props.article, val => {
progress = 0
failCount = 0
apply()
console.log('ar', editArticle)
}, {immediate: true})
watch(() => editArticle.text, (s) => {
@@ -542,6 +535,7 @@ function setStartTime(val: Sentence, i: number, j: number) {
.content {
color: var(--color-article);
height: 100%;
width: 100%;
box-sizing: border-box;
display: flex;
gap: var(--space);

View File

@@ -7,7 +7,6 @@ import {useBaseStore} from "@/stores/base.ts";
import List from "@/pages/pc/components/list/List.vue";
import Dialog from "@/pages/pc/components/dialog/Dialog.vue";
import EditArticle from "@/pages/pc/components/article/EditArticle.vue";
import {emitter, EventKey} from "@/utils/eventBus.ts";
import {useDisableEventListener, useWindowClick} from "@/hooks/event.ts";
import {MessageBox} from "@/utils/MessageBox.tsx";
@@ -15,6 +14,7 @@ import {useRuntimeStore} from "@/stores/runtime.ts";
import {nanoid} from "nanoid";
import {syncMyDictList} from "@/hooks/dict.ts";
import MiniDialog from "@/pages/pc/components/dialog/MiniDialog.vue";
import EditArticle2 from "@/pages/pc/components/article/EditArticle2.vue";
const emit = defineEmits<{
importData: [val: Event]
@@ -193,7 +193,7 @@ useWindowClick(() => showExport = false)
<BaseButton size="small" @click="add">新增</BaseButton>
</div>
</div>
<EditArticle
<EditArticle2
ref="editArticleRef"
type="batch"
@save="saveArticle"

View File

@@ -3,8 +3,8 @@
import {Article, DefaultArticle} from "@/types.ts";
import {cloneDeep} from "lodash-es";
import Dialog from "@/pages/pc/components/dialog/Dialog.vue";
import EditArticle from "@/pages/pc/components/article/EditArticle.vue";
import {useDisableEventListener} from "@/hooks/event.ts";
import EditArticle2 from "@/pages/pc/components/article/EditArticle2.vue";
interface IProps {
article?: Article
@@ -32,7 +32,7 @@ useDisableEventListener(() => props.modelValue)
:full-screen="true"
>
<div class="wrapper">
<EditArticle
<EditArticle2
:article="article"
@save="val => emit('save',val)"
/>

View File

@@ -1,14 +1,14 @@
<script setup lang="ts">
import {splitEnArticle} from "@/hooks/article.ts";
import {splitEnArticle2} from "@/hooks/article.ts";
function test() {
let {newText, sections} = splitEnArticle(
splitEnArticle2(
`While it is yet to be seen what direction the second Trump administration will take globally in its China policy, VOA traveled to the main island of Mahe in Seychelles to look at how China and the U.S. have impacted the country, and how each is fairing in that competition for influence there.
`)
}
function test2() {
let {newText, sections} = splitEnArticle(
splitEnArticle2(
`
Last week I went to the theatre. I had a very good seat. The play was very interesting. I did not enjoy it. A young man and a young woman were sitting behind me. They were talking loudly. I got very angry. I could not hear the actors. I turned round. I looked at the man and the woman angrily. They did not pay any attention. In the end, I could not bear it. I turned round again. I cant hear a word! I said angrily.
Its none of your business, the young man said rudely. This is a private conversation!

View File

@@ -1,21 +1,10 @@
<script setup lang="ts">
import TypingArticle from "./TypingArticle.vue";
import {
Article,
ArticleItem,
ArticleWord,
DefaultArticle,
DisplayStatistics, Sentence,
ShortcutKey,
TranslateType,
Word
} from "@/types.ts";
import {Article, ArticleItem, ArticleWord, DefaultArticle, DisplayStatistics, ShortcutKey, Word} from "@/types.ts";
import {cloneDeep} from "lodash-es";
import TypingWord from "@/pages/pc/components/TypingWord.vue";
import Panel from "../../components/Panel.vue";
import {onMounted, onUnmounted} from "vue";
import {renewSectionTexts, renewSectionTranslates} from "@/hooks/translate.ts";
import {MessageBox} from "@/utils/MessageBox.tsx";
import {useBaseStore} from "@/stores/base.ts";
import EditSingleArticleModal from "@/pages/pc/components/article/EditSingleArticleModal.vue";
import {usePracticeStore} from "@/stores/practice.ts";
@@ -31,8 +20,7 @@ import ArticleList from "@/pages/pc/components/list/ArticleList.vue";
import {useOnKeyboardEventListener} from "@/hooks/event.ts";
import VolumeSetting from "@/pages/pc/components/toolbar/VolumeSetting.vue";
import TranslateSetting from "@/pages/pc/components/toolbar/TranslateSetting.vue";
import {usePlayWordAudio} from "@/hooks/sound.ts";
import {usePlaySentenceAudio} from "@/hooks/article.ts";
import {genArticleSectionData, usePlaySentenceAudio} from "@/hooks/article.ts";
const store = useBaseStore()
const statisticsStore = usePracticeStore()
@@ -105,11 +93,8 @@ function getCurrentPractice() {
if (tempArticle.sections.length) {
setArticle(tempArticle)
} else {
renewSectionTexts(tempArticle)
if (tempArticle.textTranslate.trim()) {
renewSectionTranslates(tempArticle, tempArticle.textTranslate)
setArticle(tempArticle)
}
genArticleSectionData(tempArticle)
setArticle(tempArticle)
}
}

View File

@@ -29,7 +29,7 @@ import EditArticlePage from "@/pages/pc/article/EditArticlePage.vue";
export const routes: RouteRecordRaw[] = [
{
path: '/', component: PC,
redirect: '/edit-article',
redirect: '/home',
children: [
{path: 'home', component: HomeIndex},
{path: 'word', component: WordHome},

View File

@@ -132,7 +132,7 @@ export const DefaultBaseState = (): BaseState => ({
type: DictType.article,
resourceId: 'article_nce2',
length: 96,
lastLearnIndex:24
lastLearnIndex:1
},
],
wordDictList: [
@@ -429,4 +429,4 @@ export const useBaseStore = defineStore('base', {
},
},
})
})