Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/refactor/base/AlphaBeta.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Created锛�May 10, 2013 2:18:41 PM
* Project锛�ThulacJava
* @author cxx
* @since JDK 1.6.0_13
* filename锛�AlphaBeta.java
* description锛�
*/
package base;


//a structure for alphas and betas
public class AlphaBeta {
public int value;
public int nodeId;
public int labelId;

public AlphaBeta() {
super();
value = 0;
nodeId = -2;
labelId = 0;
}

public AlphaBeta(int value, int nodeId, int labelId) {
super();
this.value = value;
this.nodeId = nodeId;
this.labelId = labelId;
}



}
26 changes: 26 additions & 0 deletions src/refactor/base/Counter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Created锛�May 9, 2013 2:32:10 PM
* Project锛�ThulacJava
* @author cxx
* @since JDK 1.6.0_13
* filename锛�Counter.java
* description锛�
*/
package base;

import java.util.HashMap;

public class Counter<KeyType> extends HashMap<KeyType, Integer> {
/**
*
*/
private static final long serialVersionUID = 1L;

public void update(KeyType key){
Integer value = get(key);
if(value == null){
value = 0;
}
put(key, value + 1);
}
}
140 changes: 140 additions & 0 deletions src/refactor/base/Dat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package base;


import java.io.*;
import java.util.Vector;

public class Dat {

public Vector<Entry> dat;
public int datSize;

public Dat(){
dat = new Vector<Entry>();
datSize = 0;
}
public Dat(int datSize, Vector<Entry> olddat){
this.datSize = datSize;
dat= new Vector<Entry>();
for(int i = 0; i < datSize; i ++){
dat.add(new Entry());
dat.get(i).base = olddat.get(i).base;
dat.get(i).check = olddat.get(i).check;
}
}
public Dat(String filename) throws IOException {
filename = Dat.class.getClassLoader().getResource(filename).getFile();
File file = new File(filename);
datSize = (int)(file.length() / 8);
//System.out.println(datSize);

FileInputStream in = new FileInputStream(file);

byte[] tempbytes = new byte[8 * datSize];
dat = new Vector<Entry>();
in.read(tempbytes);
for(int i = 0; i < datSize; i ++){
Entry entry = new Entry();
entry.base = bytesToInt(tempbytes, 8 * i);

dat.add(entry);
dat.get(i).check = bytesToInt(tempbytes, 8 * i + 4);
}

in.close();
}

public static int bytesToInt(byte[] bb, int index) {
return (int) (((((int)bb[index + 3] & 0xff) << 24)
| (((int)bb[index + 2] & 0xff) << 16)
| (((int)bb[index + 1] & 0xff) << 8) | (((int)bb[index + 0] & 0xff) << 0)));
}

public static byte[] intToBytes(int n){
byte[] b = new byte[4];
for(int i = 0;i < 4;i++){
b[i] = (byte)(n >> (8 * i));
}
return b;
}

public void save(String filename) throws IOException{
FileOutputStream out = new FileOutputStream(filename);
for(Entry e : dat){
out.write(intToBytes(e.base));
}
out.flush();
for(Entry e : dat){
out.write(intToBytes(e.check));
}
out.flush();
out.close();
}

public boolean search(String sentence, Vector<Integer> bs, Vector<Integer> es){
bs.clear();
es.clear();
boolean empty = true;
for(int offset = 0; offset < sentence.length(); offset ++){
int preBase = 0;
int preInd = 0;
int ind = 0;
for(int i = offset; i < sentence.length(); i ++){
ind = preBase + sentence.charAt(i);
if(ind < 0 || ind >= datSize || dat.get(ind).check != preInd)break;
preInd = ind;
preBase = dat.get(ind).base;
ind = preBase;
if(!(ind < 0 || ind >= datSize || dat.get(ind).check != preInd)){
bs.add(offset);
es.add(i + 1);
if(empty){
empty = false;
}
}
}
}
return !empty;
}

public int match(String word){
int ind = 0;
int base = 0;
for(int i = 0; i < word.length(); i ++){
ind = dat.get(ind).base + word.charAt(i);
if((ind >= datSize) || (dat.get(ind).check != base)) return -1;
base = ind;
}
ind = dat.get(base).base;
if((ind < datSize) && (dat.get(ind).check == base)){
return ind;
}
return -1;
}

public void update(String word, int value){
int base = match(word);
if(base >= 0){
dat.get(base).base = value;
}
}

public int getInfo(String prefix){
int ind = 0;
int base = 0;
for(int i = 0; i < prefix.length(); i ++){
ind = dat.get(ind).base + prefix.charAt(i);
if((ind >= datSize) || dat.get(ind).check != base) return i;
base = ind;
}
return -base;
}

public int getDatSize(){
return datSize;
}

public Vector<Entry> getDat(){
return dat;
}
}
Loading