Skip to content
This repository was archived by the owner on Dec 13, 2021. It is now read-only.

Commit 1586853

Browse files
committed
Replaced System.err.print* with logger api
1 parent d4ac451 commit 1586853

29 files changed

+223
-192
lines changed

src/main/java/org/apache/joshua/adagrad/AdaGradCore.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,25 @@
5050
import org.apache.joshua.util.StreamGobbler;
5151

5252
import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap;
53+
import org.slf4j.Logger;
54+
import org.slf4j.LoggerFactory;
5355

5456
/**
5557
* This code was originally written by Yuan Cao, who copied the MERT code to produce this file.
5658
*/
5759

5860
public class AdaGradCore {
59-
private final JoshuaConfiguration joshuaConfiguration;
60-
private TreeSet<Integer>[] indicesOfInterest_all;
61-
62-
private final static DecimalFormat f4 = new DecimalFormat("###0.0000");
63-
private final Runtime myRuntime = Runtime.getRuntime();
6461

62+
private static final Logger LOG = LoggerFactory.getLogger(AdaGradCore.class);
6563
private final static double NegInf = (-1.0 / 0.0);
6664
private final static double PosInf = (+1.0 / 0.0);
6765
private final static double epsilon = 1.0 / 1000000;
66+
private final static DecimalFormat f4 = new DecimalFormat("###0.0000");
67+
68+
private final JoshuaConfiguration joshuaConfiguration;
69+
private final Runtime myRuntime = Runtime.getRuntime();
70+
71+
private TreeSet<Integer>[] indicesOfInterest_all;
6872

6973
private int progress;
7074

@@ -1928,12 +1932,8 @@ private boolean copyFile(String origFileName, String newFileName) {
19281932
* inFile.close(); outFile.close();
19291933
*/
19301934
return true;
1931-
} catch (FileNotFoundException e) {
1932-
System.err.println("FileNotFoundException in AdaGradCore.copyFile(String,String): "
1933-
+ e.getMessage());
1934-
return false;
19351935
} catch (IOException e) {
1936-
System.err.println("IOException in AdaGradCore.copyFile(String,String): " + e.getMessage());
1936+
LOG.error(e.getMessage(), e);
19371937
return false;
19381938
}
19391939
}

src/main/java/org/apache/joshua/decoder/Decoder.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,12 @@ private void handleMetadata(MetaDataException meta) throws IOException {
239239
// Change a decoder weight
240240
String[] tokens = meta.tokens();
241241
if (tokens.length != 3) {
242-
System.err.println("* Error: weight change requires three tokens");
242+
LOG.error("weight change requires three tokens");
243243
} else {
244244
float old_weight = Decoder.weights.getWeight(tokens[1]);
245245
Decoder.weights.set(tokens[1], Float.parseFloat(tokens[2]));
246-
System.err.println(String.format("@set_weight: %s %.3f -> %.3f",
247-
tokens[1], old_weight,
248-
Decoder.weights.getWeight(tokens[1])));
246+
LOG.error("@set_weight: {} {} -> {}", tokens[1], old_weight,
247+
Decoder.weights.getWeight(tokens[1]));
249248
}
250249

251250
// TODO: return a JSON object with this weight or all weights
@@ -256,15 +255,15 @@ private void handleMetadata(MetaDataException meta) throws IOException {
256255

257256
String[] tokens = meta.tokens();
258257

259-
System.err.println(String.format("%s = %f", tokens[1], Decoder.weights.getWeight(tokens[1])));
258+
LOG.error("{} = {}", tokens[1], Decoder.weights.getWeight(tokens[1]));
260259

261260
out.write("".getBytes());
262261

263262
} else if (meta.type().equals("add_rule")) {
264263
String tokens[] = meta.tokens(" \\|\\|\\| ");
265264

266265
if (tokens.length != 2) {
267-
System.err.println("* INVALID RULE '" + meta.tokenString() + "'");;
266+
LOG.error("* INVALID RULE '{}'", meta);
268267
out.write("bad rule".getBytes());
269268
return;
270269
}

src/main/java/org/apache/joshua/decoder/NbestMinRiskReranker.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131

3232
import org.apache.joshua.util.Ngram;
3333
import org.apache.joshua.util.Regex;
34-
34+
import org.slf4j.Logger;
35+
import org.slf4j.LoggerFactory;
3536

3637
/**
3738
* this class implements: (1) nbest min risk (MBR) reranking using BLEU as a gain funtion.
@@ -46,6 +47,8 @@
4647
*/
4748
public class NbestMinRiskReranker {
4849

50+
private static final Logger LOG = LoggerFactory.getLogger(NbestMinRiskReranker.class);
51+
4952
// TODO: this functionality is not implemented yet; default is to produce 1best without any
5053
// feature scores;
5154
boolean produceRerankedNbest = false;
@@ -67,7 +70,7 @@ public NbestMinRiskReranker(boolean produceRerankedNbest, double scalingFactor)
6770

6871

6972
public String processOneSent(List<String> nbest, int sentID) {
70-
System.err.println("Now process sentence " + sentID);
73+
LOG.info("Now process sentence {}", sentID);
7174

7275
// step-0: preprocess
7376
// assumption: each hyp has a formate:
@@ -77,7 +80,7 @@ public String processOneSent(List<String> nbest, int sentID) {
7780
if (nbest.size() == 1) {
7881
String[] fields = Regex.threeBarsWithSpace.split(nbest.get(0));
7982
if (fields[1].equals("") || Regex.spaces.matches(fields[1])) {
80-
System.err.println(String.format("-> sentence is empty"));
83+
LOG.warn("-> sentence is empty");
8184
return "";
8285
}
8386
}
@@ -171,7 +174,7 @@ public String processOneSent(List<String> nbest, int sentID) {
171174
*/
172175
}
173176

174-
System.err.println("best gain: " + bestGain);
177+
LOG.info("best gain: {}", bestGain);
175178
if (null == bestHyp) {
176179
throw new RuntimeException("mbr reranked one best is null, must be wrong");
177180
}
@@ -311,8 +314,10 @@ public static void main(String[] args) throws IOException {
311314
// If you don't know what to use for scaling factor, try using 1
312315

313316
if (args.length < 2) {
314-
System.err
315-
.println("usage: java NbestMinRiskReranker <produce_reranked_nbest> <scaling_factor> [numThreads]");
317+
String msg = "usage: java NbestMinRiskReranker <produce_reranked_nbest> <scaling_factor> "
318+
+ "[numThreads]";
319+
System.err.println(msg);
320+
LOG.error(msg);
316321
return;
317322
}
318323
long startTime = System.currentTimeMillis();
@@ -324,7 +329,7 @@ public static void main(String[] args) throws IOException {
324329
NbestMinRiskReranker mbrReranker =
325330
new NbestMinRiskReranker(produceRerankedNbest, scalingFactor);
326331

327-
System.err.println("##############running mbr reranking");
332+
LOG.info("Running mbr reranking");
328333

329334
int oldSentID = -1;
330335
List<String> nbest = new ArrayList<String>();
@@ -390,18 +395,15 @@ public static void main(String[] args) throws IOException {
390395
String best_hyp = result.toString();
391396
System.out.println(best_hyp);
392397
}
393-
394-
395398
} catch (InterruptedException e) {
396-
e.printStackTrace();
399+
LOG.error(e.getMessage(), e);
397400
}
398-
399401
}
400402

401403
scanner.close();
402404

403-
System.err.println("Total running time (seconds) is "
404-
+ (System.currentTimeMillis() - startTime) / 1000.0);
405+
LOG.info("Total running time (seconds) is {} ",
406+
(System.currentTimeMillis() - startTime) / 1000.0);
405407
}
406408

407409
private class RankerTask implements Runnable {

src/main/java/org/apache/joshua/decoder/chart_parser/ComputeNodeResult.java

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import org.apache.joshua.decoder.hypergraph.HGNode;
3232
import org.apache.joshua.decoder.hypergraph.HyperEdge;
3333
import org.apache.joshua.decoder.segment_file.Sentence;
34+
import org.slf4j.Logger;
35+
import org.slf4j.LoggerFactory;
3436

3537
/**
3638
* This class computes the cost of applying a rule.
@@ -41,6 +43,8 @@
4143

4244
public class ComputeNodeResult {
4345

46+
private static final Logger LOG = LoggerFactory.getLogger(ComputeNodeResult.class);
47+
4448
// The cost incurred by the rule itself (and all associated feature functions)
4549
private float transitionCost;
4650

@@ -67,11 +71,9 @@ public ComputeNodeResult(List<FeatureFunction> featureFunctions, Rule rule, List
6771
// whatever costs we incur applying this rule to create a new hyperedge.
6872
float viterbiCost = 0.0f;
6973

70-
if (Decoder.VERBOSE >= 4) {
71-
System.err.println("ComputeNodeResult():");
72-
System.err.println("-> RULE " + rule);
73-
}
74-
74+
LOG.debug("ComputeNodeResult():");
75+
LOG.info("-> RULE {}", rule);
76+
7577
/*
7678
* Here we sum the accumulated cost of each of the tail nodes. The total cost of the new
7779
* hyperedge (the inside or Viterbi cost) is the sum of these nodes plus the cost of the
@@ -80,10 +82,8 @@ public ComputeNodeResult(List<FeatureFunction> featureFunctions, Rule rule, List
8082
*/
8183
if (null != tailNodes) {
8284
for (HGNode item : tailNodes) {
83-
if (Decoder.VERBOSE >= 4) {
84-
System.err.println(" -> item.bestedge: " + item);
85-
System.err.println("-> TAIL NODE " + item);
86-
}
85+
LOG.info("-> item.bestedge: {}", item);
86+
LOG.info("-> TAIL NODE {}", item);
8787
viterbiCost += item.bestHyperedge.getBestDerivationScore();
8888
}
8989
}
@@ -105,11 +105,10 @@ public ComputeNodeResult(List<FeatureFunction> featureFunctions, Rule rule, List
105105

106106
DPState newState = feature.compute(rule, tailNodes, i, j, sourcePath, sentence, acc);
107107
transitionCost += acc.getScore();
108-
109-
if (Decoder.VERBOSE >= 4)
110-
System.err.println(String.format("-> FEATURE %s = %.3f * %.3f = %.3f",
111-
feature.getName(), acc.getScore() / Decoder.weights.getSparse(feature.getName()),
112-
Decoder.weights.getSparse(feature.getName()), acc.getScore()));
108+
109+
LOG.debug("FEATURE {} = {} * {} = {}", feature.getName(),
110+
acc.getScore() / Decoder.weights.getSparse(feature.getName()),
111+
Decoder.weights.getSparse(feature.getName()), acc.getScore());
113112

114113
if (feature.isStateful()) {
115114
futureCostEstimate += feature.estimateFutureCost(rule, newState, sentence);
@@ -118,9 +117,7 @@ public ComputeNodeResult(List<FeatureFunction> featureFunctions, Rule rule, List
118117
}
119118

120119
viterbiCost += transitionCost;
121-
122-
if (Decoder.VERBOSE >= 4)
123-
System.err.println(String.format("-> COST = %.3f", transitionCost));
120+
LOG.debug("-> COST = {}", transitionCost);
124121

125122
// Set the final results.
126123
this.pruningCostEstimate = viterbiCost + futureCostEstimate;

src/main/java/org/apache/joshua/decoder/ff/RuleCountBin.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,16 @@
2727
import org.apache.joshua.decoder.ff.tm.Rule;
2828
import org.apache.joshua.decoder.hypergraph.HGNode;
2929
import org.apache.joshua.decoder.segment_file.Sentence;
30+
import org.slf4j.Logger;
31+
import org.slf4j.LoggerFactory;
3032

3133
/*
3234
* This feature computes a bin for the rule and activates a feature for it. It requires access to
3335
* the index of the RarityPenalty field, from which the rule count can be computed.
3436
*/
3537
public class RuleCountBin extends StatelessFF {
38+
39+
private static final Logger LOG = LoggerFactory.getLogger(RuleCountBin.class);
3640
private int field = -1;
3741

3842
public RuleCountBin(FeatureVector weights, String[] args, JoshuaConfiguration config) {
@@ -61,7 +65,7 @@ public DPState compute(Rule rule, List<HGNode> tailNodes, int i, int j, SourcePa
6165
}
6266
}
6367

64-
System.err.println(String.format("RuleCountBin(%f) = %d ==> %s", rarityPenalty, count, feature));
68+
LOG.debug("RuleCountBin({}) = {} ==> {}", rarityPenalty, count, feature);
6569

6670
acc.add(feature, 1.0f);
6771

src/main/java/org/apache/joshua/decoder/ff/fragmentlm/FragmentLMFF.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import org.apache.joshua.decoder.hypergraph.HGNode;
3737
import org.apache.joshua.decoder.hypergraph.HyperEdge;
3838
import org.apache.joshua.decoder.segment_file.Sentence;
39+
import org.slf4j.Logger;
40+
import org.slf4j.LoggerFactory;
3941

4042
/**
4143
* Feature function that reads in a list of language model fragments and matches them against the
@@ -68,6 +70,8 @@
6870
*/
6971
public class FragmentLMFF extends StatefulFF {
7072

73+
private static final Logger LOG = LoggerFactory.getLogger(FragmentLMFF.class);
74+
7175
/*
7276
* When building a fragment from a rule rooted in the hypergraph, this parameter determines how
7377
* deep we'll go. Smaller values mean less hypergraph traversal but may also limit the LM
@@ -131,8 +135,7 @@ public FragmentLMFF(FeatureVector weights, String[] args, JoshuaConfiguration co
131135
throw new RuntimeException(String.format("* WARNING: couldn't read fragment LM file '%s'",
132136
fragmentLMFile), e);
133137
}
134-
System.err.println(String.format("FragmentLMFF: Read %d LM fragments from '%s'", numFragments,
135-
fragmentLMFile));
138+
LOG.info("FragmentLMFF: Read {} LM fragments from '{}'", numFragments, fragmentLMFile);
136139
}
137140

138141
/**
@@ -147,19 +150,18 @@ public void addLMFragment(Tree fragment) {
147150
int fragmentDepth = fragment.getDepth();
148151

149152
if (MAX_DEPTH != 0 && fragmentDepth > MAX_DEPTH) {
150-
System.err.println(String.format(" Skipping fragment %s (depth %d > %d)", fragment,
151-
fragmentDepth, MAX_DEPTH));
153+
LOG.warn("Skipping fragment {} (depth {} > {})", fragment, fragmentDepth, MAX_DEPTH);
152154
return;
153155
}
154156

155157
if (MIN_LEX_DEPTH > 1 && fragment.isLexicalized() && fragmentDepth < MIN_LEX_DEPTH) {
156-
System.err.println(String.format(" Skipping fragment %s (lex depth %d < %d)", fragment,
157-
fragmentDepth, MIN_LEX_DEPTH));
158+
LOG.warn("Skipping fragment {} (lex depth {} < {})", fragment, fragmentDepth, MIN_LEX_DEPTH);
158159
return;
159160
}
160161

161-
if (lmFragments.get(fragment.getRule()) == null)
162+
if (lmFragments.get(fragment.getRule()) == null) {
162163
lmFragments.put(fragment.getRule(), new ArrayList<Tree>());
164+
}
163165
lmFragments.get(fragment.getRule()).add(fragment);
164166
numFragments++;
165167
}
@@ -314,7 +316,7 @@ public static void main(String[] args) {
314316

315317
Tree tree = Tree.buildTree(ruleS, tailNodes, 1);
316318
boolean matched = fragmentLMFF.match(fragment, tree);
317-
System.err.println(String.format("Does\n %s match\n %s??\n -> %s", fragment, tree, matched));
319+
LOG.info("Does\n {} match\n {}??\n -> {}", fragment, tree, matched);
318320
}
319321

320322
/**

src/main/java/org/apache/joshua/decoder/ff/fragmentlm/Tree.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -550,10 +550,12 @@ public static Tree buildTree(Rule rule, DerivationState[] derivationStates, int
550550
}
551551

552552
tree = tree.shallowClone();
553-
554-
System.err.println(String.format("buildTree(%s)", tree));
555-
for (int i = 0; i < derivationStates.length; i++) {
556-
System.err.println(String.format(" -> %d: %s", i, derivationStates[i]));
553+
554+
if (LOG.isDebugEnabled()) {
555+
LOG.debug("buildTree({})", tree);
556+
for (int i = 0; i < derivationStates.length; i++) {
557+
LOG.debug(" -> {}: {}", i, derivationStates[i]);
558+
}
557559
}
558560

559561
List<Tree> frontier = tree.getNonterminalYield();
@@ -630,7 +632,7 @@ public static Tree buildTree(DerivationState derivationState, int maxDepth) {
630632

631633
tree = tree.shallowClone();
632634

633-
System.err.println(String.format("buildTree(%s)", tree));
635+
LOG.debug("buildTree({})", tree);
634636

635637
if (rule.getArity() > 0 && maxDepth > 0) {
636638
List<Tree> frontier = tree.getNonterminalYield();

src/main/java/org/apache/joshua/decoder/ff/lm/KenLM.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import org.apache.joshua.corpus.Vocabulary;
2222
import org.apache.joshua.decoder.ff.lm.NGramLanguageModel;
2323
import org.apache.joshua.decoder.ff.state_maintenance.KenLMState;
24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
2426

2527
/**
2628
* JNI wrapper for KenLM. This version of KenLM supports two use cases, implemented by the separate
@@ -34,17 +36,19 @@
3436

3537
public class KenLM implements NGramLanguageModel, Comparable<KenLM> {
3638

39+
private static final Logger LOG = LoggerFactory.getLogger(KenLM.class);
40+
3741
static {
3842
try {
3943
System.loadLibrary("ken");
4044
} catch (UnsatisfiedLinkError e) {
4145
//TODO: send these prints to LOG.err
42-
System.err.println("* FATAL: Can't find libken.so (libken.dylib on OS X) in $JOSHUA/lib");
43-
System.err.println("* This probably means that the KenLM library didn't compile.");
44-
System.err.println("* Make sure that BOOST_ROOT is set to the root of your boost");
45-
System.err.println("* installation (it's not /opt/local/, the default), change to");
46-
System.err.println("* $JOSHUA, and type 'ant kenlm'. If problems persist, see the");
47-
System.err.println("* website (joshua-decoder.org).");
46+
LOG.error("* FATAL: Can't find libken.so (libken.dylib on OS X) in $JOSHUA/lib");
47+
LOG.error("* This probably means that the KenLM library didn't compile.");
48+
LOG.error("* Make sure that BOOST_ROOT is set to the root of your boost");
49+
LOG.error("* installation (it's not /opt/local/, the default), change to");
50+
LOG.error("* $JOSHUA, and type 'ant kenlm'. If problems persist, see the");
51+
LOG.error("* website (joshua-decoder.org)."); //FIXME: update link to newer url
4852
throw new RuntimeException(e);
4953
}
5054
}

0 commit comments

Comments
 (0)