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);
}
}
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);
}
}
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;
}
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;
}
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;
}
Aggregations