/** * edu.princeton.wordnet.pojos.app * WordNetQueries.java * * @author Bernard Bou */ package edu.princeton.wordnet.pojos.app; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Set; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; import edu.princeton.wordnet.pojos.Lexlink; import edu.princeton.wordnet.pojos.Morphmap; import edu.princeton.wordnet.pojos.Semlink; import edu.princeton.wordnet.pojos.Sense; import edu.princeton.wordnet.pojos.Synset; import edu.princeton.wordnet.pojos.Word; /** * WordNet queries (sample application) * * @author Bernard Bou */ public class WordNetQueries { // Q U E R Y /** * Wrapper class * * @author Bernard Bou */ static abstract class WordQuery { /** * Make Hibernate query * * @param thisSession * Hibernate session * @return Hibernate query */ protected abstract Query make(Session thisSession); /** * Process set resulting from executing this query Assumes the query yields a set of Words * * @param thisQuery * Hibernate query */ @SuppressWarnings("unchecked") static private void process(final Query thisQuery) { final Iterator thisResultSet = thisQuery.iterate(); while (thisResultSet.hasNext()) { final Word w = (Word) thisResultSet.next(); WordNetQueries.process(w); } } public void run() { final Session thisSession = SessionFactoryUtil.getInstance().openSession(); try { WordQuery.process(make(thisSession)); } catch (final RuntimeException e) { // throw again the first exception throw e; } } public void runInTransaction() { Transaction thisTransaction = null; final Session thisSession = SessionFactoryUtil.getInstance().openSession(); try { thisTransaction = thisSession.beginTransaction(); WordQuery.process(make(thisSession)); thisTransaction.commit(); } catch (final RuntimeException e) { if (thisTransaction != null && thisTransaction.isActive()) { try { // this is read-only so transaction is unnecessary // second try catch as the rollback could fail as well thisTransaction.rollback(); } catch (final HibernateException e1) { System.err.println("Error rolling back transaction"); //$NON-NLS-1$ } } // throw again the first exception throw e; } } } static WordQuery allWords = new WordQuery() { @Override protected Query make(final Session session) { return session.createQuery("from Word"); //$NON-NLS-1$ } }; static class WordQueryFromLemma extends WordQuery { private final String theLemma; public WordQueryFromLemma(final String thisLemma) { this.theLemma = thisLemma; } /* * (non-Javadoc) * * @see edu.princeton.wordnet.pojos.app.WordNetQueries.WordQuery#make(org.hibernate.Session) */ @Override protected Query make(final Session session) { final Query thisQuery = session.createQuery("from Word where lemma = :lemma"); //$NON-NLS-1$ thisQuery.setString("lemma", this.theLemma); //$NON-NLS-1$ return thisQuery; } } static class WordQueryFromLikeness extends WordQuery { private final String thePattern; public WordQueryFromLikeness(final String thisPattern) { this.thePattern = thisPattern; } /* * (non-Javadoc) * * @see edu.princeton.wordnet.pojos.app.WordNetQueries.WordQuery#make(org.hibernate.Session) */ @Override protected Query make(final Session session) { final Query thisQuery = session.createQuery("from Word where lemma like :pattern"); //$NON-NLS-1$ thisQuery.setString("pattern", this.thePattern); //$NON-NLS-1$ return thisQuery; } } static class WordQueryFromId extends WordQuery { private final int theId; public WordQueryFromId(final int thisId) { this.theId = thisId; } /* * (non-Javadoc) * * @see edu.princeton.wordnet.pojos.app.WordNetQueries.WordQuery#make(org.hibernate.Session) */ @Override protected Query make(final Session thisSession) { final Query thisQuery = thisSession.createQuery("from Word where wordid = :id"); //$NON-NLS-1$ thisQuery.setInteger("id", this.theId); //$NON-NLS-1$ return thisQuery; } } // G E T T E R /** * Wrapper class * * @author Bernard Bou */ static class WordGetter { private final int theId; public WordGetter(final int thisId) { this.theId = thisId; } /** * Get word * * @param thisSession * Hibernated Session * @param thisWordId * wordid * @return word */ private Word get(final Session thisSession) { return (Word) thisSession.get(Word.class, new Integer(this.theId)); } /** * Get word * * @param thisWordId * wordid */ public void run() { final Session thisSession = SessionFactoryUtil.getInstance().openSession(); try { WordNetQueries.process(get(thisSession)); } catch (final RuntimeException e) { // throw again the first exception throw e; } } } // P R O C E S S /** * Process word * * @param thisWord * word */ static void process(final Word thisWord) { System.out.printf("%s\n", Utils.word2String(thisWord)); //$NON-NLS-1$ final Set theseSenses = thisWord.getSenses(); if (theseSenses != null) { for (final Sense thisSense : theseSenses) { final Synset sy = thisSense.getSynset(); System.out.println(Utils.synset2String(sy)); // AS SOURCE OF RELATION // semantic relations final Set sls1 = sy.getSemlinksForSynset1id(); for (final Semlink sl1 : sls1) { System.out.println(Utils.semLink2String(sl1, true)); } // lexical relations final Set lls1 = sy.getLexlinksForSynset1id(); for (final Lexlink ll1 : lls1) { System.out.println(Utils.lexLink2String(ll1, true)); } // AS TARGET OF RELATION // semantic relations final Set sls2 = sy.getSemlinksForSynset2id(); for (final Semlink sl2 : sls2) { System.out.println(Utils.semLink2String(sl2, false)); } // lexical relations final Set lls2 = sy.getLexlinksForSynset2id(); for (final Lexlink ll2 : lls2) { System.out.println(Utils.lexLink2String(ll2, false)); } } } System.out.println(); } // U T I L I T I E S static class Utils { /** * Get words in synset * * @param thisSynset * synset * @return array of lemma strings */ static String[] getWords(final Synset thisSynset) { final List theseWords = new ArrayList(); for (final Sense thisSense : thisSynset.getSenses()) { theseWords.add(thisSense.getWord().getLemma()); } return theseWords.toArray(new String[theseWords.size()]); } /** * Get morphological forms for word * * @param thisWord * word * @return array of morphological strings */ static String[] getMorphs(final Word thisWord) { final List theseMorphs = new ArrayList(); for (final Morphmap thisMorphmap : thisWord.getMorphmaps()) { theseMorphs.add(thisMorphmap.getMorph().getMorph()); } return theseMorphs.toArray(new String[theseMorphs.size()]); } /** * Flatten array of strings to one string * * @param theseStrings * array of strings * @return string */ static String array2String(final String[] theseStrings) { final StringBuffer thisBuffer = new StringBuffer(); int i = 0; for (final String thisString : theseStrings) { if (i++ > 0) { thisBuffer.append(' '); } thisBuffer.append(thisString); } return thisBuffer.toString(); } /** * Represent word as string * * @param thisWord * word * @return string */ @SuppressWarnings("boxing") static String word2String(final Word thisWord) { return String.format("%s #%d [%s]", //$NON-NLS-1$ thisWord.getLemma(), // thisWord.getWordid(), // Utils.array2String(Utils.getMorphs(thisWord)) // ); // } /** * Represent synset as string * * @param thisSynset * synset * @return string */ @SuppressWarnings("boxing") static String synset2String(final Synset thisSynset) { return String.format("%s %s #%d {%s} '%s'", //$NON-NLS-1$ thisSynset.getPos(), // thisSynset.getLexdomain().getLexdomainname(), // thisSynset.getSynsetid(), // Utils.array2String(Utils.getWords(thisSynset)), // thisSynset.getDefinition() // ); // } /** * Utils semantic link (x R y) * * @param thisSemlink * semantic link R * @param processTarget * whether to process link's target (y) or source (x) * @return string representing link */ static String semLink2String(final Semlink thisSemlink, final boolean processTarget) { final Synset thisSynset = processTarget ? thisSemlink.getSynsetBySynset2id() : thisSemlink.getSynsetBySynset1id(); return String.format("\t%s %s - %s", //$NON-NLS-1$ (processTarget ? "has" : "is"), //$NON-NLS-1$ //$NON-NLS-2$ thisSemlink.getLinktype().getLink(), // Utils.synset2String(thisSynset) // ); } /** * Utils lexical link (x R y) * * @param thisLexlink * lexical link R * @param processTarget * whether to process link's target (y) or source (x) * @return string representing link */ static String lexLink2String(final Lexlink thisLexlink, final boolean processTarget) { final Synset thisSynset = processTarget ? thisLexlink.getSynsetBySynset2id() : thisLexlink.getSynsetBySynset1id(); final Word thisWord = processTarget ? thisLexlink.getWordByWord2id() : thisLexlink.getWordByWord1id(); return String.format("\t%s %s - %s %s", //$NON-NLS-1$ (processTarget ? "has" : "is"), //$NON-NLS-1$ //$NON-NLS-2$ thisLexlink.getLinktype().getLink(), // thisWord.getLemma(), // Utils.synset2String(thisSynset) // ); } } /** * Main * * @param args * unused */ public static void main(final String[] args) { new WordQueryFromLemma("horse").run(); //$NON-NLS-1$ new WordQueryFromId(65264).run(); new WordGetter(65264).run(); new WordQueryFromLikeness("hors%").run(); //$NON-NLS-1$ new WordQueryFromLemma("mouse").run(); //$NON-NLS-1$ } }