Skip to content

Commit

Permalink
移出了lombok 感觉做底层不要太多依赖了吧。这个地方省代码不合适
Browse files Browse the repository at this point in the history
  • Loading branch information
ansjsun committed Jul 20, 2015
1 parent 1d1d3a5 commit 54dc708
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 66 deletions.
6 changes: 0 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,6 @@
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
19 changes: 11 additions & 8 deletions src/main/java/org/nlpcn/commons/lang/dat/DATMaker.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.util.List;
import java.util.logging.Logger;

import lombok.SneakyThrows;
import org.nlpcn.commons.lang.tire.domain.SmartForest;
import org.nlpcn.commons.lang.util.FileIterator;
import org.nlpcn.commons.lang.util.IOUtil;
Expand Down Expand Up @@ -42,9 +41,11 @@ public void maker(final String dicPath) throws Exception {

/**
* 构建用户自定义的dat
* @throws FileNotFoundException
* @throws IllegalAccessException
* @throws InstantiationException
*/
@SneakyThrows
public void maker(final String dicPath, final Class<? extends Item> cla) {
public void maker(final String dicPath, final Class<? extends Item> cla) throws FileNotFoundException, InstantiationException, IllegalAccessException {
long start = System.currentTimeMillis();
LOG.info("make basic tire begin !");

Expand Down Expand Up @@ -185,18 +186,20 @@ public Item getPre(final Item item) {

/**
* 将tire树 广度遍历为List
* @throws InstantiationException
*/
private List<Item> tree2List(final Class<? extends Item> cla, final SmartForest<Item> forest) {
private List<Item> tree2List(final Class<? extends Item> cla, final SmartForest<Item> forest) throws InstantiationException, IllegalAccessException {
final List<Item> all = new ArrayList<>();
treeToLibrary(cla, all, forest, "");
return all;
}

/**
* 广度遍历
* @throws IllegalAccessException
* @throws InstantiationException
*/
@SneakyThrows
private void treeToLibrary(final Class<? extends Item> cla, final List<Item> all, final SmartForest<Item> sf, final String preStr) {
private void treeToLibrary(final Class<? extends Item> cla, final List<Item> all, final SmartForest<Item> sf, final String preStr) throws InstantiationException, IllegalAccessException {
final SmartForest<Item>[] branches = sf.getBranches();
if (branches == null) {
return;
Expand Down Expand Up @@ -276,9 +279,9 @@ public void save(final String path) throws IOException {

/**
* 保存到可阅读的文本.需要重写这个类
* @throws IOException
*/
@SneakyThrows
public void saveText(final String path) {
public void saveText(final String path) throws IOException {
try (final Writer writer = new FileWriter(new File(path))) {
writer.write(String.valueOf(datArrLen()));
writer.write('\n');
Expand Down
20 changes: 13 additions & 7 deletions src/main/java/org/nlpcn/commons/lang/dat/DoubleArrayTire.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.nlpcn.commons.lang.dat;

import lombok.SneakyThrows;
import org.nlpcn.commons.lang.util.FileIterator;
import org.nlpcn.commons.lang.util.IOUtil;
import org.nlpcn.commons.lang.util.StringUtil;
Expand Down Expand Up @@ -74,8 +73,7 @@ public <T extends Item> T getItem(int id) {
}


@SneakyThrows
public static DoubleArrayTire load(final String filePath) {
public static DoubleArrayTire load(final String filePath) throws FileNotFoundException, IOException, ClassNotFoundException {
try (final ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(filePath)))) {
final DoubleArrayTire instance = new DoubleArrayTire();
instance.dat = new Item[ois.readInt()];
Expand All @@ -90,16 +88,21 @@ public static DoubleArrayTire load(final String filePath) {

/**
* 从文本中加载模型
* @throws IllegalAccessException
* @throws InstantiationException
* @throws FileNotFoundException
*/
public static DoubleArrayTire loadText(String filePath, Class<? extends Item> cla) {
public static DoubleArrayTire loadText(String filePath, Class<? extends Item> cla) throws FileNotFoundException, InstantiationException, IllegalAccessException {
return loadText(IOUtil.getInputStream(filePath), cla);
}

/**
* 从文本中加载模型
* @throws FileNotFoundException
* @throws IllegalAccessException
* @throws InstantiationException
*/
@SneakyThrows
public static DoubleArrayTire loadText(InputStream is, Class<? extends Item> cla) {
public static DoubleArrayTire loadText(InputStream is, Class<? extends Item> cla) throws FileNotFoundException, InstantiationException, IllegalAccessException {
final DoubleArrayTire obj = new DoubleArrayTire();
final FileIterator it = IOUtil.instanceFileIterator(is, IOUtil.UTF8);
if (it == null) {
Expand All @@ -119,8 +122,11 @@ public static DoubleArrayTire loadText(InputStream is, Class<? extends Item> cla

/**
* 从文本中加载模型
* @throws IllegalAccessException
* @throws InstantiationException
* @throws FileNotFoundException
*/
public static DoubleArrayTire loadText(String filePath) {
public static DoubleArrayTire loadText(String filePath) throws FileNotFoundException, InstantiationException, IllegalAccessException {
return loadText(filePath, BasicItem.class);
}
}
108 changes: 72 additions & 36 deletions src/main/java/org/nlpcn/commons/lang/dat/Item.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package org.nlpcn.commons.lang.dat;

import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
Expand All @@ -11,39 +8,78 @@
* @author ansj
*
*/
@Setter
@Getter
public abstract class Item implements Serializable {

private static final long serialVersionUID = 1L;

protected String name;
protected byte status;
protected int base = 65536;
protected int index;
protected int check;

/**
* 从词典中加载如果又特殊需求可重写此构造方法
*
* @param split split
*/
public abstract void init(String[] split);

/**
* 从生成的词典中加载。应该和toText方法对应
*
* @param split split
*/
public abstract void initValue(String[] split);

/**
* @return 以文本格式序列化的显示
*/
public abstract String toText();

@Override
public String toString() {
return this.toText();
}
private static final long serialVersionUID = 1L;

protected String name;
protected byte status;
protected int base = 65536;
protected int index;
protected int check;

/**
* 从词典中加载如果又特殊需求可重写此构造方法
*
* @param split split
*/
public abstract void init(String[] split);

/**
* 从生成的词典中加载。应该和toText方法对应
*
* @param split split
*/
public abstract void initValue(String[] split);

/**
* @return 以文本格式序列化的显示
*/
public abstract String toText();

@Override
public String toString() {
return this.toText();
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public byte getStatus() {
return status;
}

public void setStatus(byte status) {
this.status = status;
}

public int getBase() {
return base;
}

public void setBase(int base) {
this.base = base;
}

public int getIndex() {
return index;
}

public void setIndex(int index) {
this.index = index;
}

public int getCheck() {
return check;
}

public void setCheck(int check) {
this.check = check;
}

}
Empty file.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.nlpcn.commons.lang.tire.domain;

import lombok.Getter;
import org.nlpcn.commons.lang.util.AnsjArrays;

import java.lang.reflect.Array;
Expand All @@ -10,7 +9,6 @@ public abstract class AbstractWood<P, B extends AbstractWood<P, B>> implements W

protected static final Integer MAX_SIZE = 65536;

@Getter
protected B[] branches = null;
// 单独查找出来的对象
protected B branch = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.nlpcn.commons.lang.tire.SmartGetWord;


/**
* 一个小树,和Forest的区别是.这个在首字也是用二分查找,做过一次优化.达到到达一定量级自动扩展为hash定位 在ansj分词中这个应用是在自适应分词
*
Expand Down Expand Up @@ -119,4 +118,9 @@ protected void onNatureIdentified(final P param, final boolean append) {
protected SmartForest<P> newBranch(char c, int status, P param) {
return new SmartForest<>(c, status, param);
}

@SuppressWarnings("unchecked")
public <B> B[] getBranches() {
return (B[]) branches;
}
}
16 changes: 10 additions & 6 deletions src/main/java/org/nlpcn/commons/lang/tire/domain/Value.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
package org.nlpcn.commons.lang.tire.domain;

import lombok.Getter;
import lombok.Setter;

import java.util.Arrays;

@Setter
@Getter
public class Value {

private static final String TAB = "\t";

private String keyword;
private String keyword;
private String[] paramers;

public Value(final String keyword, final String... paramers) {
Expand All @@ -34,6 +29,15 @@ public Value(final String value) {
}
}

public String getKeyword() {
return keyword;
}


public String[] getParamers() {
return paramers;
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
Expand Down

0 comments on commit 54dc708

Please sign in to comment.