use of java.text.NumberFormat in project HanLP by hankcs.
the class Evaluator method toString.
@Override
public String toString() {
NumberFormat percentFormat = NumberFormat.getPercentInstance();
percentFormat.setMinimumFractionDigits(2);
StringBuilder sb = new StringBuilder();
sb.append("UA: ");
sb.append(percentFormat.format(getUA()));
sb.append('\t');
sb.append("LA: ");
sb.append(percentFormat.format(getLA()));
sb.append('\t');
sb.append("DA: ");
sb.append(percentFormat.format(getDA()));
sb.append('\t');
sb.append("sentences: ");
sb.append(sentenceCount);
sb.append('\t');
sb.append("speed: ");
sb.append(sentenceCount / (float) (System.currentTimeMillis() - start) * 1000);
sb.append(" sent/s");
return sb.toString();
}
use of java.text.NumberFormat in project CoreNLP by stanfordnlp.
the class TestSentence method printTop.
// This method should be called after a sentence has been tagged.
// For every word token, this method prints the 3 most probable tags
// to the file pfu except for
void printTop(PrintFile pfu) {
NumberFormat nf = new DecimalFormat("0.0000");
int numTags = maxentTagger.numTags();
double[][][] probabilities = new double[size][kBestSize][numTags];
calculateProbs(probabilities);
for (int current = 0; current < correctTags.length; current++) {
pfu.print(sent.get(current));
double[] probs = new double[3];
String[] tag3 = new String[3];
getTop3(probabilities, current, probs, tag3);
for (int i = 0; i < 3; i++) {
if (probs[i] > Double.NEGATIVE_INFINITY) {
pfu.print('\t');
pfu.print(tag3[i]);
pfu.print(' ');
pfu.print(nf.format(Math.exp(probs[i])));
}
}
int rank;
String correctTag = toNice(this.correctTags[current]);
for (rank = 0; rank < 3; rank++) {
if (correctTag.equals(tag3[rank])) {
break;
}
//if
}
pfu.print('\t');
switch(rank) {
case 0:
pfu.print("Correct");
break;
case 1:
pfu.print("2nd");
break;
case 2:
pfu.print("3rd");
break;
default:
pfu.print("Not top 3");
}
pfu.println();
}
// for
}
use of java.text.NumberFormat in project CoreNLP by stanfordnlp.
the class LambdaSolveTagger method checkCorrectness.
/** Works out whether the model expectations match the empirical
* expectations.
* @return Whether the model is correct
*/
@Override
public boolean checkCorrectness() {
log.info("Checking model correctness; x size " + p.data.xSize + ' ' + ", ysize " + p.data.ySize);
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(4);
boolean flag = true;
for (int f = 0; f < lambda.length; f++) {
if (Math.abs(lambda[f]) > 100) {
log.info(" Lambda too big " + lambda[f]);
log.info(" empirical " + ftildeArr[f] + " expected " + fExpected(p.functions.get(f)));
}
}
for (int i = 0; i < ftildeArr.length; i++) {
double exp = Math.abs(ftildeArr[i] - fExpected(p.functions.get(i)));
if (exp > 0.001) {
flag = false;
log.info("Constraint " + i + " not satisfied emp " + nf.format(ftildeArr[i]) + " exp " + nf.format(fExpected(p.functions.get(i))) + " diff " + nf.format(exp) + " lambda " + nf.format(lambda[i]));
}
}
for (int x = 0; x < p.data.xSize; x++) {
double s = 0.0;
for (int y = 0; y < p.data.ySize; y++) {
s = s + probConds[x][y];
}
if (Math.abs(s - 1) > 0.0001) {
for (int y = 0; y < p.data.ySize; y++) {
log.info(y + " : " + probConds[x][y]);
}
log.info("probabilities do not sum to one " + x + ' ' + (float) s);
}
}
return flag;
}
use of java.text.NumberFormat in project gephi by gephi.
the class PartitionColorTransformerPanel method setup.
public void setup(PartitionFunction function) {
this.function = function;
NumberFormat formatter = NumberFormat.getPercentInstance();
formatter.setMaximumFractionDigits(2);
Partition partition = function.getPartition();
values = partition.getSortedValues();
List<Object> nullColors = new ArrayList<>();
Color defaultColor = Color.LIGHT_GRAY;
for (Object val : values) {
Color c = partition.getColor(val);
if (c == null) {
nullColors.add(val);
partition.setColor(val, defaultColor);
}
}
int valuesWithColors = values.size() - nullColors.size();
if (!nullColors.isEmpty() && valuesWithColors < 8) {
Color[] cls = PaletteGenerator.generatePalette(Math.min(8, values.size()), 5, new Random(42l));
int i = 0;
for (Object val : nullColors) {
int index = valuesWithColors + i++;
if (index < cls.length) {
partition.setColor(val, cls[index]);
}
}
}
//Model
String[] columnNames = new String[] { "Color", "Partition", "Percentage" };
DefaultTableModel model = new DefaultTableModel(columnNames, values.size()) {
@Override
public boolean isCellEditable(int row, int column) {
return column == 0;
}
};
table.setModel(model);
String countMsg = NbBundle.getMessage(PartitionColorTransformerPanel.class, "PartitionColorTransformerPanel.tooltip.elementsCount");
TableColumn partCol = table.getColumnModel().getColumn(1);
partCol.setCellRenderer(new TextRenderer(null));
TableColumn percCol = table.getColumnModel().getColumn(2);
percCol.setCellRenderer(new TextRenderer(countMsg));
percCol.setPreferredWidth(60);
percCol.setMaxWidth(60);
TableColumn colorCol = table.getColumnModel().getColumn(0);
colorCol.setCellEditor(new ColorChooserEditor());
colorCol.setCellRenderer(new ColorChooserRenderer());
colorCol.setPreferredWidth(16);
colorCol.setMaxWidth(16);
int j = 0;
for (Object value : values) {
String displayName = value == null ? "null" : value.getClass().isArray() ? AttributeUtils.printArray(value) : value.toString();
int count = function.getPartition().count(value);
float percentage = function.getPartition().percentage(value) / 100f;
model.setValueAt(value, j, 0);
model.setValueAt(displayName, j, 1);
String percCount = count + "_(" + formatter.format(percentage) + ")";
model.setValueAt(percCount, j, 2);
j++;
}
}
use of java.text.NumberFormat in project gephi by gephi.
the class Modularity method getReport.
@Override
public String getReport() {
//Distribution series
Map<Integer, Integer> sizeDist = new HashMap<>();
for (Node n : structure.graph.getNodes()) {
Integer v = (Integer) n.getAttribute(MODULARITY_CLASS);
if (!sizeDist.containsKey(v)) {
sizeDist.put(v, 0);
}
sizeDist.put(v, sizeDist.get(v) + 1);
}
XYSeries dSeries = ChartUtils.createXYSeries(sizeDist, "Size Distribution");
XYSeriesCollection dataset1 = new XYSeriesCollection();
dataset1.addSeries(dSeries);
JFreeChart chart = ChartFactory.createXYLineChart("Size Distribution", "Modularity Class", "Size (number of nodes)", dataset1, PlotOrientation.VERTICAL, true, false, false);
chart.removeLegend();
ChartUtils.decorateChart(chart);
ChartUtils.scaleChart(chart, dSeries, false);
String imageFile = ChartUtils.renderChart(chart, "communities-size-distribution.png");
NumberFormat f = new DecimalFormat("#0.000");
String report = "<HTML> <BODY> <h1>Modularity Report </h1> " + "<hr>" + "<h2> Parameters: </h2>" + "Randomize: " + (isRandomized ? "On" : "Off") + "<br>" + "Use edge weights: " + (useWeight ? "On" : "Off") + "<br>" + "Resolution: " + (resolution) + "<br>" + "<br> <h2> Results: </h2>" + "Modularity: " + f.format(modularity) + "<br>" + "Modularity with resolution: " + f.format(modularityResolution) + "<br>" + "Number of Communities: " + structure.communities.size() + "<br /><br />" + imageFile + "<br /><br />" + "<h2> Algorithm: </h2>" + "Vincent D Blondel, Jean-Loup Guillaume, Renaud Lambiotte, Etienne Lefebvre, <i>Fast unfolding of communities in large networks</i>, in Journal of Statistical Mechanics: Theory and Experiment 2008 (10), P1000<br />" + "<br /><br />" + "<h2> Resolution: </h2>" + "R. Lambiotte, J.-C. Delvenne, M. Barahona <i>Laplacian Dynamics and Multiscale Modular Structure in Networks 2009<br />" + "</BODY> </HTML>";
return report;
}
Aggregations