use of java.io.BufferedReader in project blueprints by tinkerpop.
the class RestHelper method convertStreamToString.
private static String convertStreamToString(final InputStream is) throws Exception {
final BufferedReader reader = new BufferedReader(new InputStreamReader(is));
final StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
return sb.toString();
}
use of java.io.BufferedReader in project watson by totemo.
the class BlockEditSet method load.
// --------------------------------------------------------------------------
/**
* Load additional entries from the specified file.
*
* @param file the file to load.
* @return the number of edits loaded.
*/
public synchronized int load(File file) throws Exception {
BufferedReader reader = new BufferedReader(new FileReader(file));
try {
Pattern editPattern = Pattern.compile("(\\d{4})-(\\d{2})-(\\d{2})\\|(\\d{2}):(\\d{2}):(\\d{2})\\|(\\w+)\\|([cd])\\|(\\d+)\\|(\\d+)\\|(-?\\d+)\\|(\\d+)\\|(-?\\d+)");
Pattern annoPattern = Pattern.compile("#(-?\\d+)\\|(\\d+)\\|(-?\\d+)\\|(.*)");
Calendar time = Calendar.getInstance();
String line;
int edits = 0;
BlockEdit blockEdit = null;
while ((line = reader.readLine()) != null) {
Matcher edit = editPattern.matcher(line);
if (edit.matches()) {
int year = Integer.parseInt(edit.group(1));
int month = Integer.parseInt(edit.group(2)) - 1;
int day = Integer.parseInt(edit.group(3));
int hour = Integer.parseInt(edit.group(4));
int minute = Integer.parseInt(edit.group(5));
int second = Integer.parseInt(edit.group(6));
time.set(year, month, day, hour, minute, second);
String player = edit.group(7);
boolean created = edit.group(8).equals("c");
int id = Integer.parseInt(edit.group(9));
int data = Integer.parseInt(edit.group(10));
int x = Integer.parseInt(edit.group(11));
int y = Integer.parseInt(edit.group(12));
int z = Integer.parseInt(edit.group(13));
BlockType type = BlockTypeRegistry.instance.getBlockTypeByIdData(id, data);
blockEdit = new BlockEdit(time.getTimeInMillis(), player, created, x, y, z, type);
addBlockEdit(blockEdit);
++edits;
} else // if
{
// Is the line an annotation?
Matcher anno = annoPattern.matcher(line);
if (anno.matches()) {
int x = Integer.parseInt(anno.group(1));
int y = Integer.parseInt(anno.group(2));
int z = Integer.parseInt(anno.group(3));
String text = anno.group(4);
_annotations.add(new Annotation(x, y, z, text));
}
}
}
// If there was at least one BlockEdit, select it.
if (blockEdit != null) {
Controller.instance.selectBlockEdit(blockEdit);
}
return edits;
} finally {
reader.close();
}
}
use of java.io.BufferedReader in project CoreNLP by stanfordnlp.
the class CtbDict method readCtbDict.
private void readCtbDict(String filename) throws IOException {
BufferedReader ctbDetectorReader = new BufferedReader(new InputStreamReader(new FileInputStream(filename), "GB18030"));
String ctbDetectorLine;
ctb_pre_dict = Generics.newHashMap();
ctb_suf_dict = Generics.newHashMap();
while ((ctbDetectorLine = ctbDetectorReader.readLine()) != null) {
String[] fields = ctbDetectorLine.split(" ");
String tag = fields[0];
Set<String> pres = ctb_pre_dict.get(tag);
Set<String> sufs = ctb_suf_dict.get(tag);
if (pres == null) {
pres = Generics.newHashSet();
ctb_pre_dict.put(tag, pres);
}
pres.add(fields[1]);
if (sufs == null) {
sufs = Generics.newHashSet();
ctb_suf_dict.put(tag, sufs);
}
sufs.add(fields[2]);
}
}
use of java.io.BufferedReader in project trie4j by takawitter.
the class GenHtml method main.
public static void main(String[] args) throws Exception {
String templ = null;
InputStream temps = GenHtml.class.getResourceAsStream("template.txt");
try {
templ = StreamUtil.readAsString(temps, "UTF-8");
} finally {
temps.close();
}
InputStream res = GenHtml.class.getResourceAsStream("result.txt");
try {
BufferedReader r = new BufferedReader(new InputStreamReader(res, "UTF-8"));
String line = null;
while ((line = r.readLine()) != null) {
String[] vals = line.split("[ ]+");
String name = vals[0].substring(0, vals[0].length() - 1).replaceAll("\\(", "\\\\(").replaceAll("\\)", "\\\\)").trim();
templ = templ.replaceAll("\\$\\{" + name + "\\.build\\}", String.format("%,d", Integer.valueOf(vals[1].replace(',', ' ').trim()))).replaceAll("\\$\\{" + name + "\\.contains\\}", String.format("%,d", Integer.valueOf(vals[2].replace(',', ' ').trim()))).replaceAll("\\$\\{" + name + "\\.mem\\}", String.format("%.1f", Integer.valueOf(vals[3].replace(',', ' ').trim()) / 1000000.0));
}
System.out.println(templ);
} finally {
res.close();
}
}
use of java.io.BufferedReader in project trie4j by takawitter.
the class TestWikipedia method investigate.
private static void investigate(PatriciaTrie trie, int charCount) throws Exception {
System.out.println("-- count elements.");
final AtomicInteger count = new AtomicInteger();
trie.visit(new TrieVisitor() {
public void accept(Node node, int nest) {
if (node.isTerminate())
count.incrementAndGet();
}
});
System.out.println(count.intValue() + " elements.");
//*
System.out.println("-- list elements.");
final AtomicInteger n = new AtomicInteger();
final AtomicInteger l = new AtomicInteger();
final AtomicInteger ln = new AtomicInteger();
final AtomicInteger chars = new AtomicInteger();
trie.visit(new TrieVisitor() {
public void accept(Node node, int nest) {
if (node.isTerminate()) {
l.incrementAndGet();
} else {
n.incrementAndGet();
}
chars.addAndGet(node.getLetters().length);
}
});
System.out.println("node: " + n.intValue());
System.out.println("leaf: " + l.intValue());
System.out.println("label node: " + ln.intValue());
System.out.println("total char count: " + charCount);
System.out.println("total char count in trie: " + chars.intValue());
System.out.println("verifying trie...");
BufferedReader r = new BufferedReader(new InputStreamReader(// new GZIPInputStream(new FileInputStream("jawiki-20120220-all-titles-in-ns0.gz"))
new GZIPInputStream(new FileInputStream("enwiki-20120403-all-titles-in-ns0.gz")), CharsetUtil.newUTF8Decoder()));
long lap = System.currentTimeMillis();
int c = 0;
int sum = 0;
String word = null;
while ((word = r.readLine()) != null) {
if (c == maxCount)
break;
long d = System.currentTimeMillis();
boolean found = trie.contains(word);
sum += System.currentTimeMillis() - d;
if (!found) {
System.out.println("trie not contains [" + word + "]");
break;
}
if (c % 100000 == 0) {
System.out.println(c + " elements done.");
}
c++;
}
System.out.println("done in " + (System.currentTimeMillis() - lap) + " millis.");
System.out.println("contains time: " + sum + " millis.");
System.out.println(trie.getRoot().getChildren().length + "children in root");
final PatriciaTrie t = trie;
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(100000);
t.contains("hello");
} catch (InterruptedException e) {
}
}
}).start();
//*/
}
Aggregations