use of com.joliciel.jochre.graphics.Shape in project jochre by urieli.
the class SplitController method onClick$btnSave.
@Listen("onClick = #btnSave")
public void onClick$btnSave(Event event) {
try {
LOG.debug("onClick$btnSave");
for (Shape shape : shapesToSplit) shape.save();
Messagebox.show(Labels.getLabel("button.saveComplete"));
} catch (Exception e) {
LOG.error("Failure in onClick$btnSave", e);
throw new RuntimeException(e);
}
}
use of com.joliciel.jochre.graphics.Shape in project jochre by urieli.
the class SplitController method doAfterCompose.
@Override
public void doAfterCompose(Window window) throws Exception {
super.doAfterCompose(window);
String pageTitle = Labels.getLabel("splits.title");
winSplits.getPage().setTitle(pageTitle);
Session session = Sessions.getCurrent();
currentUser = (User) session.getAttribute(LoginController.SESSION_JOCHRE_USER);
if (currentUser == null)
Executions.sendRedirect("login.zul");
GraphicsDao graphicsDao = GraphicsDao.getInstance(jochreSession);
shapesToSplit = graphicsDao.findShapesToSplit(jochreSession.getLocale());
for (Shape shape : shapesToSplit) {
Row shapeRow = new Row();
Image shapeImage = new Image();
org.zkoss.image.Image convertedImage = Images.encode("shape_" + shape.getId() + ".png", shape.getImage());
shapeImage.setContent(convertedImage);
shapeImage.setWidth(shape.getImage().getWidth() + "px");
shapeImage.setHeight((shape.getImage().getHeight()) + "px");
Cell shapeCell = new Cell();
Div shapeDiv = new Div();
shapeDiv.setStyle("position: relative;");
shapeCell.appendChild(shapeDiv);
int shapeWidth = shape.getImage().getWidth();
shapeCell.setWidth((shapeWidth + 100) + "px");
shapeImage.addEventListener("onClick", new ShapeImageOnClickEventListener(shape, shapeDiv));
shapeDiv.appendChild(shapeImage);
for (Split split : shape.getSplits()) {
Div verticalLine = new Div();
verticalLine.setWidth("1px");
verticalLine.setHeight("100px");
verticalLine.setStyle("position: absolute; top:0px; left: " + split.getPosition() + "px;background-color:RED;");
verticalLine.setId("shape" + shape.getId() + "split" + split.getPosition());
verticalLine.addEventListener("onClick", new SplitOnClickEventListener(shape, shapeDiv, verticalLine, split));
shapeDiv.appendChild(verticalLine);
}
shapeRow.appendChild(shapeCell);
Cell letterCell = new Cell();
Label letterLabel = new Label();
letterLabel.setValue(shape.getLetter());
letterCell.appendChild(letterLabel);
letterCell.setWidth("100px");
shapeRow.appendChild(letterCell);
Cell detailsCell = new Cell();
Label detailsLabel = new Label();
JochrePage page = shape.getGroup().getRow().getParagraph().getImage().getPage();
JochreDocument doc = page.getDocument();
detailsLabel.setValue(doc.getName() + ", page " + page.getIndex());
detailsCell.appendChild(detailsLabel);
shapeRow.appendChild(detailsCell);
splitGridRows.appendChild(shapeRow);
}
}
use of com.joliciel.jochre.graphics.Shape in project jochre by urieli.
the class NgramFeature method checkInternal.
@Override
public FeatureResult<String> checkInternal(LetterGuesserContext context, RuntimeEnvironment env) {
FeatureResult<String> result = null;
FeatureResult<Integer> nResult = nFeature.check(context, env);
if (nResult != null) {
int n = nResult.getOutcome();
int historyToFind = n - 1;
String ngram = "";
Shape shape = context.getShapeInSequence().getShape();
LetterSequence history = context.getHistory();
for (int i = 0; i < historyToFind; i++) {
String letter = null;
if (history != null) {
// this is during analysis, we look at the current history
if (history.getLetters().size() > i) {
letter = history.getLetters().get(history.getLetters().size() - i - 1);
} else {
letter = SPACE;
}
} else {
// this is during training - we look at the previous letters
if (shape.getIndex() > i) {
GroupOfShapes group = shape.getGroup();
letter = group.getShapes().get(shape.getIndex() - i - 1).getLetter();
} else {
letter = SPACE;
}
}
ngram = letter + ngram;
}
result = this.generateResult(ngram);
}
return result;
}
use of com.joliciel.jochre.graphics.Shape in project jochre by urieli.
the class CorpusLexiconBuilder method buildLexicon.
/**
* Build a lexicon from the training corpus.
*/
public TextFileLexicon buildLexicon() {
TextFileLexicon lexicon = new TextFileLexicon();
JochreCorpusImageReader imageReader = new JochreCorpusImageReader(jochreSession);
imageReader.setSelectionCriteria(criteria);
String wordText = "";
while (imageReader.hasNext()) {
JochreImage image = imageReader.next();
for (Paragraph paragraph : image.getParagraphs()) {
// rows ending in dashes can only be held-over within the same
// paragraph.
// to avoid strange things like a page number getting added to
// the word,
// if the dash is on the last row of the page.
String holdoverWord = null;
for (RowOfShapes row : paragraph.getRows()) {
for (GroupOfShapes group : row.getGroups()) {
if (group.isBrokenWord())
continue;
wordText = "";
for (Shape shape : group.getShapes()) {
if (shape.getLetter() != null)
wordText += shape.getLetter();
}
if (wordText.length() == 0) {
lexicon.incrementEntry("");
continue;
}
List<String> words = jochreSession.getLinguistics().splitText(wordText);
int i = 0;
for (String word : words) {
if (i == 0) {
// first word
if (holdoverWord != null && holdoverWord.length() > 0) {
word = holdoverWord + word;
holdoverWord = null;
}
}
if (i == words.size() - 1) {
// last word
if (group.getIndex() == row.getGroups().size() - 1 && word.endsWith("-")) {
// a dash at the end of a line
if (group.isHardHyphen())
holdoverWord = word;
else
holdoverWord = word.substring(0, word.length() - 1);
word = "";
}
}
lexicon.incrementEntry(word);
i++;
}
}
}
}
}
return lexicon;
}
use of com.joliciel.jochre.graphics.Shape in project jochre by urieli.
the class RecursiveShapeSplitterTest method testSplitShapeNoSplitMoreLikely.
@SuppressWarnings("unchecked")
@Test
public void testSplitShapeNoSplitMoreLikely() throws Exception {
System.setProperty("config.file", "src/test/resources/test.conf");
ConfigFactory.invalidateCaches();
Config config = ConfigFactory.load();
JochreSession jochreSession = new JochreSession(config);
BufferedImage originalImage = new BufferedImage(256, 256, BufferedImage.TYPE_INT_RGB);
final JochreImage jochreImage = new JochreImage(originalImage, jochreSession);
final Shape shape = new Shape(jochreImage, 0, 0, 63, 15, jochreSession);
shape.setBaseLine(12);
shape.setMeanLine(4);
final Shape shape1 = new Shape(jochreImage, 0, 0, 31, 15, jochreSession);
shape1.setBaseLine(12);
shape1.setMeanLine(4);
final Shape shape2 = new Shape(jochreImage, 32, 0, 63, 15, jochreSession);
shape2.setBaseLine(12);
shape2.setMeanLine(4);
final SplitCandidateFinder splitCandidateFinder = mock(SplitCandidateFinder.class);
final DecisionMaker decisionMaker = mock(DecisionMaker.class);
Split split = new Split(shape, jochreSession);
split.setPosition(31);
List<Split> splits = new ArrayList<>();
splits.add(split);
when(splitCandidateFinder.findSplitCandidates(shape)).thenReturn(splits);
Decision yesDecision = new Decision(SplitOutcome.DO_SPLIT.name(), 0.4);
Decision noDecision = new Decision(SplitOutcome.DO_NOT_SPLIT.name(), 0.6);
List<Decision> decisions = new ArrayList<>();
decisions.add(yesDecision);
decisions.add(noDecision);
when(decisionMaker.decide(anyList())).thenReturn(decisions);
Split split1 = new Split(shape1, jochreSession);
split1.setPosition(15);
List<Split> splits1 = new ArrayList<>();
splits1.add(split1);
when(splitCandidateFinder.findSplitCandidates(shape1)).thenReturn(splits1);
Split split2 = new Split(shape2, jochreSession);
split2.setPosition(15);
List<Split> splits2 = new ArrayList<>();
splits2.add(split2);
when(splitCandidateFinder.findSplitCandidates(shape2)).thenReturn(splits2);
Set<SplitFeature<?>> splitFeatures = new TreeSet<>();
RecursiveShapeSplitter splitter = new RecursiveShapeSplitter(splitCandidateFinder, splitFeatures, decisionMaker, jochreSession);
splitter.setBeamWidth(10);
splitter.setMaxDepth(2);
splitter.setMinWidthRatio(1.0);
List<ShapeSequence> shapeSequences = splitter.split(shape);
assertEquals(5, shapeSequences.size());
int i = 0;
double prob = 1.0;
double twoThirds = 0.4 / 0.6;
LOG.debug("twoThirds: " + twoThirds);
for (ShapeSequence shapeSequence : shapeSequences) {
LOG.debug("sequence " + i + " decisions:");
for (Decision decision : shapeSequence.getDecisions()) LOG.debug("" + decision.getProbability());
if (i == 0) {
prob = 1.0;
assertEquals(prob, shapeSequence.getScore(), 0.0001);
assertEquals(1, shapeSequence.size());
} else if (i == 1) {
prob = 1.0 * twoThirds;
assertEquals(prob, shapeSequence.getScore(), 0.0001);
assertEquals(2, shapeSequence.size());
} else if (i == 2) {
prob = 1.0 * twoThirds * twoThirds;
assertEquals(prob, shapeSequence.getScore(), 0.0001);
assertEquals(3, shapeSequence.size());
} else if (i == 3) {
prob = 1.0 * twoThirds * twoThirds;
assertEquals(prob, shapeSequence.getScore(), 0.0001);
assertEquals(3, shapeSequence.size());
} else if (i == 4) {
prob = 1.0 * twoThirds * twoThirds * twoThirds;
assertEquals(prob, shapeSequence.getScore(), 0.0001);
assertEquals(4, shapeSequence.size());
}
i++;
}
}
Aggregations