Skip to content

Commit 429c0ec

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 e11d14f commit 429c0ec

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
@@ -530,12 +530,17 @@ public static void main(String[] args) throws IOException {
530530
Map<String, String[]> argsMap = StringUtils.argsToMap(args, flagMap);
531531
// args = argsMap.get(null);
532532

533-
// TODO: allow patterns to be extracted from a file
534533
if (!(argsMap.containsKey(PATTERN)) || argsMap.get(PATTERN).length == 0) {
535534
help();
536535
System.exit(2);
537536
}
538-
SemgrexPattern semgrex = SemgrexPattern.compile(argsMap.get(PATTERN)[0]);
537+
SemgrexPattern semgrex;
538+
try {
539+
String pattern = IOUtils.slurpFile(argsMap.get(PATTERN)[0]);
540+
semgrex = SemgrexPattern.compile(pattern);
541+
} catch(IOException e) {
542+
semgrex = SemgrexPattern.compile(argsMap.get(PATTERN)[0]);
543+
}
539544

540545
String modeString = DEFAULT_MODE;
541546
if (argsMap.containsKey(MODE) && argsMap.get(MODE).length > 0) {
@@ -576,11 +581,20 @@ public static void main(String[] args) throws IOException {
576581
if (argsMap.containsKey(CONLLU_FILE) && argsMap.get(CONLLU_FILE).length > 0) {
577582
try {
578583
CoNLLUReader reader = new CoNLLUReader();
579-
for (String conlluFile : argsMap.get(CONLLU_FILE)) {
580-
log.info("Loading file " + conlluFile);
581-
List<Annotation> docs = reader.readCoNLLUFile(conlluFile);
582-
for (Annotation doc : docs) {
583-
sentences.addAll(doc.get(CoreAnnotations.SentencesAnnotation.class));
584+
for (String conlluPath : argsMap.get(CONLLU_FILE)) {
585+
File file = new File(conlluPath);
586+
List<File> filenames;
587+
if (file.isFile()) {
588+
filenames = Collections.singletonList(file);
589+
} else {
590+
filenames = Arrays.asList(file.listFiles());
591+
}
592+
for (File conlluFile : filenames) {
593+
log.info("Loading file " + conlluFile);
594+
List<Annotation> docs = reader.readCoNLLUFile(conlluFile.toString());
595+
for (Annotation doc : docs) {
596+
sentences.addAll(doc.get(CoreAnnotations.SentencesAnnotation.class));
597+
}
584598
}
585599
}
586600
} catch (ClassNotFoundException e) {

0 commit comments

Comments
 (0)