Skip to content

Commit

Permalink
some test changes for execution stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
reality committed May 19, 2020
1 parent eb20562 commit 21874eb
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 86 deletions.
60 changes: 59 additions & 1 deletion src/main/groovy/komenti/App.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,64 @@ class App {
_ longOpt: 'threads', 'Number of threads to use for query/annotation processes', type: Integer, args: 1
}

Komenti.run(cliBuilder, args)

if(args.contains('--verbose')) {
println args
}

if(!args[0]) { println "Must provide command." ; cliBuilder.usage() ; System.exit(1) }
def command = args[0]
def o = cliBuilder.parse(args.drop(1))

if(o.h) { cliBuilder.usage() ; return; }

def aCheck = checkArguments(command, o)
if(!aCheck) { System.exit(1) }

Komenti."$command"(o)
}

static def checkArguments(command, o) {
def success = true
if(!f.metaClass.respondsTo(Komenti, command)) {
println "Command ${command} not found." ; success = false
}

if(command == 'gen_roster') {
if(!o.q && !o.c) { println "You must provide a --query or --class-list" ; success = false }

// Check that the roster is being generated with some text (annotation mode) or with an analysis method
if(!o['with-abstracts-download'] && !o['with-metadata-download'] && !o['mine-relationship'] && !o.t) {
println "Must either download abstracts, metadata, or provide text to annotate"
success = false
}

if(o['mine-relationship']) {
if(!o.c || (o.c && o.c.split(',').size() != 2)) {
println "to --mine-relationship you must pass exactly two concept names with -c/--class-list"
success = false
}
}

if(o['suggest-axiom']) {
if(!o.o) {
println "Must pass an ontology to query with -o/--ontology"
success = false
}

if((!o.c || (o.c && o.c.split(',').size() != 1))) {
println "You must pass a class into -c to suggest axiom"
success = false
}

if(!o.entity || !o.quality || !o['default-entity'] || !o['default-relation']) {
println "To suggest axiom you must pass class lists with --entity, --quality. You must also pass --default-relation and --default-entity."
success = false
}
}
} else if(command == 'auto') {
if(!o.r) { cliBuilder.usage() ; System.exit(1) }
}
success
}
}
155 changes: 71 additions & 84 deletions src/main/groovy/komenti/Komenti.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -8,107 +8,94 @@ import groovy.json.*
import groovyx.gpars.GParsPool

public class Komenti {
static def run(cliBuilder, args) {
if(!args) { println "Must provide command." ; cliBuilder.usage() ; System.exit(1) }

if(args.contains('--verbose')) {
println args
static def gen_roster(o) {
def cLoader = getClass()
def templateFile = cLoader.getResourceAsStream('/templates/roster.json')
if(o['with-abstracts-download']) {
templateFile = cLoader.getResourceAsStream('/templates/roster_with_abstract_download.json')
}
if(o['mine-relationship']) {
templateFile = cLoader.getResourceAsStream('/templates/roster_mine_relationship.json')
}
if(o['suggest-axiom']) {
templateFile = cLoader.getResourceAsStream('/templates/roster_suggest_axiom.json')
}

def command = args[0]
def o = cliBuilder.parse(args.drop(1))

if(o.h) { cliBuilder.usage() ; System.exit(0) }
def roster = new JsonSlurper().parseText(templateFile.getText())

// TODO this can probably mostly be automated
if(o.q) {
roster.commands.find { it.id == 'class_query' }.args.query = o.q
} else {
roster.commands.find { it.id == 'class_query' }.args['class-list'] = o.c
}
if(o.o) { roster.commands.find { it.id == 'class_query' }.args.ontology = o.o }

if(command == 'gen_roster') {
if(!o.q && !o.c) { println "You must provide a query or class-list" ; cliBuilder.usage() ; System.exit(1) }
if(!o.out) { println "Must provide place to save roster" ; cliBuilder.usage() ; System.exit(1) }
if(!o['with-abstracts-download'] && !o['with-metadata-download'] && !o['mine-relationship'] && !o.t) { println "Must either download abstracts, metadata, or provide text to annotate" ; cliBuilder.usage() ; System.exit(1) }
if(o['mine-relationship'] && (!o.c || (o.c && o.c.split(',').size() != 2))) { println "to --mine-relationship you must pass exactly two concept names with -c/--class-list" ; System.exit(1) }
if(!o.o) { println "Must pass an ontology to query with -o/--ontology" ; System.exit(1) }
if(o['suggest-axiom'] && (!o.c || (o.c && o.c.split(',').size() != 1) || (!o.entity || !o.quality) || !o['default-entity'] || !o['default-relation'])) { println "to --suggest-axiom you must pass class lists for each -c, --entity, --quality. You must also pass --default-relation and --default-entity." ; System.exit(1) }

def cLoader = getClass()

def templateFile = cLoader.getResourceAsStream('/templates/roster.json')
if(o['with-abstracts-download']) {
templateFile = cLoader.getResourceAsStream('/templates/roster_with_abstract_download.json')
}
if(o['mine-relationship']) {
templateFile = cLoader.getResourceAsStream('/templates/roster_mine_relationship.json')
}
if(o['suggest-axiom']) {
templateFile = cLoader.getResourceAsStream('/templates/roster_suggest_axiom.json')
}
if(o['with-abstracts-download']) {
if(o.l) { roster.commands.find { it.command == 'get_abstracts' }.args.limit = o.l }
}

def roster = new JsonSlurper().parseText(templateFile.getText())

// TODO this can probably mostly be automated
if(o.q) {
roster.commands.find { it.id == 'class_query' }.args.query = o.q
} else {
roster.commands.find { it.id == 'class_query' }.args['class-list'] = o.c
}
if(o.o) { roster.commands.find { it.id == 'class_query' }.args.ontology = o.o }
if(!o['with-abstracts-download'] && !o['with-metadata-download']) {
roster.commands = roster.commands.findAll { it.command != 'get_abstracts' && it.command != 'get_metadata' }
roster.commands.find { it.command == 'annotate' }.text = o.t
} else if(!o['with-abstracts-download']) {
roster.commands = roster.commands.findAll { it.command != 'get_abstracts' }
} else if(!o['with-metadata-download']) {
roster.commands = roster.commands.findAll { it.command != 'get_metadata' }
}

if(o['with-abstracts-download']) {
if(o.l) { roster.commands.find { it.command == 'get_abstracts' }.args.limit = o.l }
}
if(o['mine-relationship']) {
roster.commands.find { it.command == 'summarise_entity_pair' }.args['class-list'] = o.c
}

if(!o['with-abstracts-download'] && !o['with-metadata-download']) {
roster.commands = roster.commands.findAll { it.command != 'get_abstracts' && it.command != 'get_metadata' }
roster.commands.find { it.command == 'annotate' }.text = o.t
} else if(!o['with-abstracts-download']) {
roster.commands = roster.commands.findAll { it.command != 'get_abstracts' }
} else if(!o['with-metadata-download']) {
roster.commands = roster.commands.findAll { it.command != 'get_metadata' }
}
if(o['suggest-axiom']) {
def cq = roster.commands.find { it.id == 'class_query' }
cq.args['class-list'] = o.c
cq.args['ontology'] = o.o

if(o['mine-relationship']) {
roster.commands.find { it.command == 'summarise_entity_pair' }.args['class-list'] = o.c
}
def eq = roster.commands.find { it.id == 'entity_query' }
eq.args['class-list'] = o.entity
eq.args['ontology'] = o.eo ? o.eo : o.o

if(o['suggest-axiom']) {
def cq = roster.commands.find { it.id == 'class_query' }
cq.args['class-list'] = o.c
cq.args['ontology'] = o.o
def rq = roster.commands.find { it.id == 'relation_query' }
rq.args['ontology'] = o.oo ? o.oo : o.o

def eq = roster.commands.find { it.id == 'entity_query' }
eq.args['class-list'] = o.entity
eq.args['ontology'] = o.eo ? o.eo : o.o
def qq = roster.commands.find { it.id == 'quality_query' }
qq.args['class-list'] = o.quality
qq.args['ontology'] = o.qo ? o.qo : o.o

def rq = roster.commands.find { it.id == 'relation_query' }
rq.args['ontology'] = o.oo ? o.oo : o.o
def sa = roster.commands.find { it.command == 'suggest_axiom' }
sa.args['default-relation'] = o['default-relation']
sa.args['default-entity'] = o['default-entity']
}

def qq = roster.commands.find { it.id == 'quality_query' }
qq.args['class-list'] = o.quality
qq.args['ontology'] = o.qo ? o.qo : o.o
new File(o.out).text = JsonOutput.prettyPrint(JsonOutput.toJson(roster))
println "Roster file saved to $o.out, where you can edit it manually. You can run it with 'groovy Komenti auto -r $o.out'"
}

def sa = roster.commands.find { it.command == 'suggest_axiom' }
sa.args['default-relation'] = o['default-relation']
sa.args['default-entity'] = o['default-entity']
}
static def auto(o) {
def roster = new JsonSlurper().parse(new File(o.r))

new File(o.out).text = JsonOutput.prettyPrint(JsonOutput.toJson(roster))
println "Roster file saved to $o.out, where you can edit it manually. You can run it with 'groovy Komenti auto -r $o.out'"
} else if(command == 'auto') {
if(!o.r) { cliBuilder.usage() ; System.exit(1) }
roster.commands.each { item ->
def newArgs = [item.command] + item.args.collect { k, v ->
if(k.size() == 1) { k = "-$k" } else { k = "--$k" }
if(v instanceof String && v.split(' ').size() > 1 && v[0] != '"') { v = '"' + v + '"' }
if(v instanceof Integer || v instanceof Boolean) { v = "$v" } // foolish cliBuilder ...

def roster = new JsonSlurper().parse(new File(o.r))
[k, v]
}.flatten()
newArgs.removeAll("true")

roster.commands.each { item ->
def newArgs = [item.command] + item.args.collect { k, v ->
if(k.size() == 1) { k = "-$k" } else { k = "--$k" }
if(v instanceof String && v.split(' ').size() > 1 && v[0] != '"') { v = '"' + v + '"' }
if(v instanceof Integer || v instanceof Boolean) { v = "$v" } // foolish cliBuilder ...
App.main(newArgs)
}
}


[k, v]
}.flatten()
newArgs.removeAll("true")
}

run(cliBuilder, newArgs)
}
} else if(command == 'query') {
static def run(cliBuilder, o) {
if(command == 'gen_roster') {
} else if(command == 'query') {
if((!o['object-properties'] && (!o.q && !o.c))) { cliBuilder.usage() ; System.exit(1) }
if(o['object-properties'] && (o.q || o.c)) { println "Cannot pass a query or class list for --object-properties query" ; System.exit(1) }

Expand Down
2 changes: 1 addition & 1 deletion src/main/groovy/komenti/klib/Sentence.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Sentence {
def rText
def nWords
def negated = null
def NEG_MODS = [ 'negative', 'exclude', 'without', 'no', 'excluded', 'not', 'denies', 'free', 'deny', 'denied', 'stop', 'stopped' ]
def NEG_MODS = [ 'negative', 'excluding', 'exclude', 'without', 'no', 'excluded', 'not', 'denies', 'free', 'deny', 'denied', 'stop', 'stopped' ]

def Sentence(text, fileName) {
this.text = text
Expand Down

0 comments on commit 21874eb

Please sign in to comment.