mirror of https://github.com/percyliang/sempre
simulator, sandboxing, bug fixes
This commit is contained in:
parent
3f4d37bf4d
commit
a093ef5b60
38
run
38
run
|
|
@ -1012,19 +1012,18 @@ o('Params.l1Reg', 'lazy'),
|
|||
o('Params.l1RegCoeff', 0.001),
|
||||
o('Parser.beamSize', 10),
|
||||
o('Params.initStepSize', 0.1),
|
||||
o('Params.adaptiveStepSize', true),
|
||||
o('Params.adaptiveStepSize', false),
|
||||
|
||||
o('FeatureExtractor.featureComputers', 'interactive.ActionFeatureComputer'),
|
||||
o('FeatureExtractor.featureDomains', 'rule', 'stats'),
|
||||
|
||||
lambda { |e| system 'mkdir -p ./int-output/'},
|
||||
lambda { |e| system 'mkdir -p ./int-output/log/'},
|
||||
lambda { |e| system 'mkdir -p ./int-output/grammar/'},
|
||||
lambda { |e| system 'mkdir -p ./int-output/citation/'},
|
||||
|
||||
# o('Master.newExamplesPath', './int-output/examples/'),
|
||||
# o('Master.logPath', './int-output/logs/'),
|
||||
o('Master.newGrammarPath', './int-output/grammar/'),
|
||||
o('Master.newGrammarPath', './int-output/grammar.lisp'),
|
||||
|
||||
# o('ILUtils.JSONLogPath', './int-output/json/'),
|
||||
o('ILUtils.citationPath', './int-output/citation/'),
|
||||
|
|
@ -1050,39 +1049,6 @@ o('Derivation.showTypes', false),
|
|||
o('Derivation.showValues', false),
|
||||
o('Derivation.showRules', false),
|
||||
nil) })
|
||||
|
||||
# Community Server
|
||||
addMode('int-utils', 'small commands like run community server, backup, or simulator', lambda { |e| l(
|
||||
letDefault(:cmd, 'help'),
|
||||
sel(:cmd, {
|
||||
'backup-mv' => l(
|
||||
lambda { |e| system 'echo "backing up with mv"'},
|
||||
lambda { |e| system 'mkdir -p ./int-backup/'},
|
||||
lambda { |e| system 'mv int-output int-backup/`date +%Y-%m-%d.%H:%M:%S`'},
|
||||
lambda { |e| system 'mkdir -p ./int-output'}
|
||||
),
|
||||
'backup-cp' => l(
|
||||
lambda { |e| system 'echo "backing up with cp"'},
|
||||
lambda { |e| system 'mkdir -p ./int-backup/'},
|
||||
lambda { |e| system 'cp int-output int-backup/`date +%Y-%m-%d.%H:%M:%S`'},
|
||||
lambda { |e| system 'mkdir -p ./int-output'}
|
||||
),
|
||||
'community' => l('python community-server/server.py --port 8403'),
|
||||
'simulator' => l(
|
||||
rlwrap,
|
||||
header('core,interactive'),
|
||||
'edu.stanford.nlp.sempre.interactive.Simulator',
|
||||
# figOpts,
|
||||
letDefault(:server, 'local'),
|
||||
sel(:server, {
|
||||
'local' => o('serverURL', 'http://localhost:8410'),
|
||||
'remote' => o('serverURL', 'http://jonsson.stanford.edu:8410')
|
||||
}),
|
||||
o('sandbox', 1), # set to 0 to enable logging
|
||||
nil),
|
||||
}),
|
||||
nil) })
|
||||
|
||||
############################################################
|
||||
|
||||
if ARGV.size == 0
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
|
|
@ -0,0 +1,141 @@
|
|||
#!/usr/bin/ruby
|
||||
|
||||
# This is the main entry point for running all SEMPRE programs. See
|
||||
# fig/lib/execrunner.rb for more documentation for how commands are generated.
|
||||
# There are a bunch of modes that this script can be invoked with, which
|
||||
# loosely correspond to the modules.
|
||||
|
||||
$: << 'fig/lib'
|
||||
require 'execrunner'
|
||||
|
||||
$modes = []
|
||||
def addMode(name, description, func)
|
||||
$modes << [name, description, func]
|
||||
end
|
||||
|
||||
def codalab(dependencies=nil)
|
||||
# Set @cl=1 to run job on CodaLab
|
||||
dependencies ||= l(':fig', ':lib', ':module-classes.txt', ':libsempre')
|
||||
l(
|
||||
letDefault(:cl, 0),
|
||||
sel(:cl,
|
||||
l(),
|
||||
l('cl', 'run', dependencies, '---', 'LC_ALL=C.UTF-8'),
|
||||
nil),
|
||||
nil)
|
||||
end
|
||||
|
||||
def header(modules='core', codalabDependencies=nil)
|
||||
l(
|
||||
codalab(codalabDependencies),
|
||||
# Queuing system
|
||||
letDefault(:q, 0), sel(:q, l(), l('fig/bin/q', '-shareWorkingPath', o('mem', '5g'), o('memGrace', 10), '-add', '---')),
|
||||
# Create execution directory
|
||||
letDefault(:pooldir, 1),
|
||||
sel(:pooldir, l(), 'fig/bin/qcreate'),
|
||||
# Run the Java command...
|
||||
'java',
|
||||
'-ea',
|
||||
'-Dmodules='+modules,
|
||||
# Memory size
|
||||
letDefault(:memsize, 'default'),
|
||||
sel(:memsize, {
|
||||
'tiny' => l('-Xms2G', '-Xmx4G'),
|
||||
'low' => l('-Xms5G', '-Xmx7G'),
|
||||
'default' => l('-Xms8G', '-Xmx10G'),
|
||||
'medium' => l('-Xms12G', '-Xmx14G'),
|
||||
'high' => l('-Xms20G', '-Xmx24G'),
|
||||
'higher' => l('-Xms40G', '-Xmx50G'),
|
||||
'impressive' => l('-Xms75G', '-Xmx90G'),
|
||||
}),
|
||||
# Classpath
|
||||
'-cp', 'libsempre/*:lib/*',
|
||||
# Profiling
|
||||
letDefault(:prof, 0), sel(:prof, l(), '-Xrunhprof:cpu=samples,depth=100,file=_OUTPATH_/java.hprof.txt'),
|
||||
# debugging
|
||||
letDefault(:debug, 0), sel(:debug, l(), l('-Xdebug', '-Xrunjdwp:server=y,transport=dt_socket,suspend=y,address=8898')),
|
||||
nil)
|
||||
end
|
||||
|
||||
def rlwrap; system('which rlwrap') ? 'rlwrap' : nil end
|
||||
|
||||
def unbalancedTrainDevSplit
|
||||
l(o('Dataset.trainFrac', 0.8), o('Dataset.devFrac', 0.2))
|
||||
end
|
||||
def balancedTrainDevSplit
|
||||
l(o('Dataset.trainFrac', 0.5), o('Dataset.devFrac', 0.5))
|
||||
end
|
||||
|
||||
def figOpts; l(selo(:pooldir, 'execDir', 'exec', '_OUTPATH_'), o('overwriteExecDir'), o('addToView', 0)) end
|
||||
|
||||
############################################################
|
||||
# Unit tests
|
||||
# Community Server
|
||||
addMode('backup-cp', 'small commands like run community server, backup, or simulator', lambda { |e| l(
|
||||
lambda { |e| system 'echo "backing up with cp"'},
|
||||
lambda { |e| system 'mkdir -p ./int-backup/'},
|
||||
lambda { |e| system 'cp int-output int-backup/`date +%Y-%m-%d.%H:%M:%S`'},
|
||||
lambda { |e| system 'mkdir -p ./int-output'},
|
||||
nil)})
|
||||
|
||||
addMode('backup-mv', 'small commands like run community server, backup, or simulator', lambda { |e| l(
|
||||
lambda { |e| system 'echo "backing up with mv"'},
|
||||
lambda { |e| system 'mkdir -p ./int-backup/'},
|
||||
lambda { |e| system 'mv int-output int-backup/`date +%Y-%m-%d.%H:%M:%S`'},
|
||||
lambda { |e| system 'mkdir -p ./int-output'},
|
||||
nil)})
|
||||
|
||||
addMode('trash', 'put int-output into trash with time stamp', lambda { |e| l(
|
||||
lambda { |e| system 'echo "trashing int-output with time stamp"'},
|
||||
lambda { |e| system 'rm int-output-trash-*'},
|
||||
lambda { |e| system 'mkdir -p ./int-output'},
|
||||
nil)})
|
||||
|
||||
addMode('community', 'start the community server', lambda { |e| l(
|
||||
l('python community-server/server.py -- port 8403'),
|
||||
nil)})
|
||||
|
||||
addMode('simulator', 'run the simulator', lambda { |e| l(
|
||||
rlwrap,
|
||||
header('core,interactive'),
|
||||
'edu.stanford.nlp.sempre.interactive.Simulator',
|
||||
figOpts,
|
||||
letDefault(:server, 'local'),
|
||||
sel(:server, {
|
||||
'local' => o('serverURL', 'http://localhost:8410'),
|
||||
'remote' => o('serverURL', 'http://jonsson.stanford.edu:8410')
|
||||
}),
|
||||
# set to 0 to enable logging
|
||||
o('numThreads', 4),
|
||||
letDefault(:sandbox, 'full'),
|
||||
sel(:sandbox, {
|
||||
'all' => o('reqParams', 'grammar=0\&cite=0\&learn=0\&logging=0'),
|
||||
'nolog' => o('reqParams', 'grammar=0\&cite=0\&learn=1\&logging=0'),
|
||||
'nolearn' => o('reqParams', 'grammar=1\&cite=1\&learn=0\&logging=0'),
|
||||
'none' => o('reqParams', 'grammar=1\&cite=1\&learn=1\&logging=0')
|
||||
}),
|
||||
letDefault(:task, 'sidaw'),
|
||||
sel(:task, {
|
||||
'qual1' => o('logFiles', './shrdlurn/commandInputs/qualifier20-0118.json'),
|
||||
'free1' => o('logFiles', './shrdlurn/commandInputs/freebuild1-0121.json'),
|
||||
'sidaw' => o('logFiles', './shrdlurn/commandInputs/sidaw.json.log')
|
||||
}),
|
||||
nil)})
|
||||
|
||||
############################################################
|
||||
|
||||
if ARGV.size == 0
|
||||
puts "#{$0} @mode=<mode> [options]"
|
||||
puts
|
||||
puts 'This is the main entry point for all SHRDLRUN related runs.'
|
||||
puts "Modes:"
|
||||
$modes.each { |name,description,func|
|
||||
puts " #{name}: #{description}"
|
||||
}
|
||||
end
|
||||
|
||||
modesMap = {}
|
||||
$modes.each { |name,description,func|
|
||||
modesMap[name] = func
|
||||
}
|
||||
run!(sel(:mode, modesMap))
|
||||
|
|
@ -300,7 +300,9 @@ class BeamFloatingParserState extends ChartParserState {
|
|||
// Base case: our fencepost has walked to the end of the span, so
|
||||
// apply the rule on all the children gathered during the walk.
|
||||
if (i == end) {
|
||||
for (Rule rule : node.rules) {
|
||||
Iterator<Rule> ruleIterator = node.rules.iterator();
|
||||
while (ruleIterator.hasNext()) {
|
||||
Rule rule = ruleIterator.next();
|
||||
if (coarseAllows(rule.lhs, start, end)) {
|
||||
numNew.value += applyRule(start, end, rule, children);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -127,7 +127,7 @@ public final class ILUtils {
|
|||
allDerivs.add(stripDerivation(d));
|
||||
}
|
||||
}
|
||||
if (!found && !formula.equals("?")) LogInfo.errors("matching formula not found: %s", formula);
|
||||
if (!found && !formula.equals("?")) LogInfo.logs("Error: matching formula not found: %s", formula);
|
||||
|
||||
// just making testing easier, use top derivation when we formula is not given
|
||||
if (!found && ex.predDerivations.size() > 0 && (formula.equals("?") || formula==null || opts.useBestFormula) )
|
||||
|
|
@ -182,14 +182,19 @@ public final class ILUtils {
|
|||
b.setUtterance(head);
|
||||
Example exHead = b.createExample();
|
||||
exHead.preprocess();
|
||||
|
||||
|
||||
LogInfo.begin_track("Definition");
|
||||
LogInfo.logs("mode: %s", blockmode);
|
||||
LogInfo.logs("headraw: %s", head);
|
||||
LogInfo.logs("head: %s", exHead.getTokens());
|
||||
List<String> bodyList = ILUtils.utterancefromJson(jsonDef);
|
||||
LogInfo.logs("body: %s", bodyList);
|
||||
LogInfo.logs("defderiv: %s", bodyDeriv.toLispTree());
|
||||
LogInfo.logs("bodyformula: %s", bodyDeriv.formula.toLispTree());
|
||||
|
||||
if (exHead.getTokens()==null || exHead.getTokens().size() == 0)
|
||||
throw new RuntimeException(String.format("Cannot define with an empty head: %s", head));
|
||||
|
||||
BeamFloatingParserState state = (BeamFloatingParserState)parser.parse(params, exHead, true);
|
||||
LogInfo.logs("anchored: %s", state.chartList);
|
||||
|
|
|
|||
|
|
@ -48,9 +48,6 @@ public class JsonServer {
|
|||
@Option public String fullResponseLogPath;
|
||||
}
|
||||
public static Options opts = new Options();
|
||||
|
||||
private static Object queryLogLock = new Object();
|
||||
private static Object responseLogLock = new Object();
|
||||
|
||||
Master master;
|
||||
|
||||
|
|
@ -73,7 +70,8 @@ public class JsonServer {
|
|||
// For header
|
||||
HttpCookie cookie;
|
||||
boolean isNewSession;
|
||||
|
||||
private Object queryLogLock = new Object();
|
||||
private Object responseLogLock = new Object();
|
||||
// For writing main content
|
||||
|
||||
public ExchangeState(HttpExchange exchange) throws IOException {
|
||||
|
|
@ -90,7 +88,7 @@ public class JsonServer {
|
|||
try {
|
||||
String key = URLDecoder.decode(kv[0], "UTF-8");
|
||||
String value = URLDecoder.decode(kv[1], "UTF-8");
|
||||
logs("%s => %s", key, value);
|
||||
// logs("%s => %s", key, value);
|
||||
reqParams.put(key, value);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new RuntimeException(e);
|
||||
|
|
@ -104,7 +102,7 @@ public class JsonServer {
|
|||
} else {
|
||||
isNewSession = true;
|
||||
}
|
||||
|
||||
|
||||
if (opts.verbose >= 2)
|
||||
LogInfo.logs("GET %s from %s (%ssessionId=%s)", uri, remoteHost, isNewSession ? "new " : "", sessionId);
|
||||
|
||||
|
|
@ -137,7 +135,7 @@ public class JsonServer {
|
|||
exchange.sendResponseHeaders(200, 0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Map<String, Object> makeJson(Master.Response response) {
|
||||
Map<String, Object> json = new HashMap<String, Object>();
|
||||
|
||||
|
|
@ -148,7 +146,7 @@ public class JsonServer {
|
|||
List<Object> items = new ArrayList<Object>();
|
||||
json.put("candidates", items);
|
||||
List<Derivation> allCandidates = response.getExample().getPredDerivations();
|
||||
|
||||
|
||||
if (allCandidates != null) {
|
||||
if (allCandidates.size() >= JsonServer.opts.maxCandidates) {
|
||||
allCandidates = allCandidates.subList(0, JsonServer.opts.maxCandidates);
|
||||
|
|
@ -157,7 +155,7 @@ public class JsonServer {
|
|||
allCandidates.size(), JsonServer.opts.maxCandidates)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
for (Derivation deriv : allCandidates) {
|
||||
Map<String, Object> item = new HashMap<String, Object>();
|
||||
Value value = deriv.getValue();
|
||||
|
|
@ -192,48 +190,40 @@ public class JsonServer {
|
|||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void handleQuery(String sessionId) throws IOException {
|
||||
String query = reqParams.get("q");
|
||||
boolean sandbox = false; // false when simulating
|
||||
if (reqParams.containsKey("sandbox") && !reqParams.get("sandbox").equals("0"))
|
||||
sandbox = true;
|
||||
|
||||
String queryTime = LocalDateTime.now().toString();
|
||||
{ // write the query log
|
||||
Map<String, Object> jsonMap = new LinkedHashMap<>();
|
||||
jsonMap.put("time", queryTime);
|
||||
jsonMap.put("sessionId", sessionId);
|
||||
jsonMap.put("q", query);
|
||||
// jsonMap.putAll(reqParams);
|
||||
jsonMap.put("remote", remoteHost);
|
||||
|
||||
|
||||
|
||||
synchronized (queryLogLock) {
|
||||
// boolean isContext = query.startsWith("(:context ");
|
||||
// if (!sandbox && (!isContext || opts.verbose >= 2)) {
|
||||
if (!sandbox) {
|
||||
PrintWriter out = IOUtils.openOutAppend(opts.queryLogPath);
|
||||
out.println(Json.writeValueAsStringHard(jsonMap));
|
||||
out.close();
|
||||
} else {
|
||||
LogInfo.log(Json.writeValueAsStringHard(jsonMap));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If JSON, don't store cookies.
|
||||
|
||||
Session session = master.getSession(sessionId);
|
||||
session.reqParams = reqParams;
|
||||
session.remoteHost = remoteHost;
|
||||
session.format = "json";
|
||||
session.sandbox = sandbox;
|
||||
|
||||
|
||||
LocalDateTime queryTime = LocalDateTime.now();
|
||||
synchronized (queryLogLock) { // write the query log
|
||||
Map<String, Object> jsonMap = new LinkedHashMap<>();
|
||||
jsonMap.put("q", query);
|
||||
jsonMap.put("remote", remoteHost);
|
||||
jsonMap.put("time", queryTime.toString());
|
||||
jsonMap.put("sessionId", sessionId);
|
||||
reqParams.remove("q");
|
||||
jsonMap.putAll(reqParams);
|
||||
if (session.isLogging()) {
|
||||
logLine(opts.queryLogPath, Json.writeValueAsStringHard(jsonMap));
|
||||
} else {
|
||||
logLine(opts.queryLogPath + ".sandbox", Json.writeValueAsStringHard(jsonMap));
|
||||
}
|
||||
}
|
||||
|
||||
// If JSON, don't store cookies.
|
||||
|
||||
if (query == null) query = "null";
|
||||
logs("Server.handleQuery %s: %s", session.id, query);
|
||||
|
||||
// Print header
|
||||
setHeaders("application/json");
|
||||
|
||||
|
||||
Master.Response masterResponse = null;
|
||||
if (query != null)
|
||||
masterResponse = processQuery(session, query);
|
||||
|
|
@ -249,34 +239,44 @@ public class JsonServer {
|
|||
}
|
||||
out.close();
|
||||
}
|
||||
|
||||
{ // write the response log log
|
||||
|
||||
synchronized (responseLogLock) { // write the response log log
|
||||
Map<String, Object> jsonMap = new LinkedHashMap<>();
|
||||
jsonMap.put("response_time", LocalDateTime.now().toString());
|
||||
jsonMap.put("query_time", queryTime);
|
||||
LocalDateTime responseTime = LocalDateTime.now();
|
||||
// jsonMap.put("responseTime", responseTime.toString());
|
||||
jsonMap.put("time", queryTime);
|
||||
jsonMap.put("ms", String.format("%.3f", java.time.Duration.between(queryTime, responseTime).toNanos()/1000.0));
|
||||
jsonMap.put("sessionId", sessionId);
|
||||
jsonMap.put("q", query); // backwards compatability...
|
||||
jsonMap.put("lines", responseMap.get("lines"));
|
||||
synchronized (responseLogLock) {
|
||||
if (!sandbox) {
|
||||
{
|
||||
PrintWriter out = IOUtils.openOutAppend(opts.responseLogPath);
|
||||
out.println(Json.writeValueAsStringHard(jsonMap));
|
||||
out.close();
|
||||
}
|
||||
|
||||
if (!Strings.isNullOrEmpty(opts.fullResponseLogPath)) {
|
||||
jsonMap.put("candidates", responseMap.get("candidates"));
|
||||
PrintWriter out = IOUtils.openOutAppend(opts.fullResponseLogPath);
|
||||
out.println(Json.writeValueAsStringHard(jsonMap));
|
||||
out.close();
|
||||
}
|
||||
} else {
|
||||
LogInfo.log(Json.writeValueAsStringHard(jsonMap));
|
||||
if (session.isLogging()) {
|
||||
logLine(opts.responseLogPath, Json.writeValueAsStringHard(jsonMap));
|
||||
if (!Strings.isNullOrEmpty(opts.fullResponseLogPath)) {
|
||||
jsonMap.put("candidates", responseMap.get("candidates"));
|
||||
logLine(opts.fullResponseLogPath, Json.writeValueAsStringHard(jsonMap));
|
||||
}
|
||||
} else {
|
||||
logLine(opts.responseLogPath + ".sandbox", Json.writeValueAsStringHard(jsonMap));
|
||||
if (!Strings.isNullOrEmpty(opts.fullResponseLogPath)) {
|
||||
jsonMap.put("candidates", responseMap.get("candidates"));
|
||||
logLine(opts.fullResponseLogPath + ".sandbox", Json.writeValueAsStringHard(jsonMap));
|
||||
}
|
||||
// LogInfo.log(Json.writeValueAsStringHard(jsonMap));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void logLine(String path, String line) {
|
||||
PrintWriter out;
|
||||
try {
|
||||
out = IOUtils.openOutAppend(path);
|
||||
out.println(line);
|
||||
out.close();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
void getFile(String path) throws IOException {
|
||||
if (!new File(path).exists()) {
|
||||
|
|
@ -302,7 +302,7 @@ public class JsonServer {
|
|||
try {
|
||||
String hostname = fig.basic.SysInfoUtils.getHostName();
|
||||
HttpServer server = HttpServer.create(new InetSocketAddress(opts.port), 10);
|
||||
|
||||
|
||||
ExecutorService pool = new ThreadPoolExecutor(opts.numThreads, opts.numThreads,
|
||||
5000, TimeUnit.MILLISECONDS,
|
||||
new LinkedBlockingQueue<Runnable>());
|
||||
|
|
|
|||
|
|
@ -1,5 +1,12 @@
|
|||
package edu.stanford.nlp.sempre;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.PrintStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import fig.basic.LogInfo;
|
||||
import fig.basic.Option;
|
||||
import fig.exec.Execution;
|
||||
|
|
|
|||
|
|
@ -454,9 +454,9 @@ public class Master {
|
|||
throw new RuntimeException(String.format("refused to execute: too many characters in one command (current: %d, max: %d)",
|
||||
utt.length(), ILUtils.opts.maxChars));
|
||||
|
||||
builder.parser.parse(builder.params, ex, false);
|
||||
|
||||
builder.parser.parse(builder.params, ex, false);
|
||||
response.ex = ex;
|
||||
|
||||
} else if (command.equals(":accept")) {
|
||||
String utt = tree.children.get(1).value;
|
||||
String formula = tree.children.get(2).value;
|
||||
|
|
@ -484,17 +484,19 @@ public class Master {
|
|||
if (match != null) {
|
||||
LogInfo.logs("Matched: %s", match);
|
||||
|
||||
if (!session.sandbox) {
|
||||
if (session.isWritingCitation()) {
|
||||
CitationTracker tracker = new CitationTracker(session.id, ex);
|
||||
tracker.citeAll(match);
|
||||
}
|
||||
|
||||
ex.setTargetValue(match.value); // this is just for logging, not actually used for learning
|
||||
LogInfo.begin_track("Updating parameters");
|
||||
learner.onlineLearnExample(ex);
|
||||
LogInfo.end_track();
|
||||
if (session.isLearning()) {
|
||||
LogInfo.begin_track("Updating parameters");
|
||||
learner.onlineLearnExample(ex);
|
||||
LogInfo.end_track();
|
||||
}
|
||||
}
|
||||
} else if (command.equals(":def") || command.equals(":def_ret")) {
|
||||
} else if (command.startsWith(":def")) {
|
||||
if (tree.children.size() == 3) {
|
||||
String head = tree.children.get(1).value;
|
||||
String jsonDef = tree.children.get(2).value;
|
||||
|
|
@ -504,18 +506,20 @@ public class Master {
|
|||
builder.parser, builder.params, session.id, refExHead);
|
||||
|
||||
if (inducedRules.size() > 0) {
|
||||
for (Rule rule : inducedRules) {
|
||||
ILUtils.addRuleInteractive(rule, builder.parser);
|
||||
if (session.isLearning()) {
|
||||
for (Rule rule : inducedRules) {
|
||||
ILUtils.addRuleInteractive(rule, builder.parser);
|
||||
}
|
||||
}
|
||||
// TODO : should not have to parse again, I guess just set the formula or something
|
||||
// builder.parser.parse(builder.params, refExHead.value, false);
|
||||
response.ex = refExHead.value;
|
||||
|
||||
// write out the grammar
|
||||
if (!session.sandbox) {
|
||||
PrintWriter out = IOUtils.openOutAppendHard(Paths.get(Master.opts.newGrammarPath, session.id + ".grammar").toString());
|
||||
if (session.isWritingGrammar()) {
|
||||
PrintWriter out = IOUtils.openOutAppendHard(Paths.get(Master.opts.newGrammarPath).toString());
|
||||
for (Rule rule : inducedRules) {
|
||||
out.println(rule.toLispTree().toStringWrap());
|
||||
out.println(rule.toJson());
|
||||
}
|
||||
out.close();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -167,10 +167,10 @@ public abstract class Parser {
|
|||
// Parse
|
||||
StopWatch watch = new StopWatch();
|
||||
watch.start();
|
||||
LogInfo.begin_track_printAll("Parser.parse: parse");
|
||||
// LogInfo.begin_track_printAll("Parser.parse: parse");
|
||||
ParserState state = newParserState(params, ex, computeExpectedCounts);
|
||||
state.infer();
|
||||
LogInfo.end_track();
|
||||
// LogInfo.end_track();
|
||||
watch.stop();
|
||||
state.parseTime = watch.getCurrTimeLong();
|
||||
state.setEvaluation();
|
||||
|
|
|
|||
|
|
@ -5,7 +5,9 @@ import com.google.common.collect.Lists;
|
|||
import fig.basic.LispTree;
|
||||
import fig.basic.Pair;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* A rule specifies how to take a right hand of terminals and non-terminals.
|
||||
|
|
@ -145,6 +147,16 @@ public class Rule {
|
|||
public int hashCode() {
|
||||
return this.toString().hashCode();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String toJson() {
|
||||
Map<String, Object> jsonMap = new LinkedHashMap<>();
|
||||
jsonMap.put("lhs", lhs);
|
||||
jsonMap.put("rhs", rhs);
|
||||
jsonMap.put("sem", sem.toString());
|
||||
if (info != null) {
|
||||
for (Pair<String, Double> p : info)
|
||||
jsonMap.put(p.getFirst(), p.getSecond());
|
||||
}
|
||||
return Json.writeValueAsStringHard(jsonMap);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ public class Session {
|
|||
Example lastEx; // Last example that we processed
|
||||
Params params;
|
||||
Learner learner;
|
||||
boolean sandbox = false;
|
||||
Map<String,String> reqParams;
|
||||
|
||||
public static Options opts = new Options();
|
||||
|
||||
|
|
@ -81,9 +81,40 @@ public class Session {
|
|||
this.params.read(opts.inParamsPath);
|
||||
this.learner = new Learner(builder.parser, this.params, new Dataset());
|
||||
}
|
||||
|
||||
|
||||
//Decides if we write out any logs
|
||||
//public boolean isWriting() {
|
||||
// return false;
|
||||
//}
|
||||
// provides highest level of isolation,
|
||||
// should not even mutate the server
|
||||
public boolean isSandbox() {
|
||||
return true;
|
||||
}
|
||||
// determines whether we add rules used by the public
|
||||
public boolean isUpdating() {
|
||||
return true;
|
||||
}
|
||||
public boolean isGlobal() {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s: %s; last: %s", id, context, lastEx);
|
||||
}
|
||||
|
||||
// Decides if we write out any logs
|
||||
public boolean isLogging() { return defaultTrue("logging");}
|
||||
public boolean isWritingCitation() { return defaultTrue("cite");}
|
||||
public boolean isWritingGrammar() { return defaultTrue("grammar");}
|
||||
public boolean isLearning() { return defaultTrue("learn");}
|
||||
|
||||
private boolean defaultTrue(String key) {
|
||||
if (this.reqParams == null) return true;
|
||||
if (!this.reqParams.containsKey(key)) return true;
|
||||
return !this.reqParams.get(key).equals("0");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,14 +25,7 @@ enum CubeColor {
|
|||
CubeColor(int value) { this.value = value; }
|
||||
public int toInt() { return this.value; }
|
||||
public boolean Compare(int i){return value == i;}
|
||||
public static CubeColor fromInt(int intc) {
|
||||
for(CubeColor c : CubeColor.values())
|
||||
{
|
||||
if (intc < 0) return CubeColor.None;
|
||||
if (c.value == intc % (CubeColor.values().length-1)) return c;
|
||||
}
|
||||
return CubeColor.None;
|
||||
}
|
||||
|
||||
public static CubeColor fromString(String color) {
|
||||
for(CubeColor c : CubeColor.values())
|
||||
if (c.name().equalsIgnoreCase(color)) return c;
|
||||
|
|
|
|||
|
|
@ -60,16 +60,21 @@ public class CitationTracker {
|
|||
|
||||
Map<String, Object> summary;
|
||||
try {
|
||||
summary = Json.readMapHard(IOUtils.readLine(summaryPath));
|
||||
String line = IOUtils.readLineEasy(summaryPath);
|
||||
if (line == null)
|
||||
summary = defaultMap(rule);
|
||||
else
|
||||
summary = Json.readMapHard(line);
|
||||
|
||||
boolean selfcite = authorCode.equals(uid);
|
||||
if (!selfcite) {
|
||||
summary.put("cite", (Integer)summary.get("cite") + 1);
|
||||
} else {
|
||||
summary.put("self", (Integer)summary.get("self") + 1);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// e.printStackTrace();
|
||||
} catch (Exception e) {
|
||||
summary = defaultMap(rule);
|
||||
e.printStackTrace();
|
||||
}
|
||||
String jsonStr = Json.writeValueAsStringHard(summary);
|
||||
PrintWriter out = IOUtils.openOutHard(file);
|
||||
|
|
|
|||
|
|
@ -13,15 +13,22 @@ import java.nio.file.Files;
|
|||
import java.nio.file.Paths;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.testng.collections.Lists;
|
||||
|
||||
import edu.stanford.nlp.sempre.Json;
|
||||
import edu.stanford.nlp.sempre.JsonServer;
|
||||
import fig.basic.LogInfo;
|
||||
import fig.basic.Option;
|
||||
import fig.basic.OptionsParser;
|
||||
import fig.exec.Execution;
|
||||
import fig.basic.Evaluation;
|
||||
|
||||
/**
|
||||
* utilites for simulating a session through the server
|
||||
|
|
@ -32,20 +39,24 @@ public class Simulator implements Runnable {
|
|||
@Option public static String serverURL = "http://localhost:8410";
|
||||
@Option public static int numThreads = 4;
|
||||
@Option public static int verbose = 1;
|
||||
@Option(gloss = "0: do not write any real logs")
|
||||
public static int logToFile = 0;
|
||||
@Option public static String reqParams = "grammar=0&cite=0&learn=0";
|
||||
@Option public static List<String> logFiles = Lists.newArrayList("./shrdlurn/commandInputs/sidaw.json.log");
|
||||
|
||||
public void readQueries() {
|
||||
LogInfo.begin_track("setsTest");
|
||||
//T.printAllRules();
|
||||
//A.assertAll();
|
||||
ExecutorService executor = new ThreadPoolExecutor(JsonServer.opts.numThreads, JsonServer.opts.numThreads,
|
||||
5000, TimeUnit.MILLISECONDS,
|
||||
new LinkedBlockingQueue<Runnable>());
|
||||
|
||||
long startTime = System.nanoTime();
|
||||
for (String fileName : logFiles) {
|
||||
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
|
||||
LogInfo.logs("Reading %s", fileName);
|
||||
|
||||
stream.forEach(l -> {
|
||||
stream.forEach(l ->
|
||||
executor.execute(() -> {
|
||||
Map<String, Object> json = Json.readMapHard(l);
|
||||
Object command = json.get("q");
|
||||
if (command == null) // to be backwards compatible
|
||||
|
|
@ -60,27 +71,35 @@ public class Simulator implements Runnable {
|
|||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
}));
|
||||
executor.shutdown();
|
||||
try {
|
||||
boolean finshed = executor.awaitTermination(5, TimeUnit.MINUTES);
|
||||
} catch (InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
long endTime = System.nanoTime();
|
||||
LogInfo.logs("Simulator.TimeTaken = %d ns or %.4f s", (endTime - startTime), (endTime - startTime)/1.0e9);
|
||||
}
|
||||
|
||||
long endTime = System.nanoTime();
|
||||
LogInfo.logs("Took %d ns or %.4f s", (endTime - startTime), (endTime - startTime)/1.0e9);
|
||||
}
|
||||
LogInfo.end_track();
|
||||
}
|
||||
|
||||
public static void sempreQuery(String query, String sessionId) throws UnsupportedEncodingException {
|
||||
public static String sempreQuery(String query, String sessionId) throws UnsupportedEncodingException {
|
||||
String params = "q=" + URLEncoder.encode(query, "UTF-8");
|
||||
params += String.format("&sessionId=%s&logtofile=%d", sessionId, logToFile);
|
||||
params += String.format("&sessionId=%s&%s", sessionId, reqParams);
|
||||
// params = URLEncoder.encode(params);
|
||||
String url = String.format("%s/sempre?", serverURL);
|
||||
// LogInfo.log(params);
|
||||
LogInfo.log(query);
|
||||
String response = executePost(url + params, "");
|
||||
LogInfo.log(response);
|
||||
return response;
|
||||
}
|
||||
|
||||
public static String executePost(String targetURL, String urlParameters) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue