8
8
9
9
import java .io .Serializable ;
10
10
11
- // import edu.stanford.nlp.util.logging.Redwood ;
11
+ import edu .stanford .nlp .util .StringUtils ;
12
12
13
13
14
14
/**
@@ -105,7 +105,7 @@ protected void setGlobalHolder(MaxentTagger tagger) {}
105
105
* @param tag The possible tag that the feature will be generated for
106
106
* @return Whether the feature extractor is applicable (true) or not (false)
107
107
*/
108
- @ SuppressWarnings ({"MethodMayBeStatic" , " UnusedDeclaration" })
108
+ @ SuppressWarnings ({"UnusedDeclaration" })
109
109
public boolean precondition (String tag ) {
110
110
return true ;
111
111
}
@@ -238,22 +238,30 @@ public String toString() {
238
238
239
239
240
240
/** This is used for argument parsing in arch variable.
241
- * It can extract a comma separated argument.
242
- * Assumes the input format is "name(arg,arg,arg)".
241
+ * It can extract from a comma separated values argument list.
242
+ * Values can be quoted with double quotes (with a second double quote as double quote escape char)
243
+ * like in a regular CSV file. It assumes the input format is "name(arg,arg,arg)".
243
244
*
244
245
* @param str arch variable component input
245
- * @param num Number of argument
246
+ * @param num Number of argument. Numbers are 1-indexed (i.e., start from 1 not 0)
246
247
* @return The parenthesized String, or null if none.
247
248
*/
248
249
static String getParenthesizedArg (String str , int num ) {
249
- String [] args = str .split ("\\ s*[,()]\\ s*" );
250
- if (args .length <= num ) {
251
- return null ;
250
+ int left = str .indexOf ('(' );
251
+ int right = str .lastIndexOf (')' );
252
+ if (left < 0 || right <= left ) {
253
+ throw new IllegalArgumentException ("getParenthesizedArg: Bad format String: " + str );
252
254
}
255
+ String argStr = str .substring (left + 1 , right );
256
+ String [] args = StringUtils .splitOnCharWithQuoting (argStr , ',' , '"' , '"' );
253
257
// log.info("getParenthesizedArg split " + str + " into " + args.length + " pieces; returning number " + num);
254
258
// for (int i = 0; i < args.length; i++) {
255
259
// log.info(" " + args[i]);
256
260
// }
261
+ num --;
262
+ if (args .length <= num || num < 0 ) {
263
+ return null ;
264
+ }
257
265
return args [num ];
258
266
}
259
267
@@ -266,11 +274,12 @@ static String getParenthesizedArg(String str, int num) {
266
274
* @param num Number of argument
267
275
* @return The int value of the arg or 0 if missing or empty
268
276
*/
277
+ @ SuppressWarnings ("ConstantConditions" )
269
278
static int getParenthesizedNum (String str , int num ) {
270
- String [] args = str . split ( " \\ s*[,()] \\ s*" );
279
+ String arg = getParenthesizedArg ( str , num );
271
280
int ans = 0 ;
272
281
try {
273
- ans = Integer .parseInt (args [ num ] );
282
+ ans = Integer .parseInt (arg );
274
283
} catch (NumberFormatException | ArrayIndexOutOfBoundsException nfe ) {
275
284
// just leave ans as 0
276
285
}
0 commit comments