Skip to content

Commit 818846d

Browse files
committed
Update most, but not all of the uses of stringToProperties. The serialized parser objects still have one because the BaseLexicons were serialized with a TestOptions inside them
1 parent 6550188 commit 818846d

File tree

4 files changed

+41
-22
lines changed

4 files changed

+41
-22
lines changed

src/edu/stanford/nlp/process/WhitespaceTokenizer.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
import java.io.PrintWriter;
88
import java.io.Reader;
99
import java.util.Iterator;
10-
import java.util.Properties;
10+
import java.util.LinkedHashMap;
1111

1212
import edu.stanford.nlp.ling.CoreLabel;
1313
import edu.stanford.nlp.ling.HasWord;
1414
import edu.stanford.nlp.ling.Word;
15-
import edu.stanford.nlp.util.PropertiesUtils;
15+
import edu.stanford.nlp.util.Maps;
1616
import edu.stanford.nlp.util.StringUtils;
1717

1818
/**
@@ -76,8 +76,8 @@ public WhitespaceTokenizerFactory(LexedTokenFactory<T> factory) {
7676
public WhitespaceTokenizerFactory(LexedTokenFactory<T> factory,
7777
String options) {
7878
this.factory = factory;
79-
Properties prop = StringUtils.stringToProperties(options);
80-
this.tokenizeNLs = PropertiesUtils.getBool(prop, "tokenizeNLs", false);
79+
LinkedHashMap<String, String> prop = StringUtils.stringToPropertiesMap(options);
80+
this.tokenizeNLs = Maps.getBool(prop, "tokenizeNLs", false);
8181
}
8282

8383
public WhitespaceTokenizerFactory(LexedTokenFactory<T> factory,
@@ -98,16 +98,16 @@ public Tokenizer<T> getTokenizer(Reader r) {
9898

9999
@Override
100100
public Tokenizer<T> getTokenizer(Reader r, String extraOptions) {
101-
Properties prop = StringUtils.stringToProperties(extraOptions);
102-
boolean tokenizeNewlines = PropertiesUtils.getBool(prop, "tokenizeNLs", this.tokenizeNLs);
101+
LinkedHashMap<String, String> prop = StringUtils.stringToPropertiesMap(extraOptions);
102+
boolean tokenizeNewlines = Maps.getBool(prop, "tokenizeNLs", this.tokenizeNLs);
103103

104104
return new WhitespaceTokenizer<>(factory, r, tokenizeNewlines);
105105
}
106106

107107
@Override
108108
public void setOptions(String options) {
109-
Properties prop = StringUtils.stringToProperties(options);
110-
tokenizeNLs = PropertiesUtils.getBool(prop, "tokenizeNLs", tokenizeNLs);
109+
LinkedHashMap<String, String> prop = StringUtils.stringToPropertiesMap(options);
110+
tokenizeNLs = Maps.getBool(prop, "tokenizeNLs", tokenizeNLs);
111111
}
112112

113113
} // end class WhitespaceTokenizerFactory

src/edu/stanford/nlp/process/WordSegmentingTokenizer.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
import java.util.Collections;
66
import java.util.Iterator;
77
import java.util.List;
8-
import java.util.Properties;
8+
import java.util.LinkedHashMap;
99

1010
import edu.stanford.nlp.ling.CoreLabel;
1111
import edu.stanford.nlp.ling.HasWord;
12-
import edu.stanford.nlp.util.PropertiesUtils;
12+
import edu.stanford.nlp.util.Maps;
1313
import edu.stanford.nlp.util.StringUtils;
1414

1515
/** A tokenizer that works by calling a WordSegmenter.
@@ -82,16 +82,16 @@ public Tokenizer<HasWord> getTokenizer(Reader r) {
8282
public Tokenizer<HasWord> getTokenizer(Reader r, String extraOptions) {
8383
boolean tokenizeNewlines = this.tokenizeNLs;
8484
if (extraOptions != null) {
85-
Properties prop = StringUtils.stringToProperties(extraOptions);
86-
tokenizeNewlines = PropertiesUtils.getBool(prop, "tokenizeNLs", this.tokenizeNLs);
85+
LinkedHashMap<String, String> prop = StringUtils.stringToPropertiesMap(extraOptions);
86+
tokenizeNewlines = Maps.getBool(prop, "tokenizeNLs", this.tokenizeNLs);
8787
}
8888

8989
return new WordSegmentingTokenizer(segmenter, WhitespaceTokenizer.newCoreLabelWhitespaceTokenizer(r, tokenizeNewlines));
9090
}
9191

9292
public void setOptions(String options) {
93-
Properties prop = StringUtils.stringToProperties(options);
94-
tokenizeNLs = PropertiesUtils.getBool(prop, "tokenizeNLs", tokenizeNLs);
93+
LinkedHashMap<String, String> prop = StringUtils.stringToPropertiesMap(options);
94+
tokenizeNLs = Maps.getBool(prop, "tokenizeNLs", tokenizeNLs);
9595
}
9696
}
9797
}

src/edu/stanford/nlp/trees/TreePrint.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ public class TreePrint {
5151
"conll2007"
5252
};
5353

54-
private final Properties formats;
55-
private final Properties options;
54+
private final LinkedHashMap<String, String> formats;
55+
private final LinkedHashMap<String, String> options;
5656

5757
private final boolean markHeadNodes; // = false;
5858
private final boolean lexicalize; // = false;
@@ -143,11 +143,10 @@ public TreePrint(String formats, String options, TreebankLanguagePack tlp) {
143143
* @param hf The HeadFinder used in printing output
144144
*/
145145
public TreePrint(String formatString, String optionsString, TreebankLanguagePack tlp, HeadFinder hf, HeadFinder typedDependencyHF) {
146-
formats = StringUtils.stringToProperties(formatString);
147-
options = StringUtils.stringToProperties(optionsString);
146+
formats = StringUtils.stringToPropertiesMap(formatString);
147+
options = StringUtils.stringToPropertiesMap(optionsString);
148148
List<String> okOutputs = Arrays.asList(outputTreeFormats);
149-
for (Object formObj : formats.keySet()) {
150-
String format = (String) formObj;
149+
for (String format : formats.keySet()) {
151150
if ( ! okOutputs.contains(format)) {
152151
throw new RuntimeException("Error: output tree format " + format + " not supported. Known formats are: " + okOutputs);
153152
}
@@ -208,8 +207,8 @@ public TreePrint(String formatString, String optionsString, TreebankLanguagePack
208207
}
209208

210209

211-
private static boolean propertyToBoolean(Properties prop, String key) {
212-
return Boolean.parseBoolean(prop.getProperty(key));
210+
private static boolean propertyToBoolean(LinkedHashMap<String, String> prop, String key) {
211+
return Boolean.parseBoolean(prop.get(key));
213212
}
214213

215214
/**

src/edu/stanford/nlp/util/Maps.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,26 @@ public static<T,V> Map<T, V> getAll(Map<T, V> map, Collection<T> indices){
164164
return result;
165165
}
166166

167+
/**
168+
* Load a boolean property from a Map. If the key is not present, returns false.
169+
*/
170+
public static boolean getBool(Map<String, String> props, String key) {
171+
return getBool(props, key, false);
172+
}
173+
174+
/**
175+
* Load a boolean property from a Map. If the key is not present, returns defaultValue.
176+
*/
177+
public static boolean getBool(Map<String, String> props, String key,
178+
boolean defaultValue) {
179+
String value = props.get(key);
180+
if (value != null) {
181+
return Boolean.parseBoolean(value);
182+
} else {
183+
return defaultValue;
184+
}
185+
}
186+
167187
/**
168188
* Pretty print a Counter. This one has more flexibility in formatting, and
169189
* doesn't sort the keys.

0 commit comments

Comments
 (0)