Search in sources :

Example 11 with GroupOfShapes

use of com.joliciel.jochre.graphics.GroupOfShapes in project jochre by urieli.

the class ImageController method save.

void save() {
    try {
        Comboitem selectedItem = cmbStatus.getSelectedItem();
        ImageStatus imageStatus = ImageStatus.forId((Integer) selectedItem.getValue());
        currentImage.setImageStatus(imageStatus);
        if (currentUser.getRole().equals(UserRole.ADMIN)) {
            User owner = (User) cmbOwner.getSelectedItem().getValue();
            currentImage.setOwner(owner);
        }
        GraphicsDao graphicsDao = GraphicsDao.getInstance(jochreSession);
        graphicsDao.saveJochreImage(currentImage);
        for (Paragraph paragraph : currentImage.getParagraphs()) {
            LOG.trace("Paragraph " + paragraph.getIndex() + ", " + paragraph.getRows().size() + " rows");
            for (RowOfShapes row : paragraph.getRows()) {
                List<List<String>> letterGroups = this.getLetterGroups(row);
                LOG.trace("Row " + row.getIndex() + ", " + row.getGroups().size() + " groups, " + letterGroups.size() + " letter groups");
                Iterator<List<String>> iLetterGroups = letterGroups.iterator();
                for (GroupOfShapes group : row.getGroups()) {
                    LOG.trace("Group " + group.getIndex() + " text : " + group.getWord());
                    boolean hasChange = false;
                    List<String> letters = null;
                    if (iLetterGroups.hasNext())
                        letters = iLetterGroups.next();
                    else
                        letters = new ArrayList<String>();
                    LOG.trace("Found " + letters.size() + " letters in text");
                    Iterator<String> iLetters = letters.iterator();
                    for (Shape shape : group.getShapes()) {
                        String currentLetter = shape.getLetter();
                        if (currentLetter == null)
                            currentLetter = "";
                        String newLetter = "";
                        if (iLetters.hasNext())
                            newLetter = iLetters.next();
                        if (newLetter.startsWith("[") && newLetter.endsWith("]")) {
                            newLetter = newLetter.substring(1, newLetter.length() - 1);
                        }
                        LOG.trace("currentLetter:  " + currentLetter + ", newLetter: " + newLetter);
                        if (!currentLetter.equals(newLetter)) {
                            LOG.trace("newLetter: " + newLetter);
                            shape.setLetter(newLetter);
                            shape.save();
                            hasChange = true;
                        }
                    }
                    if (hasChange)
                        LOG.trace("Group text after : " + group.getWord());
                }
            // next group
            }
        // next row
        }
        // next paragraph
        Messagebox.show(Labels.getLabel("button.saveComplete"));
    } catch (Exception e) {
        LOG.error("Failure in save", e);
        throw new RuntimeException(e);
    }
}
Also used : User(com.joliciel.jochre.security.User) Shape(com.joliciel.jochre.graphics.Shape) ImageStatus(com.joliciel.jochre.graphics.ImageStatus) ArrayList(java.util.ArrayList) RowOfShapes(com.joliciel.jochre.graphics.RowOfShapes) Paragraph(com.joliciel.jochre.graphics.Paragraph) GraphicsDao(com.joliciel.jochre.graphics.GraphicsDao) GroupOfShapes(com.joliciel.jochre.graphics.GroupOfShapes) Comboitem(org.zkoss.zul.Comboitem) List(java.util.List) ArrayList(java.util.ArrayList)

Example 12 with GroupOfShapes

use of com.joliciel.jochre.graphics.GroupOfShapes in project jochre by urieli.

the class UnknownWordListWriter method onImageComplete.

@Override
public void onImageComplete(JochreImage image) {
    try {
        for (Paragraph paragraph : image.getParagraphs()) {
            if (!paragraph.isJunk()) {
                for (RowOfShapes row : paragraph.getRows()) {
                    for (GroupOfShapes group : row.getGroups()) {
                        if (group.getBestLetterSequence() != null) {
                            for (LetterSequence subsequence : group.getBestLetterSequence().getSubsequences()) {
                                for (CountedOutcome<String> wordFrequency : subsequence.getWordFrequencies()) {
                                    if (wordFrequency.getCount() == 0) {
                                        writer.write(wordFrequency.getOutcome() + "\n");
                                        writer.flush();
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    } catch (IOException e) {
        LOG.error("Failed to write to UnknownWordListWriter", e);
        throw new RuntimeException(e);
    }
}
Also used : LetterSequence(com.joliciel.jochre.letterGuesser.LetterSequence) GroupOfShapes(com.joliciel.jochre.graphics.GroupOfShapes) RowOfShapes(com.joliciel.jochre.graphics.RowOfShapes) IOException(java.io.IOException) Paragraph(com.joliciel.jochre.graphics.Paragraph)

Example 13 with GroupOfShapes

use of com.joliciel.jochre.graphics.GroupOfShapes 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;
}
Also used : LetterSequence(com.joliciel.jochre.letterGuesser.LetterSequence) Shape(com.joliciel.jochre.graphics.Shape) GroupOfShapes(com.joliciel.jochre.graphics.GroupOfShapes)

Example 14 with GroupOfShapes

use of com.joliciel.jochre.graphics.GroupOfShapes 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;
}
Also used : JochreCorpusImageReader(com.joliciel.jochre.graphics.JochreCorpusImageReader) JochreImage(com.joliciel.jochre.graphics.JochreImage) Shape(com.joliciel.jochre.graphics.Shape) GroupOfShapes(com.joliciel.jochre.graphics.GroupOfShapes) RowOfShapes(com.joliciel.jochre.graphics.RowOfShapes) Paragraph(com.joliciel.jochre.graphics.Paragraph)

Example 15 with GroupOfShapes

use of com.joliciel.jochre.graphics.GroupOfShapes in project jochre by urieli.

the class LetterSequence method getGroups.

/**
 * A list of groups of shapes underlying this letter sequence.
 */
public List<GroupOfShapes> getGroups() {
    List<GroupOfShapes> groups = new ArrayList<GroupOfShapes>();
    GroupOfShapes currentGroup = this.getUnderlyingShapeSequence().get(0).getShape().getGroup();
    groups.add(currentGroup);
    for (ShapeInSequence shapeInSequence : this.getUnderlyingShapeSequence()) {
        if (!shapeInSequence.getShape().getGroup().equals(currentGroup)) {
            currentGroup = shapeInSequence.getShape().getGroup();
            groups.add(currentGroup);
        }
    }
    return groups;
}
Also used : GroupOfShapes(com.joliciel.jochre.graphics.GroupOfShapes) ArrayList(java.util.ArrayList) ShapeInSequence(com.joliciel.jochre.boundaries.ShapeInSequence)

Aggregations

GroupOfShapes (com.joliciel.jochre.graphics.GroupOfShapes)18 Shape (com.joliciel.jochre.graphics.Shape)14 Paragraph (com.joliciel.jochre.graphics.Paragraph)10 RowOfShapes (com.joliciel.jochre.graphics.RowOfShapes)10 ArrayList (java.util.ArrayList)9 JochreImage (com.joliciel.jochre.graphics.JochreImage)6 ShapeInSequence (com.joliciel.jochre.boundaries.ShapeInSequence)4 JochreDocument (com.joliciel.jochre.doc.JochreDocument)4 JochrePage (com.joliciel.jochre.doc.JochrePage)4 LetterSequence (com.joliciel.jochre.letterGuesser.LetterSequence)4 Test (org.junit.Test)4 ShapeSequence (com.joliciel.jochre.boundaries.ShapeSequence)3 IOException (java.io.IOException)3 StringWriter (java.io.StringWriter)3 List (java.util.List)3 JochreSession (com.joliciel.jochre.JochreSession)2 Config (com.typesafe.config.Config)2 Writer (java.io.Writer)2 TreeMap (java.util.TreeMap)2 GraphicsDao (com.joliciel.jochre.graphics.GraphicsDao)1