Skip to content

Commit

Permalink
Reformat codes
Browse files Browse the repository at this point in the history
  • Loading branch information
hankcs committed Apr 1, 2018
1 parent a3d5a5e commit 438023c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
26 changes: 21 additions & 5 deletions src/main/java/com/hankcs/algorithm/AhoCorasickDoubleArrayTrie.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public class AhoCorasickDoubleArrayTrie<V> implements Serializable

/**
* Parse text
*
* @param text The text
* @return a list of outputs
*/
Expand All @@ -86,7 +87,8 @@ public List<Hit<V>> parseText(String text)

/**
* Parse text
* @param text The text
*
* @param text The text
* @param processor A processor which handles the output
*/
public void parseText(String text, IHit<V> processor)
Expand All @@ -110,7 +112,8 @@ public void parseText(String text, IHit<V> processor)

/**
* Parse text
* @param text The text
*
* @param text The text
* @param processor A processor which handles the output
*/
public void parseText(String text, IHitCancellable<V> processor)
Expand All @@ -126,7 +129,8 @@ public void parseText(String text, IHitCancellable<V> processor)
for (int hit : hitArray)
{
boolean proceed = processor.hit(position - l[hit], position, v[hit]);
if(!proceed) {
if (!proceed)
{
return;
}
}
Expand All @@ -136,7 +140,8 @@ public void parseText(String text, IHitCancellable<V> processor)

/**
* Parse text
* @param text The text
*
* @param text The text
* @param processor A processor which handles the output
*/
public void parseText(char[] text, IHit<V> processor)
Expand All @@ -160,7 +165,8 @@ public void parseText(char[] text, IHit<V> processor)

/**
* Parse text
* @param text The text
*
* @param text The text
* @param processor A processor which handles the output
*/
public void parseText(char[] text, IHitFull<V> processor)
Expand Down Expand Up @@ -230,6 +236,7 @@ public Hit<V> findFirst(String text)

/**
* Save
*
* @param out An ObjectOutputStream object
* @throws IOException Some IOException
*/
Expand All @@ -245,6 +252,7 @@ public void save(ObjectOutputStream out) throws IOException

/**
* Load
*
* @param in An ObjectInputStream object
* @throws IOException
* @throws ClassNotFoundException
Expand All @@ -261,6 +269,7 @@ public void load(ObjectInputStream in) throws IOException, ClassNotFoundExceptio

/**
* Get value by a String key, just like a map.get() method
*
* @param key The key
* @return
*/
Expand All @@ -278,6 +287,7 @@ public V get(String key)
/**
* Pick the value by index in value array <br>
* Notice that to be more efficiently, this method DO NOT check the parameter
*
* @param index The index
* @return The value
*/
Expand All @@ -293,6 +303,7 @@ public interface IHit<V>
{
/**
* Hit a keyword, you can use some code like text.substring(begin, end) to get the keyword
*
* @param begin the beginning index, inclusive.
* @param end the ending index, exclusive.
* @param value the value assigned to the keyword
Expand All @@ -307,6 +318,7 @@ public interface IHitFull<V>
{
/**
* Hit a keyword, you can use some code like text.substring(begin, end) to get the keyword
*
* @param begin the beginning index, inclusive.
* @param end the ending index, exclusive.
* @param value the value assigned to the keyword
Expand All @@ -322,6 +334,7 @@ public interface IHitCancellable<V>
{
/**
* Hit a keyword, you can use some code like text.substring(begin, end) to get the keyword
*
* @param begin the beginning index, inclusive.
* @param end the ending index, exclusive.
* @param value the value assigned to the keyword
Expand Down Expand Up @@ -448,6 +461,7 @@ protected int transitionWithRoot(int nodePos, char c)

/**
* Build a AhoCorasickDoubleArrayTrie from a map
*
* @param map a map containing key-value pairs
*/
public void build(Map<String, V> map)
Expand Down Expand Up @@ -650,6 +664,7 @@ private int exactMatchSearch(char[] keyChars, int pos, int len, int nodePos)

/**
* Get the size of the keywords
*
* @return
*/
public int size()
Expand Down Expand Up @@ -689,6 +704,7 @@ private class Builder

/**
* Build from a map
*
* @param map a map containing key-value pairs
*/
@SuppressWarnings("unchecked")
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/com/hankcs/algorithm/State.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public State()

/**
* 构造深度为depth的节点
*
* @param depth
*/
public State(int depth)
Expand All @@ -85,6 +86,7 @@ public State(int depth)

/**
* 获取节点深度
*
* @return
*/
public int getDepth()
Expand All @@ -94,6 +96,7 @@ public int getDepth()

/**
* 添加一个匹配到的模式串(这个状态对应着这个模式串)
*
* @param keyword
*/
public void addEmit(int keyword)
Expand All @@ -107,6 +110,7 @@ public void addEmit(int keyword)

/**
* 获取最大的值
*
* @return
*/
public Integer getLargestValueId()
Expand All @@ -118,6 +122,7 @@ public Integer getLargestValueId()

/**
* 添加一些匹配到的模式串
*
* @param emits
*/
public void addEmit(Collection<Integer> emits)
Expand All @@ -130,6 +135,7 @@ public void addEmit(Collection<Integer> emits)

/**
* 获取这个节点代表的模式串(们)
*
* @return
*/
public Collection<Integer> emit()
Expand All @@ -139,6 +145,7 @@ public Collection<Integer> emit()

/**
* 是否是终止状态
*
* @return
*/
public boolean isAcceptable()
Expand All @@ -148,6 +155,7 @@ public boolean isAcceptable()

/**
* 获取failure状态
*
* @return
*/
public State failure()
Expand All @@ -157,6 +165,7 @@ public State failure()

/**
* 设置failure状态
*
* @param failState
*/
public void setFailure(State failState, int fail[])
Expand All @@ -167,7 +176,8 @@ public void setFailure(State failState, int fail[])

/**
* 转移到下一个状态
* @param character 希望按此字符转移
*
* @param character 希望按此字符转移
* @param ignoreRootState 是否忽略根节点,如果是根节点自己调用则应该是true,否则为false
* @return 转移结果
*/
Expand All @@ -183,6 +193,7 @@ private State nextState(Character character, boolean ignoreRootState)

/**
* 按照character转移,根节点转移失败会返回自己(永远不会返回null)
*
* @param character
* @return
*/
Expand All @@ -193,6 +204,7 @@ public State nextState(Character character)

/**
* 按照character转移,任何节点转移失败会返回null
*
* @param character
* @return
*/
Expand Down Expand Up @@ -238,6 +250,7 @@ public String toString()

/**
* 获取goto表
*
* @return
*/
public Map<Character, State> getSuccess()
Expand Down
22 changes: 15 additions & 7 deletions src/test/java/TestAhoCorasickDoubleArrayTrie.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,27 +102,32 @@ public void hit(int begin, int end, String value)
});
}

private static class CountHits implements AhoCorasickDoubleArrayTrie.IHitCancellable<String> {
private static class CountHits implements AhoCorasickDoubleArrayTrie.IHitCancellable<String>
{
private int count;
private boolean countAll;

CountHits(boolean countAll) {
CountHits(boolean countAll)
{
this.count = 0;
this.countAll = countAll;
}

public int getCount() {
public int getCount()
{
return count;
}

@Override
public boolean hit(int begin, int end, String value) {
public boolean hit(int begin, int end, String value)
{
count += 1;
return countAll;
}
}

public void testMatches() {
public void testMatches()
{
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("space", 1);
map.put("keyword", 2);
Expand All @@ -142,7 +147,8 @@ public void testMatches() {
assertFalse(trie.matches("nothing"));
}

public void testFirstMatch() {
public void testFirstMatch()
{
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("space", 1);
map.put("keyword", 2);
Expand All @@ -166,7 +172,8 @@ public void testFirstMatch() {
assertNull(trie.findFirst(" no pace"));
}

public void testCancellation() throws Exception {
public void testCancellation() throws Exception
{
// Collect test data set
TreeMap<String, String> map = new TreeMap<String, String>();
String[] keyArray = new String[]
Expand Down Expand Up @@ -265,6 +272,7 @@ public void hit(int begin, int end, String value)
* Compare my AhoCorasickDoubleArrayTrie with robert-bor's aho-corasick, notice that robert-bor's aho-corasick is
* compiled under jdk1.8, so you will need jdk1.8 to run this test<br>
* To avoid JVM wasting time on allocating memory, please use -Xms512m -Xmx512m -Xmn256m .
*
* @throws Exception
*/
public void testBenchmark() throws Exception
Expand Down

0 comments on commit 438023c

Please sign in to comment.