Commit dac419c5 authored by Christos Christodoulopoulos's avatar Christos Christodoulopoulos
Browse files

Fixed a bug with reversing changes

parent 6bb58a18
......@@ -32,6 +32,7 @@ public class JBCorpus {
contexts = new ArrayList<String>();
corpusFile = file;
currentAnnotationIndex = 0;
currentAnnotationOriginal = new JBDataStructure();
readCorpus();
}
......@@ -126,27 +127,31 @@ public class JBCorpus {
}
public void copyCurrent(JBCorpus otherCorpus) {
getCurrentAnnotation().copyFrom(otherCorpus.getCurrentAnnotation());
getCurrentAnnotation().copyFrom(otherCorpus.getCurrentAnnotation(), false);
}
public void revertChanges() throws Exception {
// Make sure that there is something to revert back to
if (currentAnnotationOriginal == null) throw new Exception("Original annotation is missing");
annotations.remove(currentAnnotationIndex);
annotations.add(currentAnnotationIndex, currentAnnotationOriginal);
annotations.set(currentAnnotationIndex, currentAnnotationOriginal);
}
public void removeCurrentAnnotation() {
annotations.remove(currentAnnotationIndex);
}
public void backup(JBDataStructure currentAnnotation) {
// If this is the first type we visit this annotation, make a copy
if (currentAnnotationOriginal.getIndexInContext() != currentAnnotation.getIndexInContext()) {
currentAnnotationOriginal = new JBDataStructure();
currentAnnotationOriginal.copyFrom(currentAnnotation, true);
}
}
// --------------------- Getters --------------------- //
public JBDataStructure getAnnotation(int index) {
JBDataStructure annotation = annotations.get(index);
currentAnnotationOriginal = new JBDataStructure();
currentAnnotationOriginal.copyFrom(annotation);
return annotation;
return annotations.get(index);
}
public int getSize() {
......@@ -168,7 +173,7 @@ public class JBCorpus {
public JBDataStructure getCurrentAnnotation() {
// In case we just deleted the last annotation
if (currentAnnotationIndex >= annotations.size()) return null;
return annotations.get(currentAnnotationIndex);
return getAnnotation(currentAnnotationIndex);
}
public List<String> getContexts() {
return contexts;
......
......@@ -35,8 +35,11 @@ public class JBDataStructure {
private String roleset;
private TBTree tbTree;
/** Dummy constructor */
public JBDataStructure() { this.changed = false; }
/** Dummy constructor used for the backup of annotations (see {@link JBCorpus#backup(JBDataStructure)}) */
public JBDataStructure() {
this.indexInContext = -1;
this.changed = false;
}
public JBDataStructure(String treeString, String pbInstanceString, int indexInContext) throws Exception {
this.indexInContext = indexInContext;
......@@ -164,11 +167,13 @@ public class JBDataStructure {
}
}
public void copyFrom(JBDataStructure other) {
public void copyFrom(JBDataStructure other, boolean copyAnnotator) {
this.indexInContext = other.indexInContext;
this.tbTree = other.tbTree;
this.tbTree = other.tbTree.copy();
this.type = other.type;
this.roleset = other.roleset;
if (copyAnnotator)
this.annotator = other.annotator;
}
public void hasChanged(boolean changed) {
......
......@@ -300,8 +300,9 @@ public class JBToolkit extends JFrame implements ActionListener, ItemListener, L
public void itemStateChanged(ItemEvent e) {
if (e.getSource() == comboJump && comboJump.getSelectedIndex() >= 0) {
corpus.setCurrentAnnotationIndex(comboJump.getSelectedIndex());
updateAll();
updateGoldList();
corpus.backup(corpus.getCurrentAnnotation());
updateAll();
updateGoldList();
}
}
......@@ -312,7 +313,7 @@ public class JBToolkit extends JFrame implements ActionListener, ItemListener, L
}
}
private void updateAll() {
public void updateAll() {
JBDataStructure annotation = corpus.getCurrentAnnotation();
tv_tree.setAnnotation(annotation);
ta_sentence.setText(annotation.getTbTree().getSentence());
......@@ -351,8 +352,7 @@ public class JBToolkit extends JFrame implements ActionListener, ItemListener, L
}
private void actionButtonTreeEdit() {
new JBTreeEditPanel(corpus.getCurrentAnnotation());
updateAll();
new JBTreeEditPanel(this, corpus.getCurrentAnnotation());
}
private void actionButtonRemoveAnnotation() {
......@@ -377,8 +377,8 @@ public class JBToolkit extends JFrame implements ActionListener, ItemListener, L
}
// Now move to the next annotation
if (index - 1 >= 0) comboJump.setSelectedIndex(index - 1);
else comboJump.setSelectedIndex(index + 1);
if (index < corpus.getSize()) comboJump.setSelectedIndex(index);
else comboJump.setSelectedIndex(index - 1);
}
}
......
......@@ -19,10 +19,13 @@ public class JBTreeEditPanel extends JFrame implements ActionListener {
private JButton buttonApply, buttonCancel;
private JBDataStructure annotation;
public JBTreeEditPanel(JBDataStructure annotation) {
JBToolkit parent;
public JBTreeEditPanel(JBToolkit parent, JBDataStructure annotation) {
super("Syntax tree");
this.annotation = annotation;
this.parent = parent;
textArea = new JTextArea(annotation.getTbTree().toTextTree());
textArea.setMargin(new Insets(10, 10, 10, 10));
......@@ -59,6 +62,7 @@ public class JBTreeEditPanel extends JFrame implements ActionListener {
else if (e.getSource() == buttonApply)
try {
annotation.setTree(textArea.getText());
parent.updateAll();
dispose();
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "There was an error: \n" + ex.getMessage(), "Error",
......
......@@ -146,4 +146,25 @@ public class TBNode {
children.add(child);
}
public TBNode copy() {
return copySelf(this, null);
}
private TBNode copySelf(TBNode origin, TBNode parent) {
TBNode curr = new TBNode(parent, origin.getPos());
curr.word = origin.getWord();
curr.arg = origin.getArg();
curr.loc = origin.getLoc();
if (origin.children != null) {
curr.children = new ArrayList<TBNode>();
for (int i=0; i < origin.getChildren().size(); i++)
curr.children.add(copySelf(origin.children.get(i), curr));
}
return curr;
}
}
......@@ -396,4 +396,8 @@ public class TBTree {
TBTree otherTree = (TBTree) otherTreeObj;
return otherTree.toPropbank().equals(toPropbank()) && otherTree.toTextTree().equals(toTextTree());
}
public TBTree copy() {
return new TBTree(mb_head.copy());
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment