This commit is contained in:
zyronon
2023-09-21 01:43:24 +08:00
parent a262f1ec49
commit 5c1b5cf70e
7 changed files with 245 additions and 1306 deletions

View File

@@ -27,8 +27,10 @@ export const EnKeyboardMap: KeyboardMap = {
QuoteRight: '"',
}
export function splitArticle(article: string, lang: string = 'en', keyboardMap: KeyboardMap = EnKeyboardMap): Sentence[][] {
export function splitEnArticle(text: string, lang: string = 'en', keyboardMap: KeyboardMap = EnKeyboardMap): {
sections: Sentence[][],
text: string
} {
let sections: Sentence[][] = []
let section: Sentence[] = []
let sentence: Sentence = {
@@ -40,17 +42,18 @@ export function splitArticle(article: string, lang: string = 'en', keyboardMap:
sections.push(section)
let word = cloneDeep({...DefaultArticleWord, name: '', nextSpace: true});
//加\n用于添加最后一段
article += '\n'
if (lang === 'en') {
article = article.replaceAll(``, '"')
article = article.replaceAll(``, '"')
article = article.replaceAll(``, '"')
article = article.replaceAll(``, '"')
}
text += '\n'
text = text.replaceAll(``, '"')
text = text.replaceAll(``, '"')
text = text.replaceAll(``, '"')
text = text.replaceAll(``, '"')
//替换所有单引号为双引号
text = text.replaceAll(`'`, '"')
//将缩写词的双引号替换回单引号
text = text.replaceAll(`"t`, `'t`)
text = text.replaceAll(`"s`, `'s`)
// console.log('articles', articles)
article.split('').map((v, i, arr) => {
text.split('').map((v, i, arr) => {
switch (v) {
case ' ':
if (word.name) {
@@ -165,7 +168,10 @@ export function splitArticle(article: string, lang: string = 'en', keyboardMap:
}, '')
})
})
return sections
return {
text,
sections
}
}
export function splitCNArticle(article: string, lang: string = 'cn', keyboardMap: KeyboardMap = CnKeyboardMap): Sentence[][] {

View File

@@ -1,7 +1,7 @@
import {Article, Sentence, TranslateEngine, TranslateType} from "@/types.ts";
import Baidu from "@opentranslate/baidu";
import {axiosInstance} from "@/utils/http.ts";
import {getSplitTranslateText, splitArticle} from "@/hooks/article.ts";
import {getSplitTranslateText, splitEnArticle} from "@/hooks/article.ts";
import {Translator} from "@opentranslate/translator/src/translator.ts";
export function updateLocalSentenceTranslate(article: Article, translate: string) {
@@ -191,7 +191,7 @@ export async function getNetworkTranslate(
}
export function test(article: Article) {
if (article?.isTranslated) {
if (article?.translateSplit) {
if (!article.sections?.length) {
}
@@ -203,7 +203,7 @@ export function test(article: Article) {
updateLocalSentenceTranslate(article, article.networkTranslate)
}
} else {
article.sections = splitArticle(article.article)
// article.sections = splitEnArticle(article.article)
if (article.customTranslate) {
article.customTranslate = getSplitTranslateText(article.customTranslate)
updateLocalSentenceTranslate(article, article.customTranslate)