Skip to content

Commit

Permalink
1.0.0リリース
Browse files Browse the repository at this point in the history
  • Loading branch information
KatatsumuriPan committed Jan 13, 2024
1 parent be6a698 commit c6ad455
Show file tree
Hide file tree
Showing 20 changed files with 2,324 additions and 31 deletions.
15 changes: 13 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ repositories {
// Loom adds the essential maven repositories to download Minecraft and libraries from automatically.
// See https://docs.gradle.org/current/userguide/declaring_repositories.html
// for more information about repositories.

maven { url "https://maven.shedaniel.me/" }
maven { url "https://maven.terraformersmc.com/releases/" }
}

dependencies {
Expand All @@ -26,11 +29,19 @@ dependencies {

// Fabric API. This is technically optional, but you probably want it anyway.
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
// Uncomment the following line to enable the deprecated Fabric API modules.

// Uncomment the following line to enable the deprecated Fabric API modules.
// These are included in the Fabric API production distribution and allow you to update your mod to the latest modules at a later more convenient time.

// modImplementation "net.fabricmc.fabric-api:fabric-api-deprecated:${project.fabric_version}"

modApi( "me.shedaniel.cloth:cloth-config-fabric:10.1.117") {
exclude(group: "net.fabricmc.fabric-api")
exclude(module: "fabric-loader")
}
modApi( "com.terraformersmc:modmenu:6.2.1") {
exclude(module: "fabric-loader")
}
}

processResources {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ loader_version=0.15.3
# Mod Properties
mod_version=1.0.0
maven_group=kpan.b_line_break
archives_base_name=better_line_break
archives_base_name=BetterLineBreak-1.19.4(Fabric)

# Dependencies
fabric_version=0.87.2+1.19.4
179 changes: 179 additions & 0 deletions src/main/java/com/google/budoux/Parser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.budoux;

import com.google.gson.Gson;
import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/*
・HTMLは使用しないので、translateHTMLStringを削除しています。
・totalScoreをキャッシュしています。
*/
public class Parser {
private final Map<String, Map<String, Integer>> model;
private final int totalScore;

/**
* Constructs a BudouX parser.
*
* @param model the model data.
*/
public Parser(Map<String, Map<String, Integer>> model) {
this.model = model;
totalScore =
model.values().stream()
.mapToInt(group -> group.values().stream().mapToInt(Integer::intValue).sum())
.sum();
}

/**
* Loads the default Japanese parser.
*
* @return a BudouX parser with the default Japanese model.
*/
public static Parser loadDefaultJapaneseParser() {
return loadByFileName("/models/ja.json");
}

/**
* Loads the default Simplified Chinese parser.
*
* @return a BudouX parser with the default Simplified Chinese model.
*/
public static Parser loadDefaultSimplifiedChineseParser() {
return loadByFileName("/models/zh-hans.json");
}

/**
* Loads the default Traditional Chinese parser.
*
* @return a BudouX parser with the default Traditional Chinese model.
*/
public static Parser loadDefaultTraditionalChineseParser() {
return loadByFileName("/models/zh-hant.json");
}

/**
* Loads the default Thai parser.
*
* @return a BudouX parser with the default Thai model.
*/
public static Parser loadDefaultThaiParser() {
return loadByFileName("/models/th.json");
}

/**
* Loads a parser by specifying the model file path.
*
* @param modelFileName the model file path.
* @return a BudouX parser.
*/
public static Parser loadByFileName(String modelFileName) {
Gson gson = new Gson();
Type type = new TypeToken<Map<String, Map<String, Integer>>>() {
}.getType();
InputStream inputStream = Parser.class.getResourceAsStream(modelFileName);
try (Reader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8)) {
Map<String, Map<String, Integer>> model = gson.fromJson(reader, type);
return new Parser(model);
} catch (JsonIOException | JsonSyntaxException | IOException e) {
throw new AssertionError(e);
}
}

/**
* Gets the score for the specified feature of the given sequence.
*
* @param featureKey the feature key to examine.
* @param sequence the sequence to look up the score.
* @return the contribution score to support a phrase break.
*/
private int getScore(String featureKey, String sequence) {
return Optional.ofNullable(model.get(featureKey))
.map(group -> group.get(sequence))
.orElse(0);
}

/**
* Parses a sentence into phrases.
*
* @param sentence the sentence to break by phrase.
* @return a list of phrases.
*/
public List<String> parse(String sentence) {
if (sentence.isEmpty()) {
return new ArrayList<>();
}
List<String> result = new ArrayList<>();
result.add(String.valueOf(sentence.charAt(0)));
for (int i = 1; i < sentence.length(); i++) {
int score = -totalScore;
if (i - 2 > 0) {
score += 2 * getScore("UW1", sentence.substring(i - 3, i - 2));
}
if (i - 1 > 0) {
score += 2 * getScore("UW2", sentence.substring(i - 2, i - 1));
}
score += 2 * getScore("UW3", sentence.substring(i - 1, i));
score += 2 * getScore("UW4", sentence.substring(i, i + 1));
if (i + 1 < sentence.length()) {
score += 2 * getScore("UW5", sentence.substring(i + 1, i + 2));
}
if (i + 2 < sentence.length()) {
score += 2 * getScore("UW6", sentence.substring(i + 2, i + 3));
}
if (i > 1) {
score += 2 * getScore("BW1", sentence.substring(i - 2, i));
}
score += 2 * getScore("BW2", sentence.substring(i - 1, i + 1));
if (i + 1 < sentence.length()) {
score += 2 * getScore("BW3", sentence.substring(i, i + 2));
}
if (i - 2 > 0) {
score += 2 * getScore("TW1", sentence.substring(i - 3, i));
}
if (i - 1 > 0) {
score += 2 * getScore("TW2", sentence.substring(i - 2, i + 1));
}
if (i + 1 < sentence.length()) {
score += 2 * getScore("TW3", sentence.substring(i - 1, i + 2));
}
if (i + 2 < sentence.length()) {
score += 2 * getScore("TW4", sentence.substring(i, i + 3));
}
if (score > 0) {
result.add("");
}
result.set(result.size() - 1, result.get(result.size() - 1) + sentence.charAt(i));
}
return result;
}

}
18 changes: 9 additions & 9 deletions src/main/java/kpan/b_line_break/BetterLineBreak.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package kpan.b_line_break;

import kpan.b_line_break.config.ModConfig;
import me.shedaniel.autoconfig.AutoConfig;
import me.shedaniel.autoconfig.serializer.GsonConfigSerializer;
import net.fabricmc.api.ModInitializer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BetterLineBreak implements ModInitializer {
// This logger is used to write text to the console and the log file.
// It is considered best practice to use your mod id as the logger's name.
// That way, it's clear which mod wrote info, warnings, and errors.
public static final Logger LOGGER = LoggerFactory.getLogger("better_line_break");
public static final String MOD_ID = "better_line_break";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);

public static ModConfig config;

@Override
public void onInitialize() {
// This code runs as soon as Minecraft is in a mod-load-ready state.
// However, some things (like resources) may still be uninitialized.
// Proceed with mild caution.

LOGGER.info("Hello Fabric world!");
AutoConfig.register(ModConfig.class, GsonConfigSerializer::new);
config = AutoConfig.getConfigHolder(ModConfig.class).getConfig();
}
}
Loading

0 comments on commit c6ad455

Please sign in to comment.