Changeset 70

Show
Ignore:
Timestamp:
01/06/07 16:05:57 (2 years ago)
Author:
chris
Message:

Added parse.jsp to display and edit parser output

Files:

Legend:

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

    r26 r70  
     1<%@ page import="java.util.*" %> 
     2<%@ page import="com.qwirx.lex.ontology.*" %> 
     3<%@ page import="com.qwirx.lex.wordnet.*" %> 
     4<%@ page import="com.qwirx.lex.parser.*" %> 
     5 
     6<html> 
     7<head> 
     8        <title>Lex: Parser Testing</title> 
     9        <link rel="stylesheet" href="style.css"/> 
     10        <style> 
     11                div.topmenu a.parse_jsp <%@ include file="hilite.inc" %> 
     12                .hilite   { background-color: #fdd; } 
     13                .selected { background-color: #fee; } 
     14                .cb { float: right; } 
     15        </style> 
     16</head> 
     17         
     18<script language="javascript"> 
     19<!-- 
     20         
     21        var old_hilites = null; 
     22        var selected_state = new Array(); 
     23         
     24        function selected_class(state) 
     25        { 
     26                return state ? "selected" : ""; 
     27        } 
     28 
     29        function object_default_class(object) 
     30        { 
     31                var state = selected_state[object.id]; 
     32         
     33                if (state == null) 
     34                { 
     35                        state = false; 
     36                } 
     37         
     38                return selected_class(state); 
     39        } 
     40                 
     41        function highlight_recursive(object) 
     42        { 
     43                old_hilites[old_hilites.length] = object; 
     44                object.className = "hilite"; 
     45         
     46                var children = children_by_id[object.id]; 
     47         
     48                if (children == null) 
     49                { 
     50                        alert("object has no id: " + object); 
     51                        return; 
     52                } 
     53         
     54                for (var i = 0; i < children.length; i++) 
     55                { 
     56                        var child = document.getElementById(children[i]); 
     57                        if (child == null) 
     58                        { 
     59                                alert("no such child: " + children[i]); 
     60                        } 
     61                        else 
     62                        { 
     63                                highlight_recursive(child); 
     64                        } 
     65                } 
     66        } 
     67         
     68        function highlight(object) 
     69        { 
     70                unhighlight_all(); 
     71         
     72                highlight_recursive(object); 
     73 
     74                return true; 
     75        } 
     76 
     77        function unhighlight_all() 
     78        { 
     79                if (old_hilites != null) 
     80                { 
     81                        for (var i = 0; i < old_hilites.length; i++) 
     82                        { 
     83                                old_hilites[i].className = object_default_class(old_hilites[i]); 
     84                        } 
     85                } 
     86         
     87                old_hilites = new Array(); 
     88         
     89                return true; 
     90        } 
     91         
     92        function select_children_recursive(object, state) 
     93        { 
     94                selected_state[object.id] = state; 
     95                object.className = selected_class(state); 
     96         
     97                var children = children_by_id[object.id]; 
     98         
     99                if (children == null) 
     100                { 
     101                        alert("object has no id: " + object); 
     102                        return; 
     103                } 
     104         
     105                for (var i = 0; i < children.length; i++) 
     106                { 
     107                        var child = document.getElementById(children[i]); 
     108                        if (child == null) 
     109                        { 
     110                                alert("no such child: " + children[i]); 
     111                        } 
     112                        else 
     113                        { 
     114                                select_children_recursive(child, state); 
     115                        } 
     116                } 
     117        } 
     118         
     119        function on_checkbox_click(target_checkbox) 
     120        { 
     121                var table_cell = document.getElementById(target_checkbox.name); 
     122                select_children_recursive(table_cell, target_checkbox.checked); 
     123         
     124                var sel_left  = -1; 
     125                var sel_right = -1; 
     126                var any_checked = false; 
     127         
     128                for (var i = 0; i < edges.length; i++) 
     129                { 
     130                        var edge = edges[i]; 
     131                        var checkbox = document.forms.rulecmd[edge[0]]; 
     132         
     133                        if (checkbox.checked) 
     134                        { 
     135                                any_checked = true; 
     136         
     137                                if (sel_left == -1 || sel_left > edge[1]) 
     138                                { 
     139                                        sel_left = edge[1]; 
     140                                } 
     141                                if (sel_right == -1 || sel_right < edge[2]) 
     142                                { 
     143                                        sel_right = edge[2]; 
     144                                } 
     145                        } 
     146                } 
     147         
     148                for (var i = 0; i < edges.length; i++) 
     149                { 
     150                        var edge = edges[i]; 
     151                        var enabled = false; 
     152                        var checkbox = document.forms.rulecmd[edge[0]]; 
     153         
     154                        if (!any_checked) 
     155                        { 
     156                                // allow the first checkbox to be checked 
     157                                enabled = true; 
     158                        } 
     159                        else if (edge[1] == sel_right + 1 || edge[2] == sel_left - 1) 
     160                        { 
     161                                // allow expansion at the edges 
     162                                enabled = true; 
     163                        } 
     164                        else if (!checkbox.checked) 
     165                        { 
     166                                enabled = false; 
     167                        } 
     168                        else if (edge[1] == sel_left || edge[2] == sel_right) 
     169                        { 
     170                                // allow deselection at the left and right edges only 
     171                                enabled = true; 
     172                        } 
     173                         
     174                        checkbox.disabled = !enabled; 
     175                } 
     176         
     177                return true; 
     178        } 
     179         
     180//--> 
     181</script> 
     182         
     183<body> 
     184 
     185<%@ include file="header.jsp" %> 
     186<%@ include file="auth.jsp" %> 
     187<%@ include file="navclause.jsp" %> 
     188 
     189<% 
     190 
     191        Map phrase_functions = emdros.getEnumerationConstants 
     192                ("phrase_function_t",false); 
     193 
     194        Map phrase_types = emdros.getEnumerationConstants 
     195                ("phrase_type_t",false); 
     196                 
     197        Map parts_of_speech = emdros.getEnumerationConstants 
     198                ("psp_t",false); 
     199 
     200        Map verbal_stems = emdros.getEnumerationConstants 
     201                ("verbal_stem_t",false); 
     202 
     203        Map persons = emdros.getEnumerationConstants 
     204                ("person_t",false); 
     205 
     206        Map numbers = emdros.getEnumerationConstants 
     207                ("number_t",false); 
     208 
     209        Map genders = emdros.getEnumerationConstants 
     210                ("gender_t",false); 
     211 
     212        Map states = emdros.getEnumerationConstants 
     213                ("state_t",false); 
     214 
     215        Map tenses = emdros.getEnumerationConstants 
     216                ("verbal_tense_t",false); 
     217 
     218        Map stems = emdros.getEnumerationConstants 
     219                ("verbal_stem_t",false); 
     220 
     221        OntologyDb ontology = Lex.getOntologyDb(); 
     222 
     223        Wordnet wordnet = Wordnet.getInstance(); 
     224         
     225        class BorderTableRenderer extends TableRenderer 
     226        { 
     227            public String getTable(String contents) 
     228            { 
     229                return "<table class=\"tree\" border>" + contents + "</table>\n"; 
     230            } 
     231        } 
     232 
     233        BorderTableRenderer rend = new BorderTableRenderer(); 
     234         
     235        Sheaf sheaf = emdros.getSheaf 
     236        ( 
     237                "SELECT ALL OBJECTS IN " + 
     238                emdros.getMonadSet(userTextAccess, min_m, max_m) + 
     239                " WHERE [clause self = "+selClauseId+ 
     240                "       GET logical_struct_id, logical_structure "+ 
     241                "        [phrase GET phrase_type, function, argument_name, "+ 
     242                "                    type_id, macrorole_number "+ 
     243                "          [word GET lexeme, pdpsp, verbal_stem, verbal_tense, " + 
     244                "                    wordnet_gloss, wordnet_synset, " + 
     245                "                    graphical_preformative, " + 
     246                "                    graphical_locative, " + 
     247                "                    graphical_lexeme, " + 
     248                "                    graphical_pron_suffix, " + 
     249                "                    graphical_verbal_ending, " + 
     250                "                    graphical_root_formation, " + 
     251                "                    graphical_nominal_ending, " + 
     252                "                    person, number, gender, state " + 
     253                "          ]"+ 
     254                "        ]"+ 
     255                "      ]" 
     256        ); 
     257 
     258        MatchedObject clause = null; 
     259         
     260        { 
     261                SheafConstIterator sci = sheaf.const_iterator(); 
     262                if (sci.hasNext())  
     263                { 
     264                        Straw straw = sci.next(); 
     265                        StrawConstIterator swci = straw.const_iterator(); 
     266                        if (swci.hasNext())  
     267                        { 
     268                                clause = swci.next(); 
     269                        } 
     270                } 
     271        } 
     272 
     273        if (clause == null) 
     274        { 
     275                %><p>Selected clause not found or access denied.</p><% 
     276        } 
     277        else 
     278        { 
     279                StringBuffer hebrewText = new StringBuffer(); 
     280                List morphEdges = new ArrayList(); 
     281                 
     282                /* Prescan to list morpheme edges */ 
     283                 
     284                { 
     285                        TreeNode root = new TreeNode("root"); 
     286                         
     287                        { 
     288                                SheafConstIterator phrases = clause.getSheaf().const_iterator(); 
     289         
     290                                while (phrases.hasNext())  
     291                                { 
     292                                        MatchedObject phrase = 
     293                                                phrases.next().const_iterator().next(); 
     294         
     295                                        String function_name = (String)( phrase_functions.get( 
     296                                                phrase.getEMdFValue("function").toString()) 
     297                                        ); 
     298         
     299                                        SheafConstIterator words = phrase.getSheaf().const_iterator(); 
     300                                        while (words.hasNext())  
     301                                        { 
     302                                                MatchedObject word = words.next().const_iterator().next(); 
     303         
     304                                                String psp = (String)( parts_of_speech.get( 
     305                                                        word.getEMdFValue("pdpsp").toString())  
     306                                                ); 
     307                                                 
     308                                                class HebrewFeatureConverter 
     309                                                { 
     310                                                        private TreeNode m_root; 
     311                                                        private MatchedObject m_word; 
     312                                                        private StringBuffer m_hebrew; 
     313                                                        private List m_morphs; 
     314                                                         
     315                                                        public HebrewFeatureConverter(TreeNode root, 
     316                                                                MatchedObject word, StringBuffer hebrew, 
     317                                                                List morphs) 
     318                                                        { 
     319                                                                m_root   = root; 
     320                                                                m_word   = word; 
     321                                                                m_hebrew = hebrew; 
     322                                                                m_morphs = morphs; 
     323                                                        } 
     324                                                         
     325                                                        public void convert(String surface,  
     326                                                                boolean lastMorpheme, String desc, 
     327                                                                String morphNode) 
     328                                                        { 
     329                                                                String raw = m_word.getEMdFValue(surface).getString(); 
     330 
     331                                                                String hebrew = HebrewConverter.toHebrew(raw); 
     332                                                                m_hebrew.append(hebrew); 
     333 
     334                                                                String translit = HebrewConverter.toTranslit(raw); 
     335                                                                translit = HebrewConverter.toHtml(translit); 
     336                                                                if (translit.equals("")) translit = "&Oslash;"; 
     337                                                                if (!lastMorpheme) translit += "-"; 
     338                                                                TreeNode node = m_root.createChild(translit); 
     339 
     340                                                                node = node.createChild(raw); 
     341                                                                node.createChild(desc); 
     342                                                                 
     343                                                                m_morphs.add(new MorphEdge(morphNode,  
     344                                                                        translit, m_morphs.size())); 
     345                                                        } 
     346                                                } 
     347 
     348                                                HebrewFeatureConverter hfc =  
     349                                                        new HebrewFeatureConverter(root, word, hebrewText, 
     350                                                        morphEdges); 
     351                                                 
     352                                                String person = (String)persons.get( 
     353                                                        word.getEMdFValue("person").toString()); 
     354                                                if      (person.equals("pers_first"))  person = "1"; 
     355                                                else if (person.equals("pers_second")) person = "2"; 
     356                                                else if (person.equals("pers_third"))  person = "3"; 
     357                                                 
     358                                                String gender = ((String)genders.get( 
     359                                                        word.getEMdFValue("gender").toString() 
     360                                                        )).substring(0, 1); 
     361                                                 
     362                                                String number = ((String)numbers.get( 
     363                                                        word.getEMdFValue("number").toString() 
     364                                                        )).substring(0, 1); 
     365                                                 
     366                                                String state = (String)states.get( 
     367                                                        word.getEMdFValue("state").toString()); 
     368                                                 
     369                                                String gloss = word.getEMdFValue("wordnet_gloss") 
     370                                                        .getString(); 
     371                                                 
     372                                                if (gloss.equals("")) 
     373                                                { 
     374                                                        String lexeme = word.getEMdFValue("lexeme") 
     375                                                                .getString(); 
     376                                                                 
     377                                                        OntologyDb.OntologyEntry entry =  
     378                                                                ontology.getWordByLexeme(lexeme); 
     379                                                                 
     380                                                        if (entry != null) 
     381                                                        { 
     382                                                                gloss = entry.m_EnglishGloss; 
     383                                                        } 
     384                                                        else 
     385                                                        { 
     386                                                                gloss = null; 
     387                                                        } 
     388                                                } 
     389         
     390                                                if (psp.equals("verb")) 
     391                                                { 
     392                                                        hfc.convert("graphical_preformative", false, 
     393                                                                (String)tenses.get(word 
     394                                                                .getEMdFValue("verbal_tense").toString()), 
     395                                                                "V/TNS"); 
     396                                                        hfc.convert("graphical_root_formation", false, 
     397                                                                (String)stems.get(word 
     398                                                                .getEMdFValue("verbal_stem").toString()), 
     399                                                                "V/STM"); 
     400                                                        hfc.convert("graphical_lexeme", false, gloss, 
     401                                                                "V/LEX"); 
     402                                                        hfc.convert("graphical_verbal_ending", true, 
     403                                                                person + gender + number, "V/PGN"); 
     404                                                } 
     405                                                else if (psp.equals("noun") 
     406                                                        || psp.equals("proper_noun")) 
     407                                                { 
     408                                                        String type = "HEAD/NCOM"; 
     409                                                         
     410                                                        if (psp.equals("proper_noun")) 
     411                                                        { 
     412                                                                type = "HEAD/NPROP"; 
     413                                                        } 
     414                                                         
     415                                                        hfc.convert("graphical_lexeme", false, gloss, type); 
     416                                                        hfc.convert("graphical_nominal_ending", true, 
     417                                                                gender + number + "." + state, "MARK/N"); 
     418                                                } 
     419                                                else 
     420                                                { 
     421                                                        String type; 
     422 
     423                                                        if (psp.equals("adjective")) 
     424                                                        { 
     425                                                                type = "ADJ"; 
     426                                                        } 
     427                                                        else if (psp.equals("adverb")) 
     428                                                        { 
     429                                                                type = "ADV"; 
     430                                                        } 
     431                                                        else if (psp.equals("article")) 
     432                                                        { 
     433                                                                type = "DET"; 
     434                                                        } 
     435                                                        else if (psp.equals("conjunction")) 
     436                                                        { 
     437                                                                type = "CONJ"; 
     438                                                        } 
     439                                                        else if (psp.equals("demonstrative_pronoun")) 
     440                                                        { 
     441                                                                type = "PRON/DEM"; 
     442                                                        } 
     443                                                        else if (psp.equals("interjection")) 
     444                                                        { 
     445                                                                type = "INTJ"; 
     446                                                        } 
     447                                                        else if (psp.equals("interrogative")) 
     448                                                        { 
     449                                                                type = "INTR"; 
     450                                                        } 
     451                                                        else if (psp.equals("interrogative_pronoun")) 
     452                                                        { 
     453                                                                type = "PRON/INT"; 
     454                                                        } 
     455                                                        else if (psp.equals("negative")) 
     456                                                        { 
     457                                                                type = "NEG"; 
     458                                                        } 
     459                                                        else if (psp.equals("personal_pronoun")) 
     460                                                        { 
     461                                                                type = "PRON/PERS"; 
     462                                                        } 
     463                                                        else if (psp.equals("preposition")) 
     464                                                        { 
     465                                                                type = "P"; 
     466                                                        } 
     467                                                        else 
     468                                                        { 
     469                                                                throw new IllegalArgumentException("Unknown " + 
     470                                                                        "part of speech: " + psp); 
     471                                                        } 
     472                                                         
     473                                                        hfc.convert("graphical_lexeme", true, psp, type); 
     474                                                }        
     475                                                 
     476                                                hebrewText.append(" ");                                          
     477                                        } 
     478                                } 
     479                        } 
     480                 
     481                        %><p>Hebrew text: <span class="hebrew"><%=  
     482                                HebrewConverter.toHtml(hebrewText.toString()) 
     483                        %></span></p><% 
     484                } 
     485 
     486                { 
     487                        Parser p = new Parser(sql); 
     488                        p.setVerbose(true); 
     489                        Chart chart = p.parse(morphEdges); 
     490                        List sentences = chart.filter("SENTENCE", morphEdges, false); 
     491                         
     492                        if (sentences.size() == 0) 
     493                        { 
     494                                %> 
     495                                <h3>Parse failed</h3> 
     496                                <p>The edges found were:</p>  
     497                                <form name="rulecmd"> 
     498                                <% 
     499                                 
     500                                Map  edgeIds   = new Hashtable(); 
     501                                Map  idEdges   = new Hashtable(); 
     502                                Map  fakeChild = new Hashtable(); 
     503                                List depths    = new ArrayList(); 
     504                                List edges     = chart.getEdges(); 
     505                                int  maxDepth  = 0; 
     506                                 
     507                                for (ListIterator i = edges.listIterator(); i.hasNext(); ) 
     508                                { 
     509                                        Edge e = (Edge)( i.next() ); 
     510                                         
     511                                        int depth = e.getDepth(); 
     512                                         
     513                                        if (maxDepth < depth) 
     514                                        { 
     515                                                maxDepth = depth; 
     516                                        } 
     517                                         
     518                                        if (e instanceof MorphEdge) 
     519                                        { 
     520                                                MorphEdge me = (MorphEdge)e; 
     521                                                WordEdge  we = new WordEdge(me.getHtmlSurface(),  
     522                                                        me.getLeftPosition()); 
     523                                                i.add(we); 
     524                                                fakeChild.put(me, we); 
     525                                        } 
     526                                } 
     527 
     528                                for (ListIterator i = edges.listIterator(); i.hasNext(); ) 
     529                                { 
     530                                        Edge e = (Edge)( i.next() ); 
     531                                         
     532                                        int uniqueNum = 1; 
     533                                        String base = e.symbol().replaceAll("[^A-Za-z0-9]","_"); 
     534                                        String id = base + "_" + uniqueNum; 
     535                                         
     536                                        while (idEdges.get(id) != null) 
     537                                        { 
     538                                                uniqueNum++; 
     539                                                id = base + "_" + uniqueNum; 
     540                                        } 
     541                                         
     542                                        edgeIds.put(e, id); 
     543                                        idEdges.put(id, e); 
     544                                } 
     545 
     546                                /* 
     547                                %> 
     548                                <ul> 
     549                                <% 
     550                                 
     551                                for (Iterator i = edges.iterator(); i.hasNext(); ) 
     552                                { 
     553                                        Edge e = (Edge)( i.next() ); 
     554                                        %> 
     555                                        <ul> 
     556                                                [<%= e.getDepth() %>] <%= edgeIds.get(e) %> 
     557                                                <%= e.toString() %> 
     558                                        </ul> 
     559                                        <% 
     560                                } 
     561                                 
     562                                %> 
     563                                </ul> 
     564                                <% 
     565                                */ 
     566                                 
     567                                for (int i = 0; i <= maxDepth; i++) 
     568                                { 
     569                                        depths.add(new ArrayList()); 
     570                                } 
     571                                 
     572                                for (Iterator i = edges.iterator(); i.hasNext(); ) 
     573                                { 
     574                                        Edge e = (Edge)( i.next() ); 
     575                                        int  depth = e.getDepth(); 
     576                                        List edgesAtDepth = (List)( depths.get(depth) ); 
     577                                        edgesAtDepth.add(e); 
     578                                } 
     579                                 
     580                                %> 
     581                                <table border> 
     582                                <% 
     583                                 
     584                                class LeftComparator implements Comparator 
     585                                { 
     586                                        public int compare(Object o1, Object o2)  
     587                                        { 
     588                                                Edge e1 = (Edge)o1; 
     589                                                Edge e2 = (Edge)o2; 
     590                                                return e1.getLeftPosition() - e2.getLeftPosition(); 
     591                                        } 
     592                                } 
     593                                 
     594                                Comparator comp = new LeftComparator(); 
     595                                 
     596                                for (int i = maxDepth; i >= 0; i--) 
     597                                { 
     598                                        List edgesAtDepth = (List)( depths.get(i) ); 
     599                                        List remaining = new ArrayList(); 
     600                                        remaining.addAll(edgesAtDepth); 
     601                                        Collections.sort(remaining, comp); 
     602                                         
     603                                        if (remaining.size() != edgesAtDepth.size()) 
     604                                        { 
     605                                                throw new AssertionError("lost elements"); 
     606                                        } 
     607                                         
     608                                        while (remaining.size() > 0) 
     609                                        { 
     610                                                List added    = new ArrayList(); 
     611                                                List leftOver = new ArrayList(); 
     612                                                 
     613                                                %> 
     614                                                <tr> 
     615                                                <% 
     616                                                 
     617                                                int pos = 0; 
     618                                                 
     619                                                for (Iterator n = remaining.iterator(); n.hasNext(); ) 
     620                                                { 
     621                                                        Edge next = (Edge)( n.next() ); 
     622