use of java.text.NumberFormat in project j2objc by google.
the class NumberFormatTest method test_customCurrencySymbol.
// Test to ensure explicitly setting a currency symbol will overwrite the defaults.
public void test_customCurrencySymbol() {
NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
DecimalFormatSymbols dfs = ((DecimalFormat) nf).getDecimalFormatSymbols();
dfs.setCurrencySymbol("SPECIAL");
((DecimalFormat) nf).setDecimalFormatSymbols(dfs);
assertEquals("SPECIAL3.14", nf.format(3.14));
// Setting the currency again should reset the symbols.
nf.setCurrency(Currency.getInstance("USD"));
assertEquals("$3.14", nf.format(3.14));
// Setting it back again should work.
dfs.setCurrencySymbol("NEW");
((DecimalFormat) nf).setDecimalFormatSymbols(dfs);
assertEquals("NEW3.14", nf.format(3.14));
}
use of java.text.NumberFormat in project j2objc by google.
the class DecimalFormatTest method testBigDecimalBug1897917.
// Regression test for http://b/1897917: BigDecimal does not take into account multiplier.
public void testBigDecimalBug1897917() {
// For example. the BigDecimal 0.17 formatted in PercentInstance is 0% instead of 17%:
NumberFormat pf = NumberFormat.getPercentInstance();
assertEquals("17%", pf.format(BigDecimal.valueOf(0.17)));
// Test long decimal formatted in PercentInstance with various fractions.
String longDec = "11.2345678901234567890123456789012345678901234567890";
BigDecimal bd = new BigDecimal(longDec);
assertBigDecimalWithFraction(bd, "1,123.46%", 2);
assertBigDecimalWithFraction(bd, "1,123.45678901%", 8);
assertBigDecimalWithFraction(bd, "1,123.4567890123%", 10);
assertBigDecimalWithFraction(bd, "1,123.45678901234567890123%", 20);
assertBigDecimalWithFraction(bd, "1,123.456789012345678901234567890123%", 30);
// Test trailing zeros.
assertDecFmtWithMultiplierAndFraction("3333.33333333", 3, 4, "10,000");
assertDecFmtWithMultiplierAndFraction("3333.33333333", -3, 4, "-10,000");
assertDecFmtWithMultiplierAndFraction("0.00333333", 3, 4, "0.01");
assertDecFmtWithMultiplierAndFractionByLocale("3330000000000000000000000000000000", 3, 4, Locale.US, "9,990,000,000,000,000,000,000,000,000,000,000");
Locale en_IN = new Locale("en", "IN");
assertDecFmtWithMultiplierAndFractionByLocale("3330000000000000000000000000000000", 3, 4, en_IN, "9,99,00,00,00,00,00,00,00,00,00,00,00,00,00,00,000");
}
use of java.text.NumberFormat in project CoreNLP by stanfordnlp.
the class TaggingEval method display.
@Override
public void display(boolean verbose, PrintWriter pw) {
super.display(verbose, pw);
if (doCatLevelEval) {
final NumberFormat nf = new DecimalFormat("0.00");
final Set<String> cats = Generics.newHashSet();
final Random rand = new Random();
cats.addAll(precisions.keySet());
cats.addAll(recalls.keySet());
Map<Double, String> f1Map = new TreeMap<>();
for (String cat : cats) {
double pnum2 = pnums2.getCount(cat);
double rnum2 = rnums2.getCount(cat);
double prec = precisions2.getCount(cat) / pnum2;
double rec = recalls2.getCount(cat) / rnum2;
double f1 = 2.0 / (1.0 / prec + 1.0 / rec);
if (new Double(f1).equals(Double.NaN))
f1 = -1.0;
if (f1Map.containsKey(f1))
f1Map.put(f1 + (rand.nextDouble() / 1000.0), cat);
else
f1Map.put(f1, cat);
}
pw.println("============================================================");
pw.println("Tagging Performance by Category -- final statistics");
pw.println("============================================================");
for (String cat : f1Map.values()) {
double pnum2 = pnums2.getCount(cat);
double rnum2 = rnums2.getCount(cat);
double prec = precisions2.getCount(cat) / pnum2;
prec *= 100.0;
double rec = recalls2.getCount(cat) / rnum2;
rec *= 100.0;
double f1 = 2.0 / (1.0 / prec + 1.0 / rec);
double oovRate = (lex == null) ? -1.0 : percentOOV.getCount(cat) / percentOOV2.getCount(cat);
pw.println(cat + "\tLP: " + ((pnum2 == 0.0) ? " N/A" : nf.format(prec)) + "\tguessed: " + (int) pnum2 + "\tLR: " + ((rnum2 == 0.0) ? " N/A" : nf.format(rec)) + "\tgold: " + (int) rnum2 + "\tF1: " + ((pnum2 == 0.0 || rnum2 == 0.0) ? " N/A" : nf.format(f1)) + "\tOOV: " + ((lex == null) ? " N/A" : nf.format(oovRate)));
}
pw.println("============================================================");
}
}
use of java.text.NumberFormat in project CoreNLP by stanfordnlp.
the class ParentAnnotationStats method printStats.
public void printStats() {
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
// System.out.println("Node rules");
// System.out.println(nodeRules);
// System.out.println("Parent rules");
// System.out.println(pRules);
// System.out.println("Grandparent rules");
// System.out.println(gPRules);
// Store java code for selSplit
StringBuffer[] javaSB = new StringBuffer[CUTOFFS.length];
for (int i = 0; i < CUTOFFS.length; i++) {
javaSB[i] = new StringBuffer(" private static String[] splitters" + (i + 1) + " = new String[] {");
}
ClassicCounter<List<String>> allScores = new ClassicCounter<>();
// do value of parent
for (String node : nodeRules.keySet()) {
ArrayList<Pair<List<String>, Double>> answers = Generics.newArrayList();
ClassicCounter<List<String>> cntr = nodeRules.get(node);
double support = (cntr.totalCount());
System.out.println("Node " + node + " support is " + support);
for (List<String> key : pRules.keySet()) {
if (key.get(0).equals(node)) {
// only do it if they match
ClassicCounter<List<String>> cntr2 = pRules.get(key);
double support2 = (cntr2.totalCount());
double kl = Counters.klDivergence(cntr2, cntr);
System.out.println("KL(" + key + "||" + node + ") = " + nf.format(kl) + "\t" + "support(" + key + ") = " + support2);
double score = kl * support2;
answers.add(new Pair<>(key, new Double(score)));
allScores.setCount(key, score);
}
}
System.out.println("----");
System.out.println("Sorted descending support * KL");
Collections.sort(answers, (o1, o2) -> o2.second().compareTo(o1.second()));
for (Pair<List<String>, Double> answer : answers) {
Pair p = (Pair) answer;
double psd = ((Double) p.second()).doubleValue();
System.out.println(p.first() + ": " + nf.format(psd));
if (psd >= CUTOFFS[0]) {
List lst = (List) p.first();
String nd = (String) lst.get(0);
String par = (String) lst.get(1);
for (int j = 0; j < CUTOFFS.length; j++) {
if (psd >= CUTOFFS[j]) {
javaSB[j].append("\"").append(nd).append("^");
javaSB[j].append(par).append("\", ");
}
}
}
}
System.out.println();
}
// do value of grandparent
for (List<String> node : pRules.keySet()) {
ArrayList<Pair<List<String>, Double>> answers = Generics.newArrayList();
ClassicCounter<List<String>> cntr = pRules.get(node);
double support = (cntr.totalCount());
if (support < SUPPCUTOFF) {
continue;
}
System.out.println("Node " + node + " support is " + support);
for (List<String> key : gPRules.keySet()) {
if (key.get(0).equals(node.get(0)) && key.get(1).equals(node.get(1))) {
// only do it if they match
ClassicCounter<List<String>> cntr2 = gPRules.get(key);
double support2 = (cntr2.totalCount());
double kl = Counters.klDivergence(cntr2, cntr);
System.out.println("KL(" + key + "||" + node + ") = " + nf.format(kl) + "\t" + "support(" + key + ") = " + support2);
double score = kl * support2;
answers.add(Pair.makePair(key, new Double(score)));
allScores.setCount(key, score);
}
}
System.out.println("----");
System.out.println("Sorted descending support * KL");
Collections.sort(answers, (o1, o2) -> o2.second().compareTo(o1.second()));
for (Pair<List<String>, Double> answer : answers) {
Pair p = (Pair) answer;
double psd = ((Double) p.second()).doubleValue();
System.out.println(p.first() + ": " + nf.format(psd));
if (psd >= CUTOFFS[0]) {
List lst = (List) p.first();
String nd = (String) lst.get(0);
String par = (String) lst.get(1);
String gpar = (String) lst.get(2);
for (int j = 0; j < CUTOFFS.length; j++) {
if (psd >= CUTOFFS[j]) {
javaSB[j].append("\"").append(nd).append("^");
javaSB[j].append(par).append("~");
javaSB[j].append(gpar).append("\", ");
}
}
}
}
System.out.println();
}
System.out.println();
System.out.println("All scores:");
edu.stanford.nlp.util.PriorityQueue<List<String>> pq = Counters.toPriorityQueue(allScores);
while (!pq.isEmpty()) {
List<String> key = pq.getFirst();
double score = pq.getPriority(key);
pq.removeFirst();
System.out.println(key + "\t" + score);
}
System.out.println(" // Automatically generated by ParentAnnotationStats -- preferably don't edit");
for (int i = 0; i < CUTOFFS.length; i++) {
int len = javaSB[i].length();
javaSB[i].replace(len - 2, len, "};");
System.out.println(javaSB[i]);
}
System.out.print(" public static HashSet splitters = new HashSet(Arrays.asList(");
for (int i = CUTOFFS.length; i > 0; i--) {
if (i == 1) {
System.out.print("splitters1");
} else {
System.out.print("selectiveSplit" + i + " ? splitters" + i + " : (");
}
}
// need to print extra one to close other things open
for (int i = CUTOFFS.length; i >= 0; i--) {
System.out.print(")");
}
System.out.println(";");
}
use of java.text.NumberFormat in project CoreNLP by stanfordnlp.
the class ParseFiles method parseFiles.
public void parseFiles(String[] args, int argIndex, boolean tokenized, TokenizerFactory<? extends HasWord> tokenizerFactory, String elementDelimiter, String sentenceDelimiter, Function<List<HasWord>, List<HasWord>> escaper, String tagDelimiter) {
final DocType docType = (elementDelimiter == null) ? DocType.Plain : DocType.XML;
if (op.testOptions.verbose) {
if (tokenizerFactory != null)
pwErr.println("parseFiles: Tokenizer factory is: " + tokenizerFactory);
}
final Timing timer = new Timing();
//Loop over the files
for (int i = argIndex; i < args.length; i++) {
final String filename = args[i];
final DocumentPreprocessor documentPreprocessor;
if (filename.equals("-")) {
try {
documentPreprocessor = new DocumentPreprocessor(IOUtils.readerFromStdin(op.tlpParams.getInputEncoding()), docType);
} catch (IOException e) {
throw new RuntimeIOException(e);
}
} else {
documentPreprocessor = new DocumentPreprocessor(filename, docType, op.tlpParams.getInputEncoding());
}
//Unused values are null per the main() method invocation below
//null is the default for these properties
documentPreprocessor.setSentenceFinalPuncWords(tlp.sentenceFinalPunctuationWords());
documentPreprocessor.setEscaper(escaper);
documentPreprocessor.setSentenceDelimiter(sentenceDelimiter);
documentPreprocessor.setTagDelimiter(tagDelimiter);
documentPreprocessor.setElementDelimiter(elementDelimiter);
if (tokenizerFactory == null)
documentPreprocessor.setTokenizerFactory((tokenized) ? null : tlp.getTokenizerFactory());
else
documentPreprocessor.setTokenizerFactory(tokenizerFactory);
//Setup the output
PrintWriter pwo = pwOut;
if (op.testOptions.writeOutputFiles) {
String normalizedName = filename;
try {
// this will exception if not a URL
new URL(normalizedName);
normalizedName = normalizedName.replaceAll("/", "_");
} catch (MalformedURLException e) {
//It isn't a URL, so silently ignore
}
String ext = (op.testOptions.outputFilesExtension == null) ? "stp" : op.testOptions.outputFilesExtension;
String fname = normalizedName + '.' + ext;
if (op.testOptions.outputFilesDirectory != null && !op.testOptions.outputFilesDirectory.isEmpty()) {
String fseparator = System.getProperty("file.separator");
if (fseparator == null || fseparator.isEmpty()) {
fseparator = "/";
}
File fnameFile = new File(fname);
fname = op.testOptions.outputFilesDirectory + fseparator + fnameFile.getName();
}
try {
pwo = op.tlpParams.pw(new FileOutputStream(fname));
} catch (IOException ioe) {
throw new RuntimeIOException(ioe);
}
}
treePrint.printHeader(pwo, op.tlpParams.getOutputEncoding());
pwErr.println("Parsing file: " + filename);
int num = 0;
int numProcessed = 0;
if (op.testOptions.testingThreads != 1) {
MulticoreWrapper<List<? extends HasWord>, ParserQuery> wrapper = new MulticoreWrapper<>(op.testOptions.testingThreads, new ParsingThreadsafeProcessor(pqFactory, pwErr));
for (List<HasWord> sentence : documentPreprocessor) {
num++;
numSents++;
int len = sentence.size();
numWords += len;
pwErr.println("Parsing [sent. " + num + " len. " + len + "]: " + SentenceUtils.listToString(sentence, true));
wrapper.put(sentence);
while (wrapper.peek()) {
ParserQuery pq = wrapper.poll();
processResults(pq, numProcessed++, pwo);
}
}
wrapper.join();
while (wrapper.peek()) {
ParserQuery pq = wrapper.poll();
processResults(pq, numProcessed++, pwo);
}
} else {
ParserQuery pq = pqFactory.parserQuery();
for (List<HasWord> sentence : documentPreprocessor) {
num++;
numSents++;
int len = sentence.size();
numWords += len;
pwErr.println("Parsing [sent. " + num + " len. " + len + "]: " + SentenceUtils.listToString(sentence, true));
pq.parseAndReport(sentence, pwErr);
processResults(pq, numProcessed++, pwo);
}
}
treePrint.printFooter(pwo);
if (op.testOptions.writeOutputFiles)
pwo.close();
pwErr.println("Parsed file: " + filename + " [" + num + " sentences].");
}
long millis = timer.stop();
if (summary) {
if (pcfgLL != null)
pcfgLL.display(false, pwErr);
if (depLL != null)
depLL.display(false, pwErr);
if (factLL != null)
factLL.display(false, pwErr);
}
if (saidMemMessage) {
ParserUtils.printOutOfMemory(pwErr);
}
double wordspersec = numWords / (((double) millis) / 1000);
double sentspersec = numSents / (((double) millis) / 1000);
// easier way!
NumberFormat nf = new DecimalFormat("0.00");
pwErr.println("Parsed " + numWords + " words in " + numSents + " sentences (" + nf.format(wordspersec) + " wds/sec; " + nf.format(sentspersec) + " sents/sec).");
if (numFallback > 0) {
pwErr.println(" " + numFallback + " sentences were parsed by fallback to PCFG.");
}
if (numUnparsable > 0 || numNoMemory > 0 || numSkipped > 0) {
pwErr.println(" " + (numUnparsable + numNoMemory + numSkipped) + " sentences were not parsed:");
if (numUnparsable > 0) {
pwErr.println(" " + numUnparsable + " were not parsable with non-zero probability.");
}
if (numNoMemory > 0) {
pwErr.println(" " + numNoMemory + " were skipped because of insufficient memory.");
}
if (numSkipped > 0) {
pwErr.println(" " + numSkipped + " were skipped as length 0 or greater than " + op.testOptions.maxLength);
}
}
}
Aggregations