This commit is contained in:
Looly 2023-04-24 11:54:39 +08:00
parent e67bbdec19
commit 26b99c7b08
18 changed files with 137 additions and 50 deletions

View File

@ -12,9 +12,7 @@
package org.dromara.hutool.extra.pinyin; package org.dromara.hutool.extra.pinyin;
import org.dromara.hutool.core.exception.ExceptionUtil;
import org.dromara.hutool.core.exception.HutoolException; import org.dromara.hutool.core.exception.HutoolException;
import org.dromara.hutool.core.text.StrUtil;
/** /**
* 模板异常 * 模板异常

View File

@ -12,38 +12,74 @@
package org.dromara.hutool.extra.qrcode; package org.dromara.hutool.extra.qrcode;
import org.dromara.hutool.core.exception.ExceptionUtil; import org.dromara.hutool.core.exception.HutoolException;
import org.dromara.hutool.core.text.StrUtil;
/** /**
* Qrcode异常 * Qrcode异常
* *
* @author xiaoleilu * @author Looly
*/ */
public class QrCodeException extends RuntimeException { public class QrCodeException extends HutoolException {
private static final long serialVersionUID = 8247610319171014183L; private static final long serialVersionUID = 8247610319171014183L;
/**
* 构造
*
* @param e 异常
*/
public QrCodeException(final Throwable e) { public QrCodeException(final Throwable e) {
super(ExceptionUtil.getMessage(e), e); super(e);
} }
/**
* 构造
*
* @param message 消息
*/
public QrCodeException(final String message) { public QrCodeException(final String message) {
super(message); super(message);
} }
/**
* 构造
*
* @param messageTemplate 消息模板
* @param params 参数
*/
public QrCodeException(final String messageTemplate, final Object... params) { public QrCodeException(final String messageTemplate, final Object... params) {
super(StrUtil.format(messageTemplate, params)); super(messageTemplate, params);
} }
public QrCodeException(final String message, final Throwable throwable) { /**
super(message, throwable); * 构造
*
* @param message 消息
* @param cause 被包装的子异常
*/
public QrCodeException(final String message, final Throwable cause) {
super(message, cause);
} }
public QrCodeException(final String message, final Throwable throwable, final boolean enableSuppression, final boolean writableStackTrace) { /**
super(message, throwable, enableSuppression, writableStackTrace); * 构造
*
* @param message 消息
* @param cause 被包装的子异常
* @param enableSuppression 是否启用抑制
* @param writableStackTrace 堆栈跟踪是否应该是可写的
*/
public QrCodeException(final String message, final Throwable cause, final boolean enableSuppression, final boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
} }
public QrCodeException(final Throwable throwable, final String messageTemplate, final Object... params) { /**
super(StrUtil.format(messageTemplate, params), throwable); * 构造
*
* @param cause 被包装的子异常
* @param messageTemplate 消息模板
* @param params 参数
*/
public QrCodeException(final Throwable cause, final String messageTemplate, final Object... params) {
super(cause, messageTemplate, params);
} }
} }

View File

@ -373,7 +373,7 @@ public class QrConfig {
* @param shapeHint DATA_MATRIX的符号形状 * @param shapeHint DATA_MATRIX的符号形状
* @return this * @return this
*/ */
public QrConfig setShapeHint(SymbolShapeHint shapeHint) { public QrConfig setShapeHint(final SymbolShapeHint shapeHint) {
this.shapeHint = shapeHint; this.shapeHint = shapeHint;
return this; return this;
} }
@ -393,7 +393,7 @@ public class QrConfig {
* @param format 码格式 * @param format 码格式
* @return this * @return this
*/ */
public QrConfig setFormat(BarcodeFormat format) { public QrConfig setFormat(final BarcodeFormat format) {
this.format = format; this.format = format;
return this; return this;
} }

View File

@ -12,38 +12,74 @@
package org.dromara.hutool.extra.tokenizer; package org.dromara.hutool.extra.tokenizer;
import org.dromara.hutool.core.exception.ExceptionUtil; import org.dromara.hutool.core.exception.HutoolException;
import org.dromara.hutool.core.text.StrUtil;
/** /**
* 分词异常 * 分词异常
* *
* @author Looly * @author Looly
*/ */
public class TokenizerException extends RuntimeException { public class TokenizerException extends HutoolException {
private static final long serialVersionUID = 8074865854534335463L; private static final long serialVersionUID = 8074865854534335463L;
/**
* 构造
*
* @param e 异常
*/
public TokenizerException(final Throwable e) { public TokenizerException(final Throwable e) {
super(ExceptionUtil.getMessage(e), e); super(e);
} }
/**
* 构造
*
* @param message 消息
*/
public TokenizerException(final String message) { public TokenizerException(final String message) {
super(message); super(message);
} }
/**
* 构造
*
* @param messageTemplate 消息模板
* @param params 参数
*/
public TokenizerException(final String messageTemplate, final Object... params) { public TokenizerException(final String messageTemplate, final Object... params) {
super(StrUtil.format(messageTemplate, params)); super(messageTemplate, params);
} }
public TokenizerException(final String message, final Throwable throwable) { /**
super(message, throwable); * 构造
*
* @param message 消息
* @param cause 被包装的子异常
*/
public TokenizerException(final String message, final Throwable cause) {
super(message, cause);
} }
public TokenizerException(final String message, final Throwable throwable, final boolean enableSuppression, final boolean writableStackTrace) { /**
super(message, throwable, enableSuppression, writableStackTrace); * 构造
*
* @param message 消息
* @param cause 被包装的子异常
* @param enableSuppression 是否启用抑制
* @param writableStackTrace 堆栈跟踪是否应该是可写的
*/
public TokenizerException(final String message, final Throwable cause, final boolean enableSuppression, final boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
} }
public TokenizerException(final Throwable throwable, final String messageTemplate, final Object... params) { /**
super(StrUtil.format(messageTemplate, params), throwable); * 构造
*
* @param cause 被包装的子异常
* @param messageTemplate 消息模板
* @param params 参数
*/
public TokenizerException(final Throwable cause, final String messageTemplate, final Object... params) {
super(cause, messageTemplate, params);
} }
} }

View File

@ -12,7 +12,8 @@
package org.dromara.hutool.extra.tokenizer; package org.dromara.hutool.extra.tokenizer;
import org.dromara.hutool.extra.tokenizer.engine.TokenizerFactory; import org.dromara.hutool.extra.tokenizer.engine.TokenizerEngine;
import org.dromara.hutool.extra.tokenizer.engine.TokenizerEngineFactory;
/** /**
* 分词工具类 * 分词工具类
@ -22,12 +23,22 @@ import org.dromara.hutool.extra.tokenizer.engine.TokenizerFactory;
*/ */
public class TokenizerUtil { public class TokenizerUtil {
/**
* 分词处理
*
* @param text 文本
* @return 分词结果
*/
public static Result parse(final String text) {
return getEngine().parse(text);
}
/** /**
* 根据用户引入的分词引擎jar自动创建对应的分词引擎对象 * 根据用户引入的分词引擎jar自动创建对应的分词引擎对象
* *
* @return {@link TokenizerEngine} * @return {@link TokenizerEngine}
*/ */
public static TokenizerEngine createEngine() { public static TokenizerEngine getEngine() {
return TokenizerFactory.of(); return TokenizerEngineFactory.getEngine();
} }
} }

View File

@ -10,7 +10,9 @@
* See the Mulan PSL v2 for more details. * See the Mulan PSL v2 for more details.
*/ */
package org.dromara.hutool.extra.tokenizer; package org.dromara.hutool.extra.tokenizer.engine;
import org.dromara.hutool.extra.tokenizer.Result;
/** /**
* 分词引擎接口定义用户通过实现此接口完成特定分词引擎的适配 * 分词引擎接口定义用户通过实现此接口完成特定分词引擎的适配

View File

@ -15,7 +15,6 @@ package org.dromara.hutool.extra.tokenizer.engine;
import org.dromara.hutool.core.lang.Singleton; import org.dromara.hutool.core.lang.Singleton;
import org.dromara.hutool.core.spi.SpiUtil; import org.dromara.hutool.core.spi.SpiUtil;
import org.dromara.hutool.core.text.StrUtil; import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.extra.tokenizer.TokenizerEngine;
import org.dromara.hutool.extra.tokenizer.TokenizerException; import org.dromara.hutool.extra.tokenizer.TokenizerException;
import org.dromara.hutool.log.StaticLog; import org.dromara.hutool.log.StaticLog;
@ -23,19 +22,17 @@ import org.dromara.hutool.log.StaticLog;
* 简单分词引擎工厂用于根据用户引入的分词引擎jar自动创建对应的引擎 * 简单分词引擎工厂用于根据用户引入的分词引擎jar自动创建对应的引擎
* *
* @author looly * @author looly
*
*/ */
public class TokenizerFactory { public class TokenizerEngineFactory {
/** /**
* 根据用户引入的模板引擎jar自动创建对应的分词引擎对象<br> * 根据用户引入的模板引擎jar自动创建对应的分词引擎对象<br>
* 获得的是单例的TokenizerEngine * 获得的是单例的TokenizerEngine
* *
* @return 单例的TokenizerEngine * @return 单例的TokenizerEngine
* @since 5.3.3
*/ */
public static TokenizerEngine get(){ public static TokenizerEngine getEngine(){
return Singleton.get(TokenizerEngine.class.getName(), TokenizerFactory::of); return Singleton.get(TokenizerEngine.class.getName(), TokenizerEngineFactory::createEngine);
} }
/** /**
@ -43,8 +40,8 @@ public class TokenizerFactory {
* *
* @return {@link TokenizerEngine} * @return {@link TokenizerEngine}
*/ */
public static TokenizerEngine of() { public static TokenizerEngine createEngine() {
final TokenizerEngine engine = doCreate(); final TokenizerEngine engine = doCreateEngine();
StaticLog.debug("Use [{}] Tokenizer Engine As Default.", StrUtil.removeSuffix(engine.getClass().getSimpleName(), "Engine")); StaticLog.debug("Use [{}] Tokenizer Engine As Default.", StrUtil.removeSuffix(engine.getClass().getSimpleName(), "Engine"));
return engine; return engine;
} }
@ -54,7 +51,7 @@ public class TokenizerFactory {
* *
* @return {@link TokenizerEngine} * @return {@link TokenizerEngine}
*/ */
private static TokenizerEngine doCreate() { private static TokenizerEngine doCreateEngine() {
final TokenizerEngine engine = SpiUtil.loadFirstAvailable(TokenizerEngine.class); final TokenizerEngine engine = SpiUtil.loadFirstAvailable(TokenizerEngine.class);
if(null != engine){ if(null != engine){
return engine; return engine;

View File

@ -19,7 +19,7 @@ import org.apache.lucene.analysis.TokenStream;
import org.dromara.hutool.core.text.StrUtil; import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.extra.tokenizer.Result; import org.dromara.hutool.extra.tokenizer.Result;
import org.dromara.hutool.extra.tokenizer.TokenizerEngine; import org.dromara.hutool.extra.tokenizer.engine.TokenizerEngine;
import org.dromara.hutool.extra.tokenizer.TokenizerException; import org.dromara.hutool.extra.tokenizer.TokenizerException;
/** /**

View File

@ -17,7 +17,7 @@ import org.ansj.splitWord.analysis.ToAnalysis;
import org.dromara.hutool.core.text.StrUtil; import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.extra.tokenizer.Result; import org.dromara.hutool.extra.tokenizer.Result;
import org.dromara.hutool.extra.tokenizer.TokenizerEngine; import org.dromara.hutool.extra.tokenizer.engine.TokenizerEngine;
/** /**
* Ansj分词引擎实现<br> * Ansj分词引擎实现<br>

View File

@ -16,7 +16,7 @@ import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.seg.Segment; import com.hankcs.hanlp.seg.Segment;
import org.dromara.hutool.core.text.StrUtil; import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.extra.tokenizer.TokenizerEngine; import org.dromara.hutool.extra.tokenizer.engine.TokenizerEngine;
import org.dromara.hutool.extra.tokenizer.Result; import org.dromara.hutool.extra.tokenizer.Result;
/** /**

View File

@ -15,7 +15,7 @@ package org.dromara.hutool.extra.tokenizer.engine.ikanalyzer;
import org.wltea.analyzer.core.IKSegmenter; import org.wltea.analyzer.core.IKSegmenter;
import org.dromara.hutool.core.text.StrUtil; import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.extra.tokenizer.TokenizerEngine; import org.dromara.hutool.extra.tokenizer.engine.TokenizerEngine;
import org.dromara.hutool.extra.tokenizer.Result; import org.dromara.hutool.extra.tokenizer.Result;
/** /**

View File

@ -14,7 +14,7 @@ package org.dromara.hutool.extra.tokenizer.engine.jcseg;
import org.dromara.hutool.core.text.StrUtil; import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.extra.tokenizer.Result; import org.dromara.hutool.extra.tokenizer.Result;
import org.dromara.hutool.extra.tokenizer.TokenizerEngine; import org.dromara.hutool.extra.tokenizer.engine.TokenizerEngine;
import org.dromara.hutool.extra.tokenizer.TokenizerException; import org.dromara.hutool.extra.tokenizer.TokenizerException;
import org.lionsoul.jcseg.ISegment; import org.lionsoul.jcseg.ISegment;
import org.lionsoul.jcseg.dic.ADictionary; import org.lionsoul.jcseg.dic.ADictionary;

View File

@ -16,7 +16,7 @@ import com.huaban.analysis.jieba.JiebaSegmenter;
import com.huaban.analysis.jieba.JiebaSegmenter.SegMode; import com.huaban.analysis.jieba.JiebaSegmenter.SegMode;
import org.dromara.hutool.core.text.StrUtil; import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.extra.tokenizer.TokenizerEngine; import org.dromara.hutool.extra.tokenizer.engine.TokenizerEngine;
import org.dromara.hutool.extra.tokenizer.Result; import org.dromara.hutool.extra.tokenizer.Result;
/** /**

View File

@ -14,7 +14,7 @@ package org.dromara.hutool.extra.tokenizer.engine.mmseg;
import org.dromara.hutool.core.text.StrUtil; import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.extra.tokenizer.Result; import org.dromara.hutool.extra.tokenizer.Result;
import org.dromara.hutool.extra.tokenizer.TokenizerEngine; import org.dromara.hutool.extra.tokenizer.engine.TokenizerEngine;
import com.chenlb.mmseg4j.ComplexSeg; import com.chenlb.mmseg4j.ComplexSeg;
import com.chenlb.mmseg4j.Dictionary; import com.chenlb.mmseg4j.Dictionary;
import com.chenlb.mmseg4j.MMSeg; import com.chenlb.mmseg4j.MMSeg;

View File

@ -17,7 +17,7 @@ import com.mayabot.nlp.segment.Lexer;
import com.mayabot.nlp.segment.Sentence; import com.mayabot.nlp.segment.Sentence;
import org.dromara.hutool.core.text.StrUtil; import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.extra.tokenizer.Result; import org.dromara.hutool.extra.tokenizer.Result;
import org.dromara.hutool.extra.tokenizer.TokenizerEngine; import org.dromara.hutool.extra.tokenizer.engine.TokenizerEngine;
/** /**
* MYNLP 中文NLP工具包分词实现<br> * MYNLP 中文NLP工具包分词实现<br>

View File

@ -18,7 +18,7 @@ import org.apdplat.word.segmentation.SegmentationFactory;
import org.dromara.hutool.core.text.StrUtil; import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.extra.tokenizer.Result; import org.dromara.hutool.extra.tokenizer.Result;
import org.dromara.hutool.extra.tokenizer.TokenizerEngine; import org.dromara.hutool.extra.tokenizer.engine.TokenizerEngine;
/** /**
* Word分词引擎实现<br> * Word分词引擎实现<br>

View File

@ -1,6 +1,7 @@
package org.dromara.hutool.extra.tokenizer; package org.dromara.hutool.extra.tokenizer;
import org.dromara.hutool.core.collection.iter.IterUtil; import org.dromara.hutool.core.collection.iter.IterUtil;
import org.dromara.hutool.extra.tokenizer.engine.TokenizerEngine;
import org.dromara.hutool.extra.tokenizer.engine.analysis.SmartcnEngine; import org.dromara.hutool.extra.tokenizer.engine.analysis.SmartcnEngine;
import org.dromara.hutool.extra.tokenizer.engine.hanlp.HanLPEngine; import org.dromara.hutool.extra.tokenizer.engine.hanlp.HanLPEngine;
import org.dromara.hutool.extra.tokenizer.engine.ikanalyzer.IKAnalyzerEngine; import org.dromara.hutool.extra.tokenizer.engine.ikanalyzer.IKAnalyzerEngine;
@ -22,10 +23,16 @@ public class TokenizerUtilTest {
String text = "这两个方法的区别在于返回值"; String text = "这两个方法的区别在于返回值";
@Test
void parseTest() {
final Result result = TokenizerUtil.parse(text);
checkResult(result);
}
@Test @Test
public void createEngineTest() { public void createEngineTest() {
// 默认分词引擎此处为Ansj // 默认分词引擎此处为Ansj
final TokenizerEngine engine = TokenizerUtil.createEngine(); final TokenizerEngine engine = TokenizerUtil.getEngine();
final Result result = engine.parse(text); final Result result = engine.parse(text);
checkResult(result); checkResult(result);
} }