forked from lintool/Ivory
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'ferhanture-master' into clpwsim-lsh
- Loading branch information
Showing
3 changed files
with
133 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
src/java/main/ivory/core/tokenize/BigramChineseTokenizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package ivory.core.tokenize; | ||
|
||
import ivory.core.Constants; | ||
import java.io.BufferedReader; | ||
import java.io.BufferedWriter; | ||
import java.io.FileInputStream; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.OutputStreamWriter; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Properties; | ||
|
||
import org.apache.hadoop.conf.Configuration; | ||
import org.apache.hadoop.fs.FSDataInputStream; | ||
import org.apache.hadoop.fs.FileSystem; | ||
import org.apache.hadoop.fs.Path; | ||
import org.apache.log4j.Level; | ||
import org.apache.log4j.Logger; | ||
import org.mortbay.log.Log; | ||
|
||
import edu.stanford.nlp.ie.crf.CRFClassifier; | ||
import edu.stanford.nlp.sequences.DocumentReaderAndWriter; | ||
|
||
public class BigramChineseTokenizer extends Tokenizer { | ||
private static final Logger LOG = Logger.getLogger(BigramChineseTokenizer.class); | ||
static{ | ||
LOG.setLevel(Level.INFO); | ||
} | ||
public BigramChineseTokenizer(){ | ||
super(); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public void configure(Configuration conf) { } | ||
|
||
@Override | ||
public void configure(Configuration conf, FileSystem fs) { } | ||
|
||
@Override | ||
public String[] processContent(String text) { | ||
int numTokens = 0; | ||
String[] chunks = text.split("\\s+"); | ||
|
||
List<String> tokens = new ArrayList<String>(); | ||
for (String chunk : chunks){ | ||
chunk = chunk.toLowerCase(); | ||
// LOG.info("chunk="+chunk.length()); | ||
|
||
char prev = 0, cur; | ||
for (int i = 0; i < chunk.length(); i++) { | ||
cur = chunk.charAt(i); | ||
if (i > 0) { | ||
String bigram = ""; | ||
bigram += prev; | ||
bigram += cur; | ||
tokens.add(bigram); | ||
// LOG.info("bigram="+bigram); | ||
numTokens++; | ||
} | ||
prev = cur; | ||
} | ||
} | ||
|
||
String[] tokensArr = new String[numTokens]; | ||
return tokens.toArray(tokensArr); | ||
} | ||
|
||
public static void main(String[] args) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException{ | ||
if(args.length < 2){ | ||
System.err.println("usage: [input] [output-file]"); | ||
System.exit(-1); | ||
} | ||
Tokenizer tokenizer = new BigramChineseTokenizer(); | ||
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(args[1]), "UTF8")); | ||
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(args[0]), "UTF8")); | ||
|
||
String line = null; | ||
while((line = in.readLine()) != null){ | ||
String[] tokens = tokenizer.processContent(line); | ||
String s = ""; | ||
for (String token : tokens) { | ||
s += token+" "; | ||
} | ||
out.write(s+"\n"); | ||
} | ||
out.close(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters