Skip to content
This repository has been archived by the owner on Nov 9, 2021. It is now read-only.

Commit

Permalink
fix net app
Browse files Browse the repository at this point in the history
  • Loading branch information
erelsgl committed Dec 23, 2013
1 parent 4095019 commit d4f7cf1
Show file tree
Hide file tree
Showing 299 changed files with 30,667 additions and 71 deletions.
1,263 changes: 1,263 additions & 0 deletions ImitatorNet/client/clean.css

Large diffs are not rendered by default.

1,264 changes: 1,264 additions & 0 deletions ImitatorNet/client/clean_rtl.css

Large diffs are not rendered by default.

80 changes: 68 additions & 12 deletions ImitatorNet/client/index.html
Expand Up @@ -9,6 +9,8 @@
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link type="text/css" rel="stylesheet" href="ImitatorGWT.css">
<link type="text/css" rel="stylesheet" href="clean.css">

<title>הקובץ התשיעי</title>

<script src="js/socket.io/socket.io.js"></script>
Expand Down Expand Up @@ -57,10 +59,10 @@ <h1 dir='rtl' lang='he'>הקובץ התשיעי</h1>
</div>
<div>&nbsp;</div>
<div>
<span id='explainButton' dir='rtl' lang='he'> </span>
<button id='explainButton' dir='rtl' lang='he' onclick='$("#explanations").toggle()'>הראה הסברים</button>
</div>

<div id='explanations' dir='rtl' lang='he'>
<div id='explanations' style='display:none' dir='rtl' lang='he'>
<ol>
<li id='explanation0'></li>
<li id='explanation1'></li>
Expand Down Expand Up @@ -93,15 +95,26 @@ <h1 dir='rtl' lang='he'>הקובץ התשיעי</h1>
return socket;
}



/**
* @see http://stackoverflow.com/a/647272/827927
*/
function getQueryString() {
var result = {}, queryString = location.search.substring(1),
re = /([^&=]+)=([^&]*)/g, m;
while (m = re.exec(queryString)) {
result[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
}
return result;
}

/**
* @param form
* @return an object whose fields are the fields of the given form.
* Also converts 'any' selected-options to nulls, and handles arrays (fields whose name ends with []).
*/
function formToObject(form) {
var request = {};
//console.log(form.serializeArray());
$.each(form.serializeArray(), function(i, field) {
if (field.name.endsWith("[]")) { // field is an array:
field.name = field.name.substr(0,field.name.length-2);
Expand All @@ -120,33 +133,76 @@ <h1 dir='rtl' lang='he'>הקובץ התשיעי</h1>
/* initialization */

var socket=null, form=null, deeplog=null;
var replies = [];
$(document).ready(function() {
if (getQueryString()["explain"])
$("#explainButton").show();
else
$("#explainButton").hide();

$("#sendButton").text("צור חידה חדשה");
$("#sendButton").prop('disabled', false);

//deeplog = new DeepLog($("#deeplog"));
form = $('#form');
socket = commonConnect(serverport, deeplog);

socket.on('puzzle', function(record) {
console.log("CLIENT: got a puzzle!");
console.log(JSON.stringify(record));

$("#sendButton").text("צור חידה חדשה");
});
socket.on('puzzle', onPuzzle)
});



/* puzzles */

function createPuzzle() {
var request = formToObject(form);
console.log("CLIENT: got a request to create a puzzle: "+JSON.stringify(request));

$("#sendButton").text("יוצר חידה...");
$("#sendButton").prop('disabled', true);

socket.emit('createPuzzle', request);
}

function onPuzzle(result) {
console.log("CLIENT: got a puzzle!");

for (var iChoice=0; iChoice<result.length; ++iChoice) {
var choice = $("#choice"+iChoice);
var explanation = $("#explanation"+iChoice);
if (choice) {
//console.log(JSON.stringify(result[iChoice]));
choice.html(result[iChoice].choice);
choice.attr("class", "");
//replies[iChoice] = result[iChoice].reply;
choice.attr("reply", result[iChoice].reply);
choice.click(function() {
var reply = this.attr("reply");
markChoiceAsCorrectOrWrong(this, /נכון/.test(reply));
alert(iChoice+": "+reply);
//choicesRegistrations[iChoiceFinal].removeHandler();
//choicesRegistrations[iChoiceFinal] = null;
});
}
if (explanation)
explanation.html(result[iChoice].explanation.replace(/[.] /, ".<br/>"));
}

$("#puzzles").show();

$("#sendButton").text("צור חידה חדשה");
$("#sendButton").prop('disabled', false);
}

function markChoiceAsCorrectOrWrong(choiceLabel, isCorrect) {
if (isCorrect) {
//setNumCorrect(numCorrect+1);
choiceLabel.attr("class", "correct");
} else {
//setNumWrong(numWrong+1);
choiceLabel.attr("class", "wrong");
}
}


</script>

</body>
Expand Down
4 changes: 3 additions & 1 deletion ImitatorNet/src/main/java/imitatornet/ImitatorServer.java
Expand Up @@ -38,11 +38,13 @@ static class PuzzleRequest {
public static void main(String[] args) throws Exception {
CommonBeginner.begin(args, "Imitator");

final PuzzleCreator puzzleCreator = new PuzzleCreator(new File("../ImitatorProject/corpora").getAbsolutePath());
final PuzzleCreator puzzleCreator = new PuzzleCreator(new File("../corpora").getAbsolutePath());
final CommonSocketIOServer networkServer = new CommonSocketIOServer(args);
final ChoiceAndExplanation[] defaultPuzzle = puzzleCreator.createPuzzle("ShmonaKvazim", 4);

networkServer.addEventListener("createPuzzle", PuzzleRequest.class, new CommonDataListener<PuzzleRequest>(logger) {
@Override public void onDataSub(SocketIOClient client, PuzzleRequest request, AckRequest ackRequest) throws Throwable {
client.sendEvent("puzzle", defaultPuzzle);
ChoiceAndExplanation[] puzzle = puzzleCreator.createPuzzle(request.corpus, request.numChoices);
System.out.println(Arrays.asList(puzzle));
client.sendEvent("puzzle", puzzle);
Expand Down
22 changes: 12 additions & 10 deletions ImitatorProject/src/Imitator/mainTasks/MeasureFrequencies.java
Expand Up @@ -20,21 +20,21 @@ public class MeasureFrequencies {

public static Timer _timer = new Timer();

public static void run(Properties properties) {
public static void run(File propertiesFolder, Properties properties) {
_corpusName = properties.getProperty("corpus.name");
String inputDirName = properties.getProperty("input.directory"); // parsed corpus
File inputDir = new File(inputDirName);
File inputDir = new File(propertiesFolder, inputDirName);
if (!inputDir.isDirectory()) {
System.err.println(" ## the corpus path "+inputDirName+" is not a valid directory !!");
return;
}

String outputDirName = properties.getProperty("output.directory"); // parsed corpus measures
File outputDir = new File(outputDirName);
File outputDir = new File(propertiesFolder, outputDirName);
if (!outputDir.exists())
outputDir.mkdirs();

initProperties(properties, outputDirName);
initProperties(properties, outputDir.getAbsolutePath());

/***************************************/
/**** Measure frequencies of corpus ****/
Expand Down Expand Up @@ -208,14 +208,16 @@ private static boolean is10s(File file)
}

public static void main(String[] args) throws FileNotFoundException, IOException {
String props = "corpora/Rambam/MeasureFrequencies.properties";
// String props = "corpora/ShmonaKvazim/MeasureFrequencies.properties";
// String props = "corpora/Herzl/MeasureFrequencies.properties";
// String propertiesFileName = "../corpora/Rambam/MeasureFrequencies.properties";
// String propertiesFileName = "../corpora/ShmonaKvazim/MeasureFrequencies.properties";
String propertiesFileName = "../corpora/Herzl/MeasureFrequencies.properties";

File propertiesFile = new File(propertiesFileName);
File propertiesFolder = propertiesFile.getParentFile();

// Read properties file.
Properties properties = new Properties();
properties.clear();
properties.load(new FileInputStream(props));
run(properties);
properties.load(new FileInputStream(propertiesFile));
run(propertiesFolder, properties);
}
}
38 changes: 17 additions & 21 deletions ImitatorProject/src/Imitator/mainTasks/SentenceCreator.java
@@ -1,5 +1,6 @@
package Imitator.mainTasks;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
Expand Down Expand Up @@ -103,9 +104,11 @@ public static void initialize_static(Properties properties) {
initPrinter();
}

public void initialize(Properties properties) throws IOException {
public void initialize(File propertiesFolder, Properties properties) throws IOException {
initialize_static(properties);
setMainFrequenciesDir(properties.getProperty("main.frequencies.dir"));
this.corpusDir = new File(propertiesFolder,
properties.getProperty("main.frequencies.dir")).getAbsolutePath();

// If there use of the supplementary corpus - initialize its location
// TODO check these lines
boolean shouldUseSupplementaryCorpus = Boolean.parseBoolean(properties.getProperty("should.use.supplementary.corpus"));
Expand Down Expand Up @@ -140,10 +143,10 @@ public void initialize(Properties properties) throws IOException {
readStatistics();
}

public void run(Properties properties) throws IOException {
public void run(File propertiesFolder, Properties properties) throws IOException {
_timer.startClock();

initialize(properties);
initialize(propertiesFolder, properties);
//System.exit(1);

/*************************************/
Expand Down Expand Up @@ -197,15 +200,6 @@ private static void initPrinter() {
_printer = new HebrewSentencePrinter();
}

private void setMainFrequenciesDir(String frequenciesPath) {
int lastDirectoryIndex = frequenciesPath.lastIndexOf("\\"); // windows
if (lastDirectoryIndex<0)
lastDirectoryIndex = frequenciesPath.lastIndexOf("/"); // linux
this.corpusDir = frequenciesPath;
//CreatorProps.setMainCorpusName(corpusName);
//CreatorProps.setBaseDir(frequenciesPath);
}

private static String memory() {
final int unit = 1000000; // MB
long usedMemory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
Expand Down Expand Up @@ -656,15 +650,17 @@ public SentenceCreator() {}
public SentenceCreator(String propertiesFileName) throws FileNotFoundException, IOException {
Properties properties = new Properties();
properties.load(new FileInputStream(propertiesFileName));
initialize(properties);
File propertiesFile = new File(propertiesFileName);
File propertiesFolder = propertiesFile.getParentFile();
initialize(propertiesFolder, properties);
}


public static void main(String[] args) throws FileNotFoundException, IOException {
String propertiesFile = "corpora/Rambam/SentenceCreator.properties";
// String propertiesFile = "corpora/Herzl/SentenceCreator.properties";
// String propertiesFile = "corpora/ShmonaKvazim/SentenceCreator.properties";

new SentenceCreator(propertiesFile).createRandomSection(1, true);
}
public static void main(String[] args) throws FileNotFoundException, IOException {
String propertiesFileName = "../corpora/Rambam/SentenceCreator.properties";
// String propertiesFileName = "../corpora/Herzl/SentenceCreator.properties";
// String propertiesFileName = "../corpora/ShmonaKvazim/SentenceCreator.properties";
new SentenceCreator(propertiesFileName).createRandomSection(1, true);
}
}
2 changes: 1 addition & 1 deletion ImitatorProject/src/puzzles/PuzzleCreator.java
Expand Up @@ -225,7 +225,7 @@ public ChoiceAndExplanation[] createPuzzle(String corpus, int numChoices) {
*/

public static void main(String[] args) throws FileNotFoundException, IOException {
PuzzleCreator impl = new PuzzleCreator("corpora");
PuzzleCreator impl = new PuzzleCreator("../corpora");
long start = System.currentTimeMillis();

// TODO: Currently doesn't work because of the memcache
Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -17,6 +17,7 @@ This is a parody-generator that focuses on Hebrew texts. The parsed texts and th
**ImitatorNet** is a web-server for creating and displaying puzzles based on ImitatorProject. It depends on:
* ImitatorProject
* [netty-socketio](https://github.com/mrniko/netty-socketio).

The main program is ImitatorServer.

CREDITS:
Expand Down
@@ -1,10 +1,10 @@
corpus.name = "BNC"

# The directory of the tagged corpus
input.directory = corpora/BNC/parsedCorpus
# The directory of the parsed corpus
input.directory = parsedCorpus

# The directory where the statistics of the corpora will be saved
output.directory = corpora/BNC/parsedCorpusMeasuresNew
output.directory = parsedCorpusMeasures

# Properties for filtering data.
minimal.template.length=3
Expand Down
@@ -1,9 +1,9 @@
# The directory where the statistics of the corpuses were saved
main.frequencies.dir = corpora/BNC/parsedCorpusMeasures
main.frequencies.dir = parsedCorpusMeasures1312

# The backup corpus name.
# Will be in use only id the property 'should.use.supplementary.corpus' is true
supplementary.frequencies.dir = corpora/BNC/parsedCorpusMeasures
supplementary.frequencies.dir = parsedCorpusMeasures1312

# It's recommended to use the backup corpus only with strong enough computer
# (relatively to the size of the corpus)
Expand Down Expand Up @@ -53,4 +53,4 @@ date.pos = ORD
time.pos = ORD
language = LTR

output.file.path = ../../../../output/BNC/generatedBNC.txt
output.file.path = ../../../../output/BNC/generatedBNC.txt
@@ -1,11 +1,11 @@
# The corpus name
corpus.name = "Herzl"
corpus.name = Herzl

# The directory of the parsed corpus
input.directory = corpora/Herzl/parsedCorpus
input.directory = parsedCorpus

# The directory where the statistics of the corpora will be saved
output.directory = corpora/Herzl/parsedCorpusMeasures
output.directory = parsedCorpusMeasures

# Properties for filtering data.
minimal.template.length=3
Expand Down
@@ -1,10 +1,9 @@
# The directory where the statistics of the corpuses were saved
main.frequencies.dir = corpora/Herzl/parsedCorpusMeasures1312

main.frequencies.dir = parsedCorpusMeasures1312

# The backup corpus name.
# Will be in use only id the property 'should.use.supplementary.corpus' is true
supplementary.frequencies.dir =corpora/Herzl/parsedCorpusMeasures1312
supplementary.frequencies.dir = parsedCorpusMeasures1312

# It's recommended to use the backup corpus only with strong enough computer
# (relatively to the size of the corpus)
Expand Down

0 comments on commit d4f7cf1

Please sign in to comment.