Skip to content

Commit dc898c5

Browse files
committed
Attempt to read -pattern as a filename - presumably filenames won't typically work directly as a search string. Read in multiple conllu files if multiple files are provided.
1 parent 3d2c5d4 commit dc898c5

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

src/edu/stanford/nlp/semgraph/semgrex/SemgrexPattern.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -516,12 +516,17 @@ public static void main(String[] args) throws IOException {
516516
Map<String, String[]> argsMap = StringUtils.argsToMap(args, flagMap);
517517
// args = argsMap.get(null);
518518

519-
// TODO: allow patterns to be extracted from a file
520519
if (!(argsMap.containsKey(PATTERN)) || argsMap.get(PATTERN).length == 0) {
521520
help();
522521
System.exit(2);
523522
}
524-
SemgrexPattern semgrex = SemgrexPattern.compile(argsMap.get(PATTERN)[0]);
523+
SemgrexPattern semgrex;
524+
try {
525+
String pattern = IOUtils.slurpFile(argsMap.get(PATTERN)[0]);
526+
semgrex = SemgrexPattern.compile(pattern);
527+
} catch(IOException e) {
528+
semgrex = SemgrexPattern.compile(argsMap.get(PATTERN)[0]);
529+
}
525530

526531
String modeString = DEFAULT_MODE;
527532
if (argsMap.containsKey(MODE) && argsMap.get(MODE).length > 0) {
@@ -562,11 +567,20 @@ public static void main(String[] args) throws IOException {
562567
if (argsMap.containsKey(CONLLU_FILE) && argsMap.get(CONLLU_FILE).length > 0) {
563568
try {
564569
CoNLLUReader reader = new CoNLLUReader();
565-
for (String conlluFile : argsMap.get(CONLLU_FILE)) {
566-
log.info("Loading file " + conlluFile);
567-
List<Annotation> docs = reader.readCoNLLUFile(conlluFile);
568-
for (Annotation doc : docs) {
569-
sentences.addAll(doc.get(CoreAnnotations.SentencesAnnotation.class));
570+
for (String conlluPath : argsMap.get(CONLLU_FILE)) {
571+
File file = new File(conlluPath);
572+
List<File> filenames;
573+
if (file.isFile()) {
574+
filenames = Collections.singletonList(file);
575+
} else {
576+
filenames = Arrays.asList(file.listFiles());
577+
}
578+
for (File conlluFile : filenames) {
579+
log.info("Loading file " + conlluFile);
580+
List<Annotation> docs = reader.readCoNLLUFile(conlluFile.toString());
581+
for (Annotation doc : docs) {
582+
sentences.addAll(doc.get(CoreAnnotations.SentencesAnnotation.class));
583+
}
570584
}
571585
}
572586
} catch (ClassNotFoundException e) {

0 commit comments

Comments
 (0)