Skip to content

Commit

Permalink
LinkedHashMap instead of Properties. Addresses #1289
Browse files Browse the repository at this point in the history
  • Loading branch information
AngledLuffa committed Aug 4, 2022
1 parent 0d2f495 commit 6550188
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
12 changes: 6 additions & 6 deletions src/edu/stanford/nlp/process/PTBLexer.flex
Expand Up @@ -28,9 +28,9 @@ package edu.stanford.nlp.process;

import java.io.Reader;
import java.text.Normalizer;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -139,11 +139,11 @@ import edu.stanford.nlp.util.logging.Redwood;
if (options == null) {
options = "";
}
Properties prop = StringUtils.stringToProperties(options);
Set<Map.Entry<Object,Object>> props = prop.entrySet();
for (Map.Entry<Object,Object> item : props) {
String key = (String) item.getKey();
String value = (String) item.getValue();
LinkedHashMap<String, String> prop = StringUtils.stringToPropertiesMap(options);
Set<Map.Entry<String, String>> props = prop.entrySet();
for (Map.Entry<String, String> item : props) {
String key = item.getKey();
String value = item.getValue();
boolean val = Boolean.parseBoolean(value);
if ("".equals(key)) {
// allow an empty item
Expand Down
12 changes: 6 additions & 6 deletions src/edu/stanford/nlp/process/PTBLexer.java
Expand Up @@ -32,9 +32,9 @@

import java.io.Reader;
import java.text.Normalizer;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -60954,11 +60954,11 @@ public PTBLexer(Reader r, LexedTokenFactory<?> tf, String options) {
if (options == null) {
options = "";
}
Properties prop = StringUtils.stringToProperties(options);
Set<Map.Entry<Object,Object>> props = prop.entrySet();
for (Map.Entry<Object,Object> item : props) {
String key = (String) item.getKey();
String value = (String) item.getValue();
LinkedHashMap<String, String> prop = StringUtils.stringToPropertiesMap(options);
Set<Map.Entry<String, String>> props = prop.entrySet();
for (Map.Entry<String, String> item : props) {
String key = item.getKey();
String value = item.getValue();
boolean val = Boolean.parseBoolean(value);
if ("".equals(key)) {
// allow an empty item
Expand Down
29 changes: 29 additions & 0 deletions src/edu/stanford/nlp/util/StringUtils.java
Expand Up @@ -1136,6 +1136,35 @@ public static Properties stringToProperties(String str, Properties props) {
return props;
}

/**
* This method updates a LinkedHashMap based on
* a comma-separated String (with whitespace
* optionally allowed after the comma) representing properties
* to a Properties object. Each property is "property=value". The value
* for properties without an explicitly given value is set to "true".
*
* TODO: remove the stringToProperties version
*/
public static LinkedHashMap<String, String> stringToPropertiesMap(String str) {
LinkedHashMap<String, String> props = new LinkedHashMap<>();

String[] propsStr = str.trim().split(",\\s*");
for (String term : propsStr) {
int divLoc = term.indexOf('=');
String key;
String value;
if (divLoc >= 0) {
key = term.substring(0, divLoc).trim();
value = term.substring(divLoc + 1).trim();
} else {
key = term.trim();
value = "true";
}
props.put(key, value);
}
return props;
}

/**
* If any of the given list of properties are not found, returns the
* name of that property. Otherwise, returns null.
Expand Down

0 comments on commit 6550188

Please sign in to comment.