Skip to content

Commit

Permalink
Merge pull request #1 from KatatsumuriPan/develop
Browse files Browse the repository at this point in the history
初リリース
  • Loading branch information
KatatsumuriPan authored Jan 9, 2024
2 parents 81a7099 + 91c4693 commit 4720416
Show file tree
Hide file tree
Showing 26 changed files with 2,222 additions and 225 deletions.
2 changes: 1 addition & 1 deletion dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
* For more details, see https://docs.gradle.org/8.0.1/userguide/java_library_plugin.html#sec:java_library_configurations_graph
*/
dependencies {

compileOnly rfg.deobf("curse.maven:smooth-font-285742:3944565")
}
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;
}

}
6 changes: 3 additions & 3 deletions src/main/java/kpan/b_line_break/ModMain.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package kpan.b_line_break;

import kpan.b_line_break.config.core.ConfigHandler;
import kpan.b_line_break.config.ConfigHolder;
import kpan.b_line_break.config.core.ConfigHandler;
import kpan.b_line_break.proxy.CommonProxy;
import kpan.b_line_break.util.handlers.RegistryHandler;
import net.minecraft.server.MinecraftServer;
Expand Down Expand Up @@ -35,7 +35,7 @@ public class ModMain {
public static CommonProxy proxy;

public static final Logger LOGGER = LogManager.getLogger(ModTagsGenerated.MODNAME);

@Nullable
public static MinecraftServer server = null;
public static final ConfigHandler defaultConfig = new ConfigHandler(ConfigHolder.class, ModTagsGenerated.MODID, ConfigHolder.getVersion(), ConfigHolder::updateVersion);
Expand All @@ -59,7 +59,7 @@ public static void preInit(FMLPreInitializationEvent event) {
public static void onServerAboutToStart(FMLServerAboutToStartEvent event) {
server = event.getServer();
}

@EventHandler
public static void onServerStopped(FMLServerStoppedEvent event) {
server = null;
Expand Down
32 changes: 0 additions & 32 deletions src/main/java/kpan/b_line_break/asm/acc/ACC_FontRenderer.java

This file was deleted.

47 changes: 0 additions & 47 deletions src/main/java/kpan/b_line_break/asm/acc/ACC_TileEntityFurnace.java

This file was deleted.

20 changes: 20 additions & 0 deletions src/main/java/kpan/b_line_break/asm/compat/CompatOptifine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package kpan.b_line_break.asm.compat;

public class CompatOptifine {
private static final boolean loaded;

static {
boolean tmp;
try {
Class.forName("optifine.Patcher");
tmp = true;
} catch (ClassNotFoundException e) {
tmp = false;
}
loaded = tmp;
}

public static boolean isLoaded() {
return loaded;
}
}
20 changes: 20 additions & 0 deletions src/main/java/kpan/b_line_break/asm/compat/CompatSmoothFont.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package kpan.b_line_break.asm.compat;

public class CompatSmoothFont {
private static final boolean loaded;

static {
boolean tmp;
try {
Class.forName("bre.smoothfont.asm.AsmHelper");
tmp = true;
} catch (ClassNotFoundException e) {
tmp = false;
}
loaded = tmp;
}

public static boolean isLoaded() {
return loaded;
}
}
4 changes: 2 additions & 2 deletions src/main/java/kpan/b_line_break/asm/core/ASMTransformer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import kpan.b_line_break.asm.core.adapters.MixinAccessorAdapter;
import kpan.b_line_break.asm.tf.TF_FontRenderer;
import kpan.b_line_break.asm.tf.TF_TileEntityFurnace;
import kpan.b_line_break.asm.tf.integration.smoothfont.TF_FontRendererHook;
import net.minecraft.launchwrapper.IClassTransformer;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
Expand Down Expand Up @@ -31,7 +31,7 @@ public byte[] transform(String name, String transformedName, byte[] bytes) {
ClassVisitor cv = cw;
cv = MixinAccessorAdapter.transformAccessor(cv, transformedName);
cv = TF_FontRenderer.appendVisitor(cv, transformedName);
cv = TF_TileEntityFurnace.appendVisitor(cv, transformedName);
cv = TF_FontRendererHook.appendVisitor(cv, transformedName);

if (cv == cw)
return bytes;
Expand Down
Loading

0 comments on commit 4720416

Please sign in to comment.