Changeset 236

Show
Ignore:
Timestamp:
12/31/07 00:44:41 (1 year ago)
Author:
chris
Message:

Change HebrewConverter?.wordToHtml to wordHebrewToHtml() and
wordTranslitToHtml().

Change clause.jsp to use header2.jsp.

Add advanced search capability.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • lex/trunk/jsp/clause.jsp

    r211 r236  
     1<% String pageTitle = "Text Browser"; %> 
     2<%@ include file="header2.jsp" %> 
    13<%@ page import="java.util.*" %> 
    24<%@ page import="java.util.regex.*" %> 
     
    1113<%@ page import="net.didion.jwnl.data.POS" %> 
    1214<%@ page import="net.didion.jwnl.data.Synset" %> 
    13 <html> 
    14 <head> 
    15 <title>Lex: Text Browser</title> 
     15 
    1616<script type="text/javascript"><!-- 
    1717 
     
    4949         
    5050//--></script> 
    51 <link rel="stylesheet" href="style.css" /> 
    52 </head> 
     51 
    5352<style type="text/css"> 
    54         TABLE.tree TD { 
     53        TABLE.tree TD 
     54        { 
    5555                text-align: center; 
    5656        } 
    57         div.topmenu a.clause_jsp <%@ include file="hilite.inc" %> 
    5857</style> 
    59 <body onLoad="enableEditButton()"> 
    60  
    61 <%@ include file="header.jsp" %> 
     58 
    6259<%@ include file="auth.jsp" %> 
     60 
    6361<% 
    6462 
     
    446444                                        if (type.equals("word")) 
    447445                                        { 
    448                                                 String lexeme = HebrewConverter.wordToHtml(word,  
     446                                                String lexeme = HebrewConverter.wordTranslitToHtml(word,  
    449447                                                        generator); 
    450448                                                String part_of_speech = (String) 
     
    12781276                %> 
    12791277                </p> 
     1278                                 
     1279                <script type="text/javascript"><!-- 
     1280                        enableEditButton(); 
     1281                //--></script> 
    12801282                 
    12811283                <p>Linked logical structure: 
     
    12931295                        { 
    12941296                                MatchedObject word = sci.next().const_iterator().next(); 
    1295                                 value_text += HebrewConverter.wordToHtml(word, generator); 
     1297                                value_text += HebrewConverter.wordTranslitToHtml(word, generator); 
    12961298                                if (sci.hasNext()) 
    12971299                                { 
  • lex/trunk/jsp/navclause.jsp

    r222 r236  
    322322                                                word_iter.next().const_iterator().next(); 
    323323                                                 
    324                                         lexemes += HebrewConverter.wordToHtml(word, generator); 
     324                                        lexemes += HebrewConverter.wordTranslitToHtml(word, generator); 
    325325                                         
    326326                                        if (word_iter.hasNext())  
  • lex/trunk/src/com/qwirx/lex/hebrew/HebrewConverter.java

    r220 r236  
    4040        { 
    4141            char c = input.charAt(i); 
    42             if (c >= 0x80) 
    43             { 
    44                 output.append("&#" + (int)c + ";"); 
     42            if (c == '<') 
     43            { 
     44                output.append("&lt;"); 
     45            } 
     46            else if (c == '>') 
     47            { 
     48                output.append("&gt;"); 
     49            } 
     50            else if (c == '&') 
     51            { 
     52                output.append("&amp;"); 
    4553            } 
    4654            else 
     
    383391    } 
    384392 
     393    static class Hebrewator implements MorphemeHandler 
     394    { 
     395        private MatchedObject m_Word; 
     396        private StringBuffer  m_Output; 
     397         
     398        public Hebrewator(MatchedObject word, StringBuffer output) 
     399        { 
     400            m_Word   = word; 
     401            m_Output = output; 
     402        } 
     403         
     404        public void convert(String surface,  
     405            boolean lastMorpheme, String desc, 
     406            String morphNode) 
     407        { 
     408            String raw = m_Word.getEMdFValue(surface).getString(); 
     409            String hebrew = HebrewConverter.toHebrew(raw); 
     410            m_Output.append(hebrew); 
     411        } 
     412    } 
     413 
    385414    public static String wordToHtml(MatchedObject word, EmdrosDatabase emdros) 
    386415    throws IOException, DatabaseException, SAXException 
    387416    { 
    388         return wordToHtml(word, new HebrewMorphemeGenerator(emdros)); 
    389     } 
    390  
    391     public static String wordToHtml(MatchedObject word,  
     417        return wordTranslitToHtml(word, new HebrewMorphemeGenerator(emdros)); 
     418    } 
     419 
     420    public static String wordTranslitToHtml(MatchedObject word,  
    392421        HebrewMorphemeGenerator generator) 
    393422    { 
     
    397426        return toHtml(out.toString()); 
    398427    } 
     428 
     429    public static String wordHebrewToHtml(MatchedObject word,  
     430        HebrewMorphemeGenerator generator) 
     431    { 
     432        StringBuffer out = new StringBuffer(); 
     433        Hebrewator xlit = new Hebrewator(word, out); 
     434        generator.parse(word, xlit, false); 
     435        return toHtml(out.toString()); 
     436    } 
    399437} 
  • lex/trunk/src/com/qwirx/lex/Search.java

    r232 r236  
    66import java.util.Iterator; 
    77import java.util.List; 
     8import java.util.Set; 
    89 
    910import jemdros.FlatSheaf; 
     
    1617import jemdros.SheafConstIterator; 
    1718import jemdros.Straw; 
     19import jemdros.StrawConstIterator; 
    1820 
    1921import org.xml.sax.SAXException; 
     
    2628public class Search 
    2729{ 
    28     private String m_Query; 
     30    private int m_ResultCount = 0; 
     31    private int m_MaxResults = 100; 
    2932    private EmdrosDatabase m_Emdros; 
    3033     
    31     public Search(String query, EmdrosDatabase emdros) 
    32     { 
    33         m_Query  = query; 
     34    public Search(EmdrosDatabase emdros) 
     35    { 
    3436        m_Emdros = emdros; 
    3537    } 
    36      
     38 
    3739    private static class ResultBase 
    3840    { 
     
    4345    } 
    4446     
    45     public List<SearchResult> run()  
     47    public void setMaxResults(int limit) 
     48    { 
     49        m_MaxResults = limit; 
     50    } 
     51     
     52    public List<SearchResult> basic(String query)  
    4653    throws DatabaseException, IOException, SAXException, SQLException 
    4754    { 
     55        return advanced("[word " + 
     56            "lexeme = '"+query+"' OR " + 
     57            "lexeme = '"+query+"/' OR " + 
     58            "lexeme = '"+query+"[']"); 
     59    } 
     60     
     61    private void addToMonadSet(Sheaf sheaf, SetOfMonads set) 
     62    { 
     63        SheafConstIterator shci = sheaf.const_iterator(); 
     64         
     65        while (shci.hasNext()) 
     66        { 
     67            Straw straw = shci.next(); 
     68            StrawConstIterator swci = straw.const_iterator(); 
     69             
     70            while (swci.hasNext()) 
     71            { 
     72                MatchedObject object = swci.next(); 
     73                set.unionWith(object.getMonads()); 
     74            } 
     75        } 
     76    } 
     77     
     78    public List<SearchResult> advanced(String query)  
     79    throws DatabaseException, IOException, SAXException, SQLException 
     80    {     
    4881        HebrewMorphemeGenerator generator =  
    4982            new HebrewMorphemeGenerator(m_Emdros); 
     
    5285        ( 
    5386            "SELECT ALL OBJECTS IN " + 
    54             m_Emdros.getVisibleMonadString() + " " + 
     87            m_Emdros.getVisibleMonads().toString() + " " + 
    5588            "WHERE [verse GET book, chapter, verse, verse_label " + 
    56             "       [clause "+ 
    57             "        [word NORETRIEVE " + 
    58             "         lexeme = '"+m_Query+"' OR " + 
    59             "         lexeme = '"+m_Query+"/' OR " + 
    60             "         lexeme = '"+m_Query+"['" + 
    61             "        ]" + 
    62             "       ]" + 
    63             "      ]" 
     89            "       [clause " + query + "]]" 
    6490        ); 
    6591         
    6692        List<ResultBase> resultBases = new ArrayList<ResultBase>(); 
    6793        SetOfMonads powerSet = new SetOfMonads(); 
    68          
     94        SetOfMonads matchSet = new SetOfMonads(); 
    6995        SheafConstIterator sci = sheaf.const_iterator(); 
     96         
     97        m_ResultCount = 0; 
     98         
    7099        while (sci.hasNext()) 
    71100        { 
     
    78107            while (clause_iter.hasNext()) 
    79108            { 
     109                m_ResultCount++; 
     110                if (m_ResultCount > m_MaxResults) continue; // just count them 
     111                 
    80112                MatchedObject clause = 
    81113                    clause_iter.next().const_iterator().next(); 
     
    92124                resultBases.add(base); 
    93125                powerSet.unionWith(base.monads); 
     126                addToMonadSet(clause.getSheaf(), matchSet); 
    94127            } 
    95128        } 
     
    119152        FlatStraw fs = fsci.next(); 
    120153        FlatStrawConstIterator fwci = fs.const_iterator(); 
    121          
     154 
    122155        while (fwci.hasNext()) 
    123156        { 
     
    136169        { 
    137170            ResultBase base = i.next(); 
    138             String lexemes = ""; 
     171            String original = ""; 
     172            String translit = ""; 
    139173             
    140174            for (Iterator<MatchedObject> j = base.words.iterator(); j.hasNext();) 
     
    142176                MatchedObject word = j.next(); 
    143177                 
    144                 if (word.getEMdFValue("lexeme").toString().equals(m_Query)) 
     178                boolean isMatch = SetOfMonads.overlap(word.getMonads(), 
     179                    matchSet); 
     180                 
     181                if (isMatch) 
    145182                { 
    146                     lexemes += "<strong>"; 
     183                    original += "<strong>"; 
     184                    translit += "<strong>"; 
    147185                } 
    148186                 
    149                 lexemes += HebrewConverter.wordToHtml(word, generator); 
    150                  
    151                 if (word.getEMdFValue("lexeme").toString().equals(m_Query)) 
     187                original += HebrewConverter.wordHebrewToHtml  (word, generator); 
     188                translit += HebrewConverter.wordTranslitToHtml(word, generator); 
     189                 
     190                if (isMatch) 
    152191                { 
    153                     lexemes += "</strong>"; 
     192                    original += "</strong>"; 
     193                    translit += "</strong>"; 
    154194                } 
    155195         
    156                 lexemes += " "; 
     196                original += " "; 
     197                translit += " "; 
    157198            } 
    158199 
    159200            SearchResult result = new SearchResult(base.location,  
    160                 lexemes, base.url); 
     201                "<div class=\"hebrew\">"   + original + "</div>\n" + 
     202                "<div class=\"translit\">" + translit + "</div>\n", 
     203                base.url); 
    161204            results.add(result); 
    162205        } 
     
    164207        return results; 
    165208    } 
     209     
     210    public int getResultCount() { return m_ResultCount; } 
    166211     
    167212    public static class SearchResult 
  • lex/trunk/test/com/qwirx/lex/SearchTest.java

    r232 r236  
    11package com.qwirx.lex; 
    22 
     3import java.util.ArrayList; 
    34import java.util.Iterator; 
    45import java.util.List; 
     
    2728    public void setUp() throws Exception 
    2829    { 
    29         m_Emdros = Lex.getEmdrosDatabase("test", "test"); 
     30        m_Emdros = Lex.getEmdrosDatabase("chris", "test"); 
    3031    } 
    3132     
     
    3536    } 
    3637     
    37     private void assertSearchResultsMatch(String query) throws Exception 
    38     { 
    39         List<SearchResult> actualResults = new Search(query, m_Emdros).run(); 
     38    private void assertSearchResultsMatch(String query) 
     39    throws Exception 
     40    { 
     41        Search search = new Search(m_Emdros); 
     42        assertSearchResultsMatch(query, search, 100); 
     43    } 
     44     
     45    private void assertSearchResultsMatch(String query, int limit) 
     46    throws Exception 
     47    { 
     48        Search search = new Search(m_Emdros); 
     49        search.setMaxResults(limit); 
     50        assertSearchResultsMatch(query, search, limit); 
     51    } 
     52     
     53    private void assertSearchResultsMatch(String query, Search search,  
     54        int limit) 
     55    throws Exception 
     56    {     
     57        List<SearchResult> actualResults = search.basic(query); 
    4058        Iterator<SearchResult> actualIterator = actualResults.iterator(); 
    4159         
     
    4361            new HebrewMorphemeGenerator(m_Emdros); 
    4462         
    45         String getFeatures = "GET " + 
     63        Sheaf sheaf = m_Emdros.getSheaf 
     64        ( 
     65            "SELECT ALL OBJECTS IN " + 
     66            m_Emdros.getVisibleMonads().toString() + " " + 
     67            "WHERE [clause "+ 
     68            "       [word " + 
     69            "        lexeme = '"+query+"' OR " +  
     70            "        lexeme = '"+query+"/' OR " + 
     71            "        lexeme = '"+query+"[' " + 
     72            "       ]" + 
     73            "      ]" 
     74        ); 
     75 
     76        List<Integer> clauses = new ArrayList<Integer>(); 
     77         
     78        int count = 0; 
     79         
     80        SheafConstIterator sci = sheaf.const_iterator(); 
     81        while (sci.hasNext()) 
     82        { 
     83            count++; 
     84            if (count > limit) continue; 
     85 
     86            Straw straw = sci.next(); 
     87            MatchedObject clause = straw.const_iterator().next(); 
     88            clauses.add(new Integer(clause.getID_D())); 
     89        } 
     90         
     91        String mql = "SELECT ALL OBJECTS IN " + 
     92            m_Emdros.getVisibleMonads().toString() + " " + 
     93            "WHERE " + 
     94            "[verse GET book, chapter, verse, verse_label " + 
     95            " [clause "; 
     96         
     97        if (clauses.size() == 0) 
     98        { 
     99            mql += "self = -1"; // never matches 
     100        } 
     101        else 
     102        { 
     103            for (Iterator<Integer> i = clauses.iterator(); i.hasNext();) 
     104            { 
     105                Integer clauseId = i.next(); 
     106                mql += "self = " + clauseId.toString(); 
     107                if (i.hasNext()) 
     108                { 
     109                    mql += " OR "; 
     110                } 
     111            } 
     112        } 
     113 
     114        mql += "[word GET " + 
    46115            "lexeme, " + 
    47116            "phrase_dependent_part_of_speech, " + 
     
    51120            "graphical_verbal_ending, " + 
    52121            "graphical_nominal_ending, " + 
    53             "graphical_pron_suffix"; 
    54          
    55         Sheaf sheaf = m_Emdros.getSheaf 
    56         ( 
    57             "SELECT ALL OBJECTS IN " + 
    58             m_Emdros.getVisibleMonadString() + " " + 
    59             "WHERE [verse GET book, chapter, verse, verse_label " + 
    60             "       [clause "+ 
    61             "        [" + 
    62             "         [word FIRST " + 
    63             "          lexeme = '"+query+"' OR " +  
    64             "          lexeme = '"+query+"/' OR " + 
    65             "          lexeme = '"+query+"[' " + getFeatures + "] " +  
    66             "         [word " + getFeatures + "]* " + 
    67             "         [word LAST " + getFeatures + "] " + 
    68             "        ]" + 
    69             "        OR" + 
    70             "        [" + 
    71             "         [word FIRST " + getFeatures + "] " + 
    72             "         [word " + getFeatures + "]* " + 
    73             "         [word " + 
    74             "          lexeme = '"+query+"' OR " +  
    75             "          lexeme = '"+query+"/' OR " + 
    76             "          lexeme = '"+query+"[' " + getFeatures + "] " +  
    77             "         [word " + getFeatures + "]* " + 
    78             "         [word LAST " + getFeatures + "] " + 
    79             "        ]" + 
    80             "        OR" + 
    81             "        [" + 
    82             "         [word FIRST " + getFeatures + "] " + 
    83             "         [word " + getFeatures + "]* " + 
    84             "         [word LAST " + 
    85             "          lexeme = '"+query+"' OR " +  
    86             "          lexeme = '"+query+"/' OR " + 
    87             "          lexeme = '"+query+"[' " + getFeatures + "] " +  
    88             "        ]" + 
    89             "       ]"+ 
    90             "      ]"); 
    91               
    92         SheafConstIterator sci = sheaf.const_iterator(); 
     122            "graphical_pron_suffix]]]"; 
     123 
     124        sheaf = m_Emdros.getSheaf(mql); 
     125        sci = sheaf.const_iterator(); 
     126         
    93127        while (sci.hasNext()) 
    94128        { 
     
    104138                    clause_iter.next().const_iterator().next(); 
    105139 
    106                 String lexemes = ""; 
     140                String original = ""; 
     141                String translit = ""; 
    107142                 
    108                 StrawConstIterator word_iter = 
    109                     clause.getSheaf().const_iterator().next().const_iterator()
     143                SheafConstIterator word_iter =  
     144                    clause.getSheaf().const_iterator()
    110145                     
    111146                while (word_iter.hasNext()) 
    112147                { 
    113                     MatchedObject word = word_iter.next(); 
    114                      
    115                     if (word.getEMdFValue("lexeme").toString().equals(query)) 
     148                    MatchedObject word =  
     149                        word_iter.next().const_iterator().next(); 
     150                     
     151                    String lexeme = word.getEMdFValue("lexeme").getString(); 
     152                    boolean isMatch = lexeme.equals(query) || 
     153                        lexeme.equals(query + "/") || 
     154                        lexeme.equals(query + "["); 
     155                     
     156                    if (isMatch) 
    116157                    { 
    117                         lexemes += "<strong>"; 
     158                        original += "<strong>"; 
     159                        translit += "<strong>"; 
    118160                    } 
    119161                     
    120                     lexemes += HebrewConverter.wordToHtml(word, generator); 
    121                      
    122                     if (word.getEMdFValue("lexeme").toString().equals(query)) 
     162                    original += HebrewConverter.wordHebrewToHtml  (word, generator); 
     163                    translit += HebrewConverter.wordTranslitToHtml(word, generator); 
     164                     
     165                    if (isMatch) 
    123166                    { 
    124                         lexemes += "</strong>"; 
     167                        original += "</strong>"; 
     168                        translit += "</strong>"; 
    125169                    } 
    126170             
    127                     lexemes += " "; 
     171                    original += " "; 
     172                    translit += " "; 
    128173                } 
    129174 
     
    133178                assertEquals(verse.getEMdFValue("verse_label").getString(), 
    134179                    actual.getLocation()); 
    135                 assertEquals(lexemes, actual.getDescription()); 
    136                 assertEquals("clause.jsp?book=" +  
     180                assertEquals(actual.getLocation(),  
     181                    "<div class=\"hebrew\">" + original + "</div>\n" + 
     182                    "<div class=\"translit\">" + translit + "</div>\n", 
     183                    actual.getDescription()); 
     184                assertEquals(actual.getLocation(), 
     185                    "clause.jsp?book=" +  
    137186                    m_Emdros.getEnumConstNameFromValue("book_name_e", 
    138187                        verse.getEMdFValue("book").getInt()) + 
     
    143192                ); 
    144193            } 
    145         } 
    146          
    147         assertFalse(actualIterator.hasNext()); 
     194 
     195            assertEquals(count, search.getResultCount()); 
     196        } 
     197         
     198        assertFalse("Found " + (actualResults.size() - count + 1) + 
     199            " more results than expected", actualIterator.hasNext()); 
    148200    } 
    149201 
    150202    public void testSearchCode() throws Exception 
    151203    { 
    152         assertSearchResultsMatch("CMJM"); // noun 
    153         assertSearchResultsMatch("BR");   // verb 
    154         assertSearchResultsMatch("W");    // conjunction 
    155         assertSearchResultsMatch("foo");  // no match 
     204        assertSearchResultsMatch("CMJM", 1); // noun 
     205        assertSearchResultsMatch("CMJM");    // noun 
     206        assertSearchResultsMatch("BR");      // verb 
     207        assertSearchResultsMatch("W", 0);    // conjunction 
     208        assertSearchResultsMatch("W", 1);    // conjunction 
     209        assertSearchResultsMatch("W", 10);   // conjunction 
     210        assertSearchResultsMatch("W", 100);  // conjunction 
     211        assertSearchResultsMatch("foo", 0);  // no match 
     212        assertSearchResultsMatch("foo", 1);  // no match 
     213        assertSearchResultsMatch("foo");     // no match 
    156214    } 
    157215