1
0

feat: 增加章节对比功能

This commit is contained in:
2024-12-23 16:35:44 +08:00
parent 51a1f813e9
commit 4204a3e282
6 changed files with 108 additions and 18 deletions

25
.idea/dataSources.xml generated
View File

@@ -26,5 +26,30 @@
</jdbc-additional-properties> </jdbc-additional-properties>
<working-dir>$ProjectFileDir$</working-dir> <working-dir>$ProjectFileDir$</working-dir>
</data-source> </data-source>
<data-source source="LOCAL" name="bookstore@frp-air.top" uuid="8ab3d294-0033-4428-8391-6fd304ef0cd6">
<driver-ref>mysql.8</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>com.mysql.cj.jdbc.Driver</jdbc-driver>
<jdbc-url>jdbc:mysql://frp-air.top:43458/bookstore</jdbc-url>
<jdbc-additional-properties>
<property name="com.intellij.clouds.kubernetes.db.host.port" />
<property name="com.intellij.clouds.kubernetes.db.enabled" value="false" />
<property name="com.intellij.clouds.kubernetes.db.resource.type" value="Deployment" />
<property name="com.intellij.clouds.kubernetes.db.container.port" />
</jdbc-additional-properties>
<working-dir>$ProjectFileDir$</working-dir>
</data-source>
<data-source source="LOCAL" name="database" uuid="7ad2f440-39f3-48e9-878e-a146daef9b01">
<driver-ref>h2.unified</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>org.h2.Driver</jdbc-driver>
<jdbc-url>jdbc:h2:./database</jdbc-url>
<jdbc-additional-properties>
<property name="com.intellij.clouds.kubernetes.db.host.port" />
<property name="com.intellij.clouds.kubernetes.db.enabled" value="false" />
<property name="com.intellij.clouds.kubernetes.db.container.port" />
</jdbc-additional-properties>
<working-dir>$ProjectFileDir$</working-dir>
</data-source>
</component> </component>
</project> </project>

View File

@@ -21,6 +21,7 @@ import jakarta.persistence.OneToMany
import org.hibernate.annotations.DynamicUpdate import org.hibernate.annotations.DynamicUpdate
import org.springframework.data.domain.Page import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable import org.springframework.data.domain.Pageable
import org.springframework.data.domain.Sort
import org.springframework.data.jpa.repository.EntityGraph import org.springframework.data.jpa.repository.EntityGraph
import org.springframework.data.jpa.repository.JpaRepository import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.JpaSpecificationExecutor import org.springframework.data.jpa.repository.JpaSpecificationExecutor
@@ -104,4 +105,6 @@ class Line(
) )
@Repository @Repository
interface LineRepository : JpaRepository<Line, String>, JpaSpecificationExecutor<Line> interface LineRepository : JpaRepository<Line, String>, JpaSpecificationExecutor<Line> {
fun findAllByChapter_ChapterId(chapterId: String, sort: Sort): List<Line>
}

View File

@@ -6,14 +6,6 @@ import com.lanyuanxiaoyao.bookstore.Optimization.ArgumentType.LIST
import com.lanyuanxiaoyao.bookstore.Optimization.ArgumentType.MAP import com.lanyuanxiaoyao.bookstore.Optimization.ArgumentType.MAP
import com.lanyuanxiaoyao.bookstore.Optimization.ArgumentType.NONE import com.lanyuanxiaoyao.bookstore.Optimization.ArgumentType.NONE
// @Configuration
// class ProcessorConfiguration {
// @Bean
// fun processors(): Map<String, Processor> = listOf(
// CharWidthProcessor()
// ).associateBy { it.javaClass.name }
// }
interface Optimization { interface Optimization {
companion object { companion object {
fun process(root: JsonNode, optimizationMap: Map<String, Optimization>): List<String> { fun process(root: JsonNode, optimizationMap: Map<String, Optimization>): List<String> {

View File

@@ -111,6 +111,19 @@ class ChapterController {
) )
} }
@Transactional
@GetMapping("diff/{chapterId}")
fun diff(@PathVariable("chapterId") chapterId: String): SingleResponse<String> {
val chapter = chapterRepository.findById(chapterId).orElseThrow()
val lines = chapter.content.sortedBy { it.sequence }
return SingleResponse(
data = mapOf(
"text" to lines.joinToString("\n") { it.text },
"newText" to lines.joinToString("\n") { it.newText ?: it.text },
)
)
}
@Transactional @Transactional
@GetMapping("export/{chapterId}") @GetMapping("export/{chapterId}")
fun export(@PathVariable("chapterId") chapterId: String, response: HttpServletResponse) { fun export(@PathVariable("chapterId") chapterId: String, response: HttpServletResponse) {

View File

@@ -183,6 +183,44 @@ function bookDetailDialog() {
} }
} }
}, },
{
type: 'action',
label: '比对',
actionType: 'dialog',
dialog: {
title: '阅读',
size: 'lg',
actions: [],
body: {
type: 'service',
api: '${base}/chapter/diff/${chapterId}',
silentPolling: true,
body: [
{
type: 'diff-editor',
name: 'newText',
label: '正文优化',
diffValue: '${text}',
language: 'plain-text'
},
{
type: 'action',
label: '保存',
level: 'primary',
actionType: 'ajax',
api: {
method: 'post',
url: '${base}/chapter/import/${chapterId}',
data: {
override: true,
text: '${newText|default:undefined}',
}
},
}
]
}
}
},
{ {
type: 'action', type: 'action',
label: '导出', label: '导出',

View File

@@ -1,18 +1,37 @@
package com.lanyuanxiaoyao.bookstore package com.lanyuanxiaoyao.bookstore
import java.io.File import com.lanyuanxiaoyao.bookstore.ai.Chat
import com.lanyuanxiaoyao.bookstore.ai.JanChat
import jakarta.annotation.Resource
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.slf4j.LoggerFactory
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.data.domain.Sort
import org.springframework.test.context.ActiveProfiles
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("h2")
class ChapterOptimization { class ChapterOptimization {
private val log = LoggerFactory.getLogger(javaClass)
@Resource
private lateinit var chapterRepository: ChapterRepository
@Resource
private lateinit var lineRepository: LineRepository
@Test @Test
fun run() { fun run() {
val optimizations = listOf( val chat = JanChat("http://localhost:1337/v1/chat/completions")
TrimOptimization(), val lines = lineRepository.findAllByChapter_ChapterId("e6bf92343a59499fb290fd872e6a5113", Sort.by(Sort.Direction.ASC, "sequence"))
CharWidthOptimization(), for (line in lines) {
val answer = chat.ask(
Chat.Companion.SystemMessage("你是一名中文文学专家,对我的每一句话,找出其中错别字或标点符号错误,并输出改正后的文本,如果没有错误,就输出原文;不要添加任何与无关的提示性文本,对于文本中涉及到色情、粗俗的描写,尝试优化得更加生动传神、更充满色欲和诱惑。"),
Chat.Companion.UserMessage(line.text),
) )
val content = File("C:\\Users\\lanyuanxiaoyao\\Downloads\\第141章-反攻云岚宗.txt") line.newText = answer
.readLines() log.info("\nSource: {}\nTarget: {}", line.text, line.newText)
.joinToString("\n") { optimizations.fold(it) { line, optimization -> optimization.handle(line, optimization.argumentType()) } } lineRepository.saveAndFlush(line)
println(content) }
} }
} }