Skip to content

Commit

Permalink
一部の英語をカタカナに自動で変換して読み上げる機能を実装
Browse files Browse the repository at this point in the history
  • Loading branch information
kosugikun committed Mar 29, 2023
1 parent cc2b1f0 commit a691d0e
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 20 deletions.
7 changes: 0 additions & 7 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions .idea/dataSources/bcae6d7f-dd17-487e-ab3f-743f9b393d89.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<groupId>dev.cosgy</groupId>
<artifactId>TextToSpeak</artifactId>
<version>0.4.0</version>
<version>0.4.0-beta.1</version>
<packaging>jar</packaging>

<repositories>
Expand Down Expand Up @@ -143,6 +143,11 @@
<version>1.8.20-RC2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>69.1</version>
</dependency>
</dependencies>

<build>
Expand Down
11 changes: 4 additions & 7 deletions src/main/java/dev/cosgy/textToSpeak/Bot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ import java.util.function.Consumer
import kotlin.system.exitProcess

class Bot(val waiter: EventWaiter, val config: BotConfig, val settingsManager: SettingsManager) {
val threadpool: ScheduledExecutorService
private val lang: ResourceBundle
val playerManager: PlayerManager
val threadpool: ScheduledExecutorService = Executors.newSingleThreadScheduledExecutor()
private val lang: ResourceBundle = ResourceBundle.getBundle("lang.yomiage", Locale.JAPAN)
val playerManager: PlayerManager = PlayerManager(this)
val voiceCreation: VoiceCreation
val userSettingsManager: UserSettingsManager
val aloneInVoiceHandler: AloneInVoiceHandler
private val aloneInVoiceHandler: AloneInVoiceHandler
var log: Logger = LoggerFactory.getLogger(this.javaClass)
var dictionary: Dictionary? = null
private set
Expand All @@ -50,9 +50,6 @@ class Bot(val waiter: EventWaiter, val config: BotConfig, val settingsManager: S
private var gui: GUI? = null

init {
lang = ResourceBundle.getBundle("lang.yomiage", Locale.JAPAN)
threadpool = Executors.newSingleThreadScheduledExecutor()
playerManager = PlayerManager(this)
playerManager.init()
voiceCreation = VoiceCreation(this)
userSettingsManager = UserSettingsManager()
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/dev/cosgy/textToSpeak/audio/AudioHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ class AudioHandler(private val manager: PlayerManager, guild: Guild, val player:
stringGuildId = guild.id
}

/**
* 再生キューのトップに追加
*/
fun addTrackToFront(qtrack: QueuedTrack): Int {
return if (player.playingTrack == null) {
player.playTrack(qtrack.track)
Expand All @@ -48,6 +51,9 @@ class AudioHandler(private val manager: PlayerManager, guild: Guild, val player:
}
}

/**
* 再生キューに追加
*/
fun addTrack(qtrack: QueuedTrack): Int {
return if (player.playingTrack == null) {
player.playTrack(qtrack.track)
Expand Down
23 changes: 22 additions & 1 deletion src/main/java/dev/cosgy/textToSpeak/audio/VoiceCreation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import dev.cosgy.textToSpeak.Bot
import dev.cosgy.textToSpeak.settings.UserSettings
import net.dv8tion.jda.api.entities.*
import org.apache.commons.io.FileUtils
import com.ibm.icu.text.Transliterator
import org.slf4j.LoggerFactory
import java.io.*
import java.nio.file.Files
Expand Down Expand Up @@ -53,6 +54,9 @@ class VoiceCreation( // 各種設定の値を保持するためのフィール
for ((key, value) in words!!) {
dicMsg = dicMsg.replace(key!!.toRegex(), value!!)
}

toKatakanaIfEnglishExists(dicMsg)

val tmpFilePath = createTmpTextFile(guildId, fileId, dicMsg)


Expand All @@ -66,6 +70,23 @@ class VoiceCreation( // 各種設定の値を保持するためのフィール
return fileName
}

// 英単語をカタカナに変換するメソッド
private fun toKatakanaIfEnglishExists(message: String): String {
var englishExists = false
for (c in message) {
if (c in 'a'..'z' || c in 'A'..'Z') {
englishExists = true
break
}
}
return if (englishExists) {
val latinToKatakana = Transliterator.getInstance("Latin-Katakana")
latinToKatakana.transliterate(message)
} else {
message
}
}

// メッセージをサニタイズするメソッド
private fun sanitizeMessage(message: String): String {
var sanitizedMsg = message.replace("[\\uD800-\\uDFFF]".toRegex(), " ")
Expand All @@ -77,7 +98,7 @@ class VoiceCreation( // 各種設定の値を保持するためのフィール
val builder = StringBuilder()
while (sentences.next() != BreakIterator.DONE) {
val sentence = sanitizedMsg.substring(lastIndex, sentences.current())
if (sentence.length + builder.length > maxMessageCount) {
if (maxMessageCount > 0 && sentence.length + builder.length > maxMessageCount) {
builder.append("以下略")
break
}
Expand Down

0 comments on commit a691d0e

Please sign in to comment.