Commit 6bb58a18 authored by Christos Christodoulopoulos's avatar Christos Christodoulopoulos
Browse files

Added button to remove annotation

Various bug fixes
parent fe4c1058
......@@ -4,6 +4,7 @@ import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import javax.swing.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
......@@ -23,7 +24,10 @@ public class JBCorpus {
private String corpusFile;
private int currentAnnotationIndex;
public JBCorpus(String file) {
private String annotator;
public JBCorpus(String file, String annotator) {
this.annotator = annotator;
annotations = new ArrayList<JBDataStructure>();
contexts = new ArrayList<String>();
corpusFile = file;
......@@ -59,6 +63,7 @@ public class JBCorpus {
}
} catch (Exception e) {
System.err.println("Error reading the corpus");
e.printStackTrace();
System.exit(-1);
}
}
......@@ -86,14 +91,40 @@ public class JBCorpus {
out.close();
}
public void setLastEditIndex(int index) {
private void setLastEditIndex(int index) {
lastEditIndex = index;
}
public void setCurrentAnnotationIndex(int currentAnnotationIndex) {
// Before moving on, confirm that we want to keep the changes to the current annotation
if (getCurrentAnnotation() != null && getCurrentAnnotation().isChanged())
confirmChanges();
this.currentAnnotationIndex = currentAnnotationIndex;
}
private void confirmChanges() {
boolean save = false;
int response = JOptionPane.showConfirmDialog(null, "Do you want to keep the changes to the annotation?",
"Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (response == JOptionPane.NO_OPTION) save = false;
else if (response == JOptionPane.YES_OPTION) save = true;
else if (response == JOptionPane.CLOSED_OPTION) save = false;
if (!save) {
try {
revertChanges();
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, "There was an error: \n" + e.getMessage(), "Error",
JOptionPane.ERROR_MESSAGE);
}
}
else {
getCurrentAnnotation().setAnnotator(annotator);
getCurrentAnnotation().hasChanged(false);
setLastEditIndex(getCurrentAnnotationIndex());
}
}
public void copyCurrent(JBCorpus otherCorpus) {
getCurrentAnnotation().copyFrom(otherCorpus.getCurrentAnnotation());
}
......@@ -101,11 +132,14 @@ public class JBCorpus {
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");
// TODO Check that this is writing to the correct place
annotations.remove(currentAnnotationIndex);
annotations.add(currentAnnotationIndex, currentAnnotationOriginal);
}
public void removeCurrentAnnotation() {
annotations.remove(currentAnnotationIndex);
}
// --------------------- Getters --------------------- //
public JBDataStructure getAnnotation(int index) {
......@@ -132,6 +166,8 @@ public class JBCorpus {
}
public JBDataStructure getCurrentAnnotation() {
// In case we just deleted the last annotation
if (currentAnnotationIndex >= annotations.size()) return null;
return annotations.get(currentAnnotationIndex);
}
public List<String> getContexts() {
......
......@@ -4,6 +4,7 @@ import jubilee.treebank.TBNode;
import jubilee.treebank.TBTree;
import jubilee.util.FileTokenizer;
import javax.swing.*;
import java.util.StringTokenizer;
/**
......@@ -29,19 +30,17 @@ public class JBDataStructure {
/** Indicates that some part of the structure has changed (to prompt for a save dialogue) */
private boolean changed;
private int predId;
private String annotator;
private String type;
private String roleset;
private String aspect;
private TBTree tbTree;
/** Dummy constructor */
public JBDataStructure() { }
public JBDataStructure() { this.changed = false; }
public JBDataStructure(String treeString, String pbInstanceString, int indexInContext) throws Exception {
this.indexInContext = indexInContext;
this.tbTree = readTBTree(treeString);
tbTree = readTBTree(treeString);
readPBInstance(pbInstanceString);
this.changed = false;
}
......@@ -99,19 +98,20 @@ public class JBDataStructure {
}
private void readPBInstance(String pbInstanceString) {
StringTokenizer tok = new StringTokenizer(pbInstanceString);
predId = Integer.parseInt(tok.nextToken());
String info = pbInstanceString.split("#")[0];
String labels = pbInstanceString.split("#")[1];
// add 'rel' as an argument to the predicate
tbTree.moveToTerminal(predId);
tbTree.setArg(predId + ":0", REL);
StringTokenizer tok = new StringTokenizer(info);
annotator = tok.nextToken();
type = tok.nextToken();
roleset = tok.nextToken();
aspect = tok.nextToken();
readPropbankLabels(labels, tbTree);
}
private void readPropbankLabels(String labels, TBTree tree) {
StringTokenizer tok = new StringTokenizer(labels);
while (tok.hasMoreTokens()) {
StringTokenizer tok_termInfo = new StringTokenizer(tok.nextToken(), ARG_JOINER);
StringTokenizer tok_description = new StringTokenizer(tok_termInfo.nextToken(), ":" + ANT_FUNC, true);
......@@ -126,9 +126,9 @@ public class JBDataStructure {
tok_description.nextToken(); // :
int height = Integer.parseInt(tok_description.nextToken());
tbTree.moveTo(terminalIdx, height);
tree.moveTo(terminalIdx, height);
loc += symbol + terminalIdx + ":" + height;
tbTree.setArg(loc, arg);
tree.setArg(loc, arg);
if (tok_description.hasMoreTokens()) // get symbol
symbol = tok_description.nextToken();
......@@ -137,35 +137,46 @@ public class JBDataStructure {
}
public void setTree(String treeString) throws Exception {
tbTree = readTBTree(treeString);
hasChanged();
TBTree newTree = readTBTree(treeString);
// Add the propbank annotation to the new tree
readPropbankLabels(tbTree.toPropbank(), newTree);
if (!tbTree.equals(newTree)) {
tbTree = newTree;
hasChanged(true);
}
else {
JOptionPane.showMessageDialog(null, "No changes detected", "Information",
JOptionPane.INFORMATION_MESSAGE);
}
}
public void setRoleset(String roleset) {
this.roleset = roleset;
hasChanged();
if (!this.roleset.equals(roleset)) {
this.roleset = roleset;
hasChanged(true);
}
}
public void setAnnotator(String annotator) {
this.annotator = annotator;
hasChanged();
if (!this.annotator.equals(annotator)) {
this.annotator = annotator;
hasChanged(true);
}
}
public void copyFrom(JBDataStructure other) {
this.indexInContext = other.indexInContext;
this.tbTree = other.tbTree;
this.predId = other.predId;
this.type = other.type;
this.roleset = other.roleset;
this.aspect = other.aspect;
}
public void hasChanged() {
changed = true;
public void hasChanged(boolean changed) {
this.changed = changed;
}
public String toProbankString() {
return predId + " " + annotator + " " + type + " " + roleset + " " + aspect + " " + tbTree.toPropbank();
return annotator + " " + type + " " + roleset + "#" + tbTree.toPropbank();
}
// --------------------- Getters --------------------- //
......
......@@ -30,10 +30,7 @@ import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.*;
import java.io.File;
import java.util.*;
import java.util.List;
......@@ -70,7 +67,17 @@ public class JBOpenDialog extends JDialog implements ActionListener, ItemListene
initComponents(cp, isCancel);
initBounds(cp);
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
if (isCancel)
setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
else {
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
setBounds(10, 10, 300, 600);
setVisible(true);
}
......@@ -217,8 +224,10 @@ public class JBOpenDialog extends JDialog implements ActionListener, ItemListene
private void actionBtEnter() {
if (ls_newTask.isSelectionEmpty() && ls_myTask.isSelectionEmpty()) return;
jbtk.i_currSetting = cb_projects.getSelectedIndex();
jbtk.initProperties(m_dataset);
jbtk.i_currSetting = cb_projects.getSelectedIndex();
StringTokenizer tok = new StringTokenizer((String)cb_projects.getSelectedItem(), ".");
String corpusStr = tok.nextToken();
jbtk.initProperties(corpusStr, m_dataset);
if (!ls_newTask.isSelectionEmpty()) {
String[] tmp = getFileList(ls_newTask.getSelectedValue(), true);
......
......@@ -51,7 +51,7 @@ public class JBToolkit extends JFrame implements ActionListener, ItemListener, L
private String str_annFile;
// Treeview: top-pane
private JButton bt_prev, bt_next, buttonTreeEdit;
private JButton bt_prev, bt_next, buttonTreeEdit, buttonRemoveAnnotation;
private JComboBox<String> comboJump;
private JTextField tf_annotator;
private JTextArea ta_sentence;
......@@ -83,7 +83,6 @@ public class JBToolkit extends JFrame implements ActionListener, ItemListener, L
showOpenDialog();
//TODO Check this
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
@Override
......@@ -126,6 +125,9 @@ public class JBToolkit extends JFrame implements ActionListener, ItemListener, L
buttonTreeEdit = new JButton("Edit Tree");
buttonTreeEdit.addActionListener(this);
buttonRemoveAnnotation = new JButton("Remove Annotation");
buttonRemoveAnnotation.addActionListener(this);
comboJump = new JComboBox<String>();
comboJump.setMaximumRowCount(20);
......@@ -192,6 +194,7 @@ public class JBToolkit extends JFrame implements ActionListener, ItemListener, L
JPanel treeEditPanel = new JPanel();
treeEditPanel.setLayout(new BorderLayout());
treeEditPanel.add(buttonTreeEdit, BorderLayout.EAST);
treeEditPanel.add(buttonRemoveAnnotation, BorderLayout.WEST);
bottomPanel.add(treeEditPanel, BorderLayout.SOUTH);
......@@ -220,8 +223,8 @@ public class JBToolkit extends JFrame implements ActionListener, ItemListener, L
}
// called from JBOpenDialog
void initProperties(HashMap<String, String> dataset) {
ArrayList<String[]> argTag = DataManager.getContents(DataManager.ARGS_FILE_EXT);
void initProperties(String corpusStr, HashMap<String, String> dataset) {
ArrayList<String[]> argTag = DataManager.getContents(corpusStr + DataManager.ARGS_FILE_EXT);
str_dataset = dataset;
mbar.setMenuArgTag(argTag);
......@@ -237,21 +240,23 @@ public class JBToolkit extends JFrame implements ActionListener, ItemListener, L
if (isNewTask) {
if (isGold()) {
String taskFile = str_dataset.get(DataManager.ANNOTATION) + File.separator + filename[1];
corpus = new JBCorpus(taskFile);
// XXX Not sure if this is right
corpus = new JBCorpus(taskFile, DataManager.GOLD_ID);
}
else {
String taskFile = str_dataset.get(DataManager.TASK) + File.separator + filename[0];
corpus = new JBCorpus(taskFile);
corpus = new JBCorpus(taskFile, str_userID);
}
}
else
corpus = new JBCorpus(str_annFile);
corpus = new JBCorpus(str_annFile, str_userID);
if (isGold()) {
moreCorpora = new JBCorpus[filename.length-1];
for (int i=1; i<filename.length; i++) {
filename[i] = str_dataset.get(DataManager.ANNOTATION) + File.separator + filename[i];
moreCorpora[i-1] = new JBCorpus(filename[i]);
// XXX Not sure if this is right
moreCorpora[i-1] = new JBCorpus(filename[i], DataManager.GOLD_ID);
}
}
......@@ -284,18 +289,16 @@ public class JBToolkit extends JFrame implements ActionListener, ItemListener, L
else if (e.getSource() == mbar.fsViewExample) framesetPanel.showExample();
else if (e.getSource() == mbar.fsViewArgument) tv_tree.viewArgument();
else if (e.getSource() == mbar.fsViewRolesetComment) framesetPanel.viewRolesetComment();
else if (menuArgumentArg(e)) ;
else if (menuArgumentFunc(e)) ;
else if (e.getSource() == mbar.argErase) argPanel.updateArg(e.getActionCommand());
else if (e.getSource() == mbar.helpAbout) menuHelpAbout();
else if (e.getSource() == buttonTreeEdit) actionButtonTreeEdit();
else if (e.getSource() == buttonRemoveAnnotation) actionButtonRemoveAnnotation();
menuArgumentArg(e);
menuArgumentFunc(e);
}
public void itemStateChanged(ItemEvent e) {
if (e.getSource() == comboJump && comboJump.getSelectedIndex() >= 0) {
// Before moving on, confirm that we want to keep the changes to the current annotation
if (corpus.getCurrentAnnotation().isChanged())
confirmChanges();
corpus.setCurrentAnnotationIndex(comboJump.getSelectedIndex());
updateAll();
updateGoldList();
......@@ -352,25 +355,30 @@ public class JBToolkit extends JFrame implements ActionListener, ItemListener, L
updateAll();
}
private void confirmChanges() {
boolean save = false;
int response = JOptionPane.showConfirmDialog(null, "Do you want to keep the changes to the annotation?",
"Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (response == JOptionPane.NO_OPTION) save = false;
else if (response == JOptionPane.YES_OPTION) save = true;
else if (response == JOptionPane.CLOSED_OPTION) save = false;
if (!save) {
try {
corpus.revertChanges();
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, "There was an error: \n" + e.getMessage(), "Error",
JOptionPane.ERROR_MESSAGE);
private void actionButtonRemoveAnnotation() {
boolean delete = false;
int response = JOptionPane.showConfirmDialog(null, "Do you really want to remove this annotation?\n" +
"THIS ACTION IS NOT REVERSIBLE!",
"Warning", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
if (response == JOptionPane.NO_OPTION) delete = false;
else if (response == JOptionPane.YES_OPTION) delete = true;
else if (response == JOptionPane.CLOSED_OPTION) delete = false;
if (delete) {
int index = corpus.getCurrentAnnotationIndex();
corpus.removeCurrentAnnotation();
// Rebuild the combo box
comboJump.removeAllItems();
for (int i = 0; i < corpus.getSize(); i++) {
int position = corpus.getAnnotation(i).getIndexInContext();
String contextId = corpus.getContext(position);
comboJump.insertItemAt(i + " - \"" + contextId.substring(0, contextId.indexOf(":")) + "\"", i);
}
}
else {
corpus.getCurrentAnnotation().setAnnotator(str_userID);
corpus.setLastEditIndex(corpus.getCurrentAnnotationIndex());
// Now move to the next annotation
if (index - 1 >= 0) comboJump.setSelectedIndex(index - 1);
else comboJump.setSelectedIndex(index + 1);
}
}
......@@ -412,7 +420,6 @@ public class JBToolkit extends JFrame implements ActionListener, ItemListener, L
corpus.setCurrentAnnotationIndex(i - 1);
if (!isGold() || !isGoldSame()) break;
}
// TODO This should ask me to save if needed (check this)
comboJump.setSelectedIndex(corpus.getCurrentAnnotationIndex());
}
......@@ -421,7 +428,6 @@ public class JBToolkit extends JFrame implements ActionListener, ItemListener, L
corpus.setCurrentAnnotationIndex(i + 1);
if (!isGold() || !isGoldSame()) break;
}
// TODO This should ask me to save if needed (check this)
comboJump.setSelectedIndex(corpus.getCurrentAnnotationIndex());
}
......
......@@ -25,9 +25,12 @@ public class JBTreeEditPanel extends JFrame implements ActionListener {
this.annotation = annotation;
textArea = new JTextArea(annotation.getTbTree().toTextTree());
textArea.setMargin(new Insets(10, 10, 10, 10));
buttonApply = new JButton("Apply");
buttonApply.addActionListener(this);
buttonCancel = new JButton("Cancel");
buttonCancel.addActionListener(this);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(0, 2));
......@@ -35,7 +38,7 @@ public class JBTreeEditPanel extends JFrame implements ActionListener {
buttonPanel.add(buttonCancel);
setLayout(new BorderLayout());
add(new JScrollPane(textArea), BorderLayout.NORTH);
add(new JScrollPane(textArea), BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
addWindowListener(new WindowAdapter() {
......@@ -45,7 +48,7 @@ public class JBTreeEditPanel extends JFrame implements ActionListener {
}
});
setBounds(20, 20, 700, 700);
setBounds(20, 20, 500, 400);
setVisible(true);
}
......@@ -56,6 +59,7 @@ public class JBTreeEditPanel extends JFrame implements ActionListener {
else if (e.getSource() == buttonApply)
try {
annotation.setTree(textArea.getText());
dispose();
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "There was an error: \n" + ex.getMessage(), "Error",
JOptionPane.ERROR_MESSAGE);
......
......@@ -134,7 +134,7 @@ public class JBTreePanel extends JPanel {
prevArg = tb_tree.getArg();
}
// Mark the change in annotation structure (to be used for save prompt)
annotation.hasChanged();
annotation.hasChanged(true);
setTree(tb_tree);
j_tree.setSelectionInterval(selectionRow[0], selectionRow[selectionRow.length-1]);
}
......
......@@ -389,4 +389,11 @@ public class TBTree {
return str+")";
}
@Override
public boolean equals(Object otherTreeObj) {
if (!(otherTreeObj instanceof TBTree)) return false;
TBTree otherTree = (TBTree) otherTreeObj;
return otherTree.toPropbank().equals(toPropbank()) && otherTree.toTextTree().equals(toTextTree());
}
}
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