root/lex/trunk/jsp/clause.jsp

Revision 299, 31.2 kB (checked in by chris, 7 months ago)

Factor Hebrew glossing morpheme handler out of clause.jsp into its own class.

Change CLM-PERF to CLM-SER2 as requested by Nicolai.

Line 
1 <% String pageTitle = "Text Browser"; %>
2 <%@ include file="header2.jsp" %>
3 <%@ page import="java.util.*" %>
4 <%@ page import="java.util.regex.*" %>
5 <%@ page import="java.net.*" %>
6 <%@ page import="java.sql.*" %>
7 <%@ page import="com.qwirx.lex.parser.*" %>
8 <%@ page import="com.qwirx.lex.morph.*" %>
9 <%@ page import="com.qwirx.lex.lexicon.*" %>
10 <%@ page import="com.qwirx.crosswire.kjv.KJV" %>
11 <%@ page import="org.crosswire.jsword.book.*" %>
12 <%@ page import="net.didion.jwnl.data.POS" %>
13 <%@ page import="net.didion.jwnl.data.Synset" %>
14
15 <script type="text/javascript"><!--
16
17         function enableEditButton()
18         {
19                 if (document.forms.changels == null)
20                 {
21                         return;
22                 }
23                
24                 var lsselect = document.forms.changels.lsid;
25                 var editform = document.forms.editls;
26                 if (editform == null || editform.submit == null)
27                 {
28                         return;
29                 }
30                        
31                 var sellsid  = -1;
32                 if (lsselect.selectedIndex > 0)
33                 {
34                         sellsid = lsselect.options[lsselect.selectedIndex].value;
35                 }       
36                
37                 editform.submit.disabled = (sellsid != editform.lsid.value);
38                 return true;
39         }
40
41         function enableChangeButton(button, oldValue, selectBox)
42         {
43                 if (button == null) return;
44                 var newValue = selectBox.options[selectBox.selectedIndex].value;
45                 button.disabled = (newValue == oldValue);
46                 return true;
47         }
48        
49 //--></script>
50
51 <style type="text/css">
52         TABLE.tree TD
53         {
54                 text-align: center;
55         }
56 </style>
57
58 <%@ include file="auth.jsp" %>
59
60 <%@ include file="navclause.jsp" %>
61
62 <%
63
64         Map phrase_functions = emdros.getEnumerationConstants
65                 ("phrase_function_e",false);
66
67         Map phrase_types = emdros.getEnumerationConstants
68                 ("phrase_type_e",false);
69                
70         Map parts_of_speech = emdros.getEnumerationConstants
71                 ("part_of_speech_e",false);
72
73         if (request.getParameter("savearg") != null)
74         {
75                 String phraseIdString = request.getParameter("phraseid");
76                 String newArg         = request.getParameter("newarg");
77                 int phraseId          = Integer.parseInt(phraseIdString);
78                 Change ch = emdros.createChange(EmdrosChange.UPDATE,
79                         "phrase", new int[]{phraseId});
80                 ch.setString("argument_name", newArg);
81                 ch.execute();
82         }
83
84         if (request.getParameter("changemr") != null &&
85                 request.getParameter("mr") != null)
86         {
87                 String phraseIdString = request.getParameter("pid");
88                 String newMRString    = request.getParameter("mr");
89                 int phraseId          = Integer.parseInt(phraseIdString);
90                 int newMR             = Integer.parseInt(newMRString);
91                 Change ch = emdros.createChange(EmdrosChange.UPDATE,
92                         "phrase", new int[]{phraseId});
93                 ch.setInt("macrorole_number", newMR);
94                 ch.execute();
95         }
96
97         Sheaf sheaf = emdros.getSheaf
98         (
99                 "SELECT ALL OBJECTS IN " +
100                 emdros.intersect(userTextAccessSet, min_m, max_m) +
101                 " WHERE [clause self = "+selClauseId+
102                 "       GET logical_struct_id, logical_structure "+
103                 "        [phrase GET phrase_type, phrase_function, argument_name, "+
104                 "                    type_id, macrorole_number "+
105                 "          [word GET lexeme, phrase_dependent_part_of_speech, " +
106                 "                    tense, stem, wordnet_gloss, wordnet_synset, " +
107                 "                    graphical_preformative, " +
108                 "                    graphical_locative, " +
109                 "                    graphical_lexeme, " +
110                 "                    graphical_pron_suffix, " +
111                 "                    graphical_verbal_ending, " +
112                 "                    graphical_root_formation, " +
113                 "                    graphical_nominal_ending, " +
114                 "                    person, number, gender, state, " +
115                 "                    surface_consonants, " +
116                 "                    suffix_person, suffix_number, suffix_gender " +
117                 "          ]"+
118                 "        ]"+
119                 "      ]"
120         );
121
122         class BorderTableRenderer extends TableRenderer
123         {
124             public String getTable(String contents)
125             {
126                 return "<table class=\"tree\" border>" + contents + "</table>\n";
127             }
128         }
129
130         BorderTableRenderer rend = new BorderTableRenderer();
131        
132         MatchedObject clause = null;
133         {
134                 SheafConstIterator sci = sheaf.const_iterator();
135                 if (sci.hasNext())
136                 {
137                         Straw straw = sci.next();
138                         StrawConstIterator swci = straw.const_iterator();
139                         if (swci.hasNext())
140                         {
141                                 clause = swci.next();
142                         }
143                 }
144         }
145                  
146         if (clause == null)
147         {
148                 %><p>Selected clause not found or access denied.</p><%
149         }
150         else
151         {
152                 String predicate_text = "";
153                 StringBuffer hebrewText = new StringBuffer();
154                 List<MorphEdge> morphEdges = new ArrayList<MorphEdge>();
155                 HebrewMorphemeGenerator generator = new HebrewMorphemeGenerator();
156                 BookData swordVerse = null;
157                
158                 /* Prescan to find the predicate lexeme and populate the chart */
159                
160                 {
161                         List<String[]> columns = new ArrayList<String[]>();
162                         SheafConstIterator phrases = clause.getSheaf().const_iterator();
163                         boolean isFirstWord = true;
164        
165                         while (phrases.hasNext())
166                         {
167                                 MatchedObject phrase =
168                                         phrases.next().const_iterator().next();
169        
170                                 String function_name = (String)( phrase_functions.get(
171                                         phrase.getEMdFValue("phrase_function").toString())
172                                 );
173        
174                                 SheafConstIterator words = phrase.getSheaf().const_iterator();
175                                
176                                 while (words.hasNext())
177                                 {
178                                         MatchedObject word = words.next().const_iterator().next();
179
180                                         String psp = (String)( parts_of_speech.get(
181                                                 word.getEMdFValue("phrase_dependent_part_of_speech").toString())
182                                         );
183                                                
184                                         HebrewGlossTableGeneratingMorphemeHandler hmh =
185                                                 new HebrewGlossTableGeneratingMorphemeHandler(
186                                                         columns, word, hebrewText,
187                                                         morphEdges, isFirstWord,
188                                                         !phrases.hasNext() && !words.hasNext());
189                                                
190                                         generator.parse(word, hmh, true, sql);
191                                         isFirstWord = false;
192                                        
193                                         if (!hmh.isMorpheme())
194                                         {                               
195                                                 hebrewText.append(" ");
196                                         }
197                                        
198                                         if (psp.equals("verb"))
199                                         {
200                                                 predicate_text =
201                                                         word.getEMdFValue("lexeme").getString();
202                                         }
203                                 }
204                         }
205                        
206                         %>
207
208                         <span class="hebrew"><%=
209                                 HebrewConverter.toHtml(hebrewText.toString())
210                         %></span>
211
212                         <table>
213                                 <% for (int row = 0; row < 2; row++) { %>
214                                 <tr>
215                                         <%
216                                         for (Iterator<String[]> i = columns.iterator();
217                                                 i.hasNext();)
218                                         {
219                                         %>
220                                                 <% String [] rows = i.next(); %>
221                                                 <td><%= rows[row] %></td>       
222                                         <%
223                                         }
224                                         %>
225                                 </tr>
226                                 <% } %>
227                                 <tr>
228                                         <td colspan="<%= columns.size() %>"><%
229                                         try
230                                         {
231                                                 swordVerse = KJV.getVerse(emdros, selBook, selChapNum,
232                                                         selVerseNum);
233                                                 %><em><%=
234                                                         OSISUtil.getCanonicalText(swordVerse.getOsisFragment())
235                                                 %></em><%
236                                         }
237                                         catch (Exception e)
238                                         {
239                                                 %>KJV Gloss not found (<%= e %>)<%
240                                         }               
241                                         %></td>
242                                 </tr>   
243                         </table>
244                         <%
245                 }
246
247                 %>
248                 <p>Predicate text is: <%=
249                         predicate_text
250                                 .replaceAll("<", "&lt;")
251                                 .replaceAll(">", "&gt;")
252                 %></p>
253                 <%
254                
255                 int numSyntacticMacroroles = -1;
256                 int currentLsId = clause.getEMdFValue("logical_struct_id").getInt();
257        
258                 String selLsIdString = request.getParameter("lsid");
259                 String structure = "";
260
261                 {       
262                         int selLsId = currentLsId;
263                
264                         String newLsString   = request.getParameter("newls");
265                         String lsSaveString  = request.getParameter("lssave");
266                
267                         if (request.getParameter("create") != null && newLsString != null)
268                         {
269                                 try
270                                 {
271                                         Change ch = sql.createChange(SqlChange.INSERT,
272                                                 "lexicon_entries", null);
273                                         ch.setString("Lexeme",    predicate_text);
274                                         ch.setString("Structure", newLsString);
275                                         ch.execute();
276                                         selLsId = ((SqlChange)ch).getInsertedRowId();
277                                         selLsIdString = "" + selLsId;
278                                         lsSaveString = "yes";
279                                 }
280                                 catch (DatabaseException ex)
281                                 {
282                                         %><%= ex %><%
283                                 }
284                                 finally
285                                 {
286                                         sql.finish();
287                                 }
288                         }
289                         else
290                         {               
291                                 try { selLsId = Integer.parseInt(selLsIdString); }
292                                 catch (Exception e) { /* do nothing, use default */ }
293                         }
294                
295                         if (selLsId != currentLsId && lsSaveString != null)
296                         {
297                                 Change ch = emdros.createChange(EmdrosChange.UPDATE,
298                                         "clause", new int[]{selClauseId});
299                                 ch.setInt("logical_struct_id", selLsId);
300                                 ch.execute();
301                                 currentLsId = selLsId;
302                         }
303                 }
304                
305                 try
306                 {
307                         PreparedStatement stmt = sql.prepareSelect
308                                 ("SELECT ID,Structure,Syntactic_Args " +
309                                  "FROM lexicon_entries WHERE ID = ?");
310                         stmt.setInt(1, currentLsId);
311                                
312                         ResultSet rs = sql.select();
313                                
314                         if (rs.next())
315                         {
316                                 int    thisLsId      = rs.getInt("ID");
317                                 String thisStructure = rs.getString("Structure");
318                                 int    thisNumSMRs   = rs.getInt("Syntactic_Args");
319
320                                 if (thisStructure != null)
321                                 {
322                                         structure = thisStructure;
323                                 }
324                                 numSyntacticMacroroles = thisNumSMRs;
325                         }
326                 }
327                 catch (DatabaseException ex)
328                 {
329                         %><%= ex %><%
330                 }
331                 finally
332                 {
333                         sql.finish();
334                 }
335        
336                 Vector argNames = new Vector();
337                 Hashtable argNamesHash = new Hashtable();
338
339                 Pattern varPat = Pattern.compile
340                         ("(?s)(?i)<([^>]*)>");
341                 Matcher m = varPat.matcher(structure);
342                 while (m.find())
343                 {
344                         String arg = m.group(1);
345                         if (argNamesHash.get(arg) != null)
346                                 continue;
347                         argNames.addElement(m.group(1));
348                         argNamesHash.put(arg, Boolean.TRUE);
349                 }
350                
351                 Hashtable variables = new Hashtable();
352                
353                 %>
354                 <p>
355                 <table border>
356                 <%
357                
358                 String [] object_types = new String [] {
359                         "word",
360                         "phrase"
361                 };
362                
363                 class Cell {
364                         String label, link, format, html;
365                         int columns;
366                         Vector subcells = new Vector();
367                 }
368
369                 // "ewg" stands for "edit word gloss"
370                 String ewgString = request.getParameter("ewg");
371                 int ewgId = -1;
372                 if (ewgString != null) {
373                         ewgId = Integer.parseInt(ewgString);
374                 }
375
376                 for (int objectNum = 0; objectNum < object_types.length;
377                         objectNum++)
378                 {
379                         String type = object_types[objectNum];
380                         Vector word_row = new Vector(), struct_row = new Vector();
381                         int column = 0;
382                        
383                         SheafConstIterator phrases = clause.getSheaf().const_iterator();
384                         while (phrases.hasNext())
385                         {
386                                 MatchedObject phrase =
387                                         phrases.next().const_iterator().next();
388                                 int first_col = column;
389                                 boolean canWriteToPhrase = emdros.canWriteTo(phrase);
390        
391                                 String function_name = (String)( phrase_functions.get(
392                                         phrase.getEMdFValue("phrase_function").toString())
393                                 );
394
395                                 String phrase_type = (String)( phrase_types.get(
396                                         phrase.getEMdFValue("phrase_type").toString())
397                                 );
398                                
399                                 SheafConstIterator words = phrase.getSheaf().const_iterator();
400                                 while (words.hasNext())
401                                 {
402                                         MatchedObject word =
403                                                 words.next().const_iterator().next();
404                                         column++;
405                                        
406                                         if (type.equals("word"))
407                                         {
408                                                 Lexeme lexeme = Lexeme.findOrBuild(sql, word);
409                                                
410                                                 String part_of_speech = word.getFeatureAsString(
411                                                         word.getEMdFValueIndex(
412                                                                 "phrase_dependent_part_of_speech"));
413                                                 boolean canWriteToWord = emdros.canWriteTo(word);
414                                                        
415                                                 Cell cell = new Cell();
416                                                 cell.label = lexeme.getTranslit();
417                                                 cell.columns = 1;
418                                                 word_row.addElement(cell);
419                                                
420                                                 int wid = word.getID_D();
421
422                                                 // lexicon gloss
423                                                 {
424                                                         Cell glossCell = new Cell();
425                                                         cell.subcells.add(glossCell);
426        
427                                                         if (ewgId == wid &&
428                                                                 request.getParameter("ewgs") != null)
429                                                         {
430                                                                 lexeme.setGloss(request.getParameter("gloss"));
431                                                                 lexeme.save();
432                                                                 lexeme = Lexeme.load(sql, word);
433                                                                 ewgId = -1;
434                                                         }
435                                                        
436                                                         String lexiconGloss = lexeme.getGloss();
437                                                        
438                                                         if (ewgId == wid)
439                                                         {
440                                                                 if (lexiconGloss == null)
441                                                                 {
442                                                                         lexiconGloss = "";
443                                                                 }
444                                                                 glossCell.html = "<form method=\"post\">\n" +
445                                                                         "<input type=\"hidden\" name=\"ewg\"" +
446                                                                         " value=\"" + wid + "\">\n" +
447                                                                         "<input name=\"gloss\" size=\"10\" value=\"" +
448                                                                         HebrewConverter.toHtml(lexiconGloss) +
449                                                                         "\">\n" +
450                                                                         "<input type=\"submit\" name=\"ewgs\""+
451                                                                         " value=\"Save\">\n" +
452                                                                         "</form>";
453                                                         }
454                                                         else
455                                                         {
456                                                                 if (lexiconGloss == null)
457                                                                 {
458                                                                         lexiconGloss = "(gloss)";
459                                                                 }
460                                                                 glossCell.html = "<a href=\"clause.jsp?ewg=" +
461                                                                         wid + "\">" + lexiconGloss + "</a>";
462                                                         }
463                                                 }
464
465                                                 // DiB lookup
466                                                 {
467                                                         Cell dictCell = new Cell();
468                                                         cell.subcells.add(dictCell);
469                                                         String gloss = KJV.getDibGloss(
470                                                                 word.getEMdFValue("lexeme").getString());
471                                                         if (gloss == null)
472                                                         {
473                                                                 dictCell.html = "";
474                                                         }
475                                                         else
476                                                         {
477                                                                 dictCell.html = "[DiB] " + gloss;
478                                                         }       
479                                                 }
480                                                                                                
481                                                 // Hebrew-English Dictionary lookup
482                                                 if (swordVerse != null)
483                                                 {
484                                                         Cell dictCell = new Cell();
485                                                         cell.subcells.add(dictCell);
486                                                         String gloss = KJV.getKingJamesGloss(swordVerse,
487                                                                 word.getEMdFValue("lexeme").getString());
488                                                         if (gloss == null)
489                                                         {
490                                                                 dictCell.html = "";
491                                                         }
492                                                         else
493                                                         {
494                                                                 dictCell.html = "[KJV] " + gloss;
495                                                         }       
496                                                 }
497                                         }
498                                 }
499                                
500                                 if (type.equals("phrase"))
501                                 {
502                                         Cell pCell    = new Cell();
503                                         pCell.label   = phrase.getEMdFValue("phrase_function").toString();
504                                         pCell.columns = column - first_col;
505                                         struct_row.addElement(pCell);
506
507                                         if (function_name != null)
508                                                 pCell.label = phrase_type + " (" + function_name + ")";
509                                        
510                                         if (phrase_type == null)
511                                                 continue;
512                                                
513                                         Cell mrCell = new Cell();
514                                         pCell.subcells.add(mrCell);
515                                         mrCell.html = "";
516
517                                         if (phrase_type.equals("VP"))
518                                         {
519                                                 String html = "Macroroles: ";
520                                                
521                                                 switch (numSyntacticMacroroles)
522                                                 {
523                                                         case -1: html += "MR? (unknown)"; break;
524                                                         case 0:  html += "MR0"; break;
525                                                         case 1:  html += "MR1"; break;
526                                                         case 2:  html += "MR2"; break;
527                                                         case 3:  html += "MR3"; break;
528                                                         default: html += "MR! (invalid)"; break;
529                                                 }
530                                                
531                                                 mrCell.html = html;
532                                         }
533                                         else if (phrase_type.equals("NP") ||
534                                                          phrase_type.equals("IrPronNP") ||
535                                                          phrase_type.equals("PersPronNP") ||
536                                                          phrase_type.equals("DemPronNP") ||
537                                                          phrase_type.equals("PropNP") ||
538                                                          phrase_type.equals("PP"))
539                                         {
540                                                 int oldMR = phrase.getEMdFValue("macrorole_number")
541                                                         .getInt();
542                                                 String formName = "mr_" + phrase.getID_D();
543                                                        
544                                                 StringBuffer html = new StringBuffer();
545                                                 html.append("<form name=\"" + formName + "\" " +
546                                                         "method=\"POST\">\n");
547                                                 html.append("<input type=\"hidden\" name=\"pid\" " +
548                                                         "value=\"" + phrase.getID_D() + "\">\n");
549                                                 html.append("<input type=\"hidden\" name=\"prev\" " +
550                                                         "value=\"" + oldMR + "\">\n");
551                                                 html.append("<select name=\"mr\" " +
552                                                         "onChange=\"return enableChangeButton(" +
553                                                         "changemr, "+oldMR+", mr)\">\n");
554                                                 html.append("<option "+((oldMR==-1)?"SELECTED":"")+
555                                                         " value=\"-1\">Unknown\n");
556                                                 html.append("<option "+((oldMR==0) ?"SELECTED":"")+
557                                                         " value=\"0\">None\n");
558                                                 html.append("<option "+((oldMR==1) ?"SELECTED":"")+
559                                                         " value=\"1\">1 (Actor)\n");
560                                                 html.append("<option "+((oldMR==2) ?"SELECTED":"")+
561                                                         " value=\"2\">2 (Undergoer)\n");
562                                                 html.append("</select>\n");
563                                                
564                                                 if (canWriteToPhrase)
565                                                 {
566                                                         html.append("<input type=\"submit\" "+
567                                                                 "name=\"changemr\" value=\"Change\">\n");
568                                                 }
569                                                
570                                                 html.append("</form>\n");
571
572                                                 html.append("<script type=\"text/javascript\"><!--\n");
573                                                 html.append("\tenableChangeButton(" +
574                                                         "document.forms."+formName+".changemr, "+
575                                                         oldMR+", "+
576                                                         "document.forms."+formName+".mr)\n");
577                                                 html.append("//--></script>\n");
578
579                                                 mrCell.html = html.toString();
580                                         }
581                                                
582                                         if (! phrase_type.equals("NP") &&
583                                                 ! phrase_type.equals("IrPronNP") &&
584                                                 ! phrase_type.equals("PersPronNP") &&
585                                                 ! phrase_type.equals("DemPronNP") &&
586                                                 ! phrase_type.equals("PropNP") &&
587                                                 ! phrase_type.equals("PP"))
588                                                 continue;
589
590                                         Cell varCell = new Cell();
591                                         pCell.subcells.add(varCell);
592
593                                         StringBuffer editHtml = new StringBuffer();
594                                          
595                                         String oldArg = phrase.getEMdFValue("argument_name")
596                                                 .toString();
597                                         String newArg = "";
598
599                                         if (oldArg.equals(""))
600                                         {
601                                                 // Argument not decided yet. Maybe we can guess
602                                                 // based on the "function" of the clause?
603                                                
604                                                 if (
605                                                         function_name.equals("Subj") ||
606                                                         function_name.equals("PreS") ||
607                                                         function_name.equals("IrpS") ||
608                                                         function_name.equals("ModS"))
609                                                 {
610                                                         newArg = "x";
611                                                 }
612                                                 else if (
613                                                         function_name.equals("Objc") ||
614                                                         function_name.equals("PreC") ||
615                                                         function_name.equals("PreO") ||
616                                                         function_name.equals("PtcO") ||
617                                                         function_name.equals("IrpO"))
618                                                 {
619                                                         newArg = "y";
620                                                 }
621                                                
622                                                 if (newArg != oldArg &&
623                                                         argNamesHash.get(newArg) != null)
624                                                 {
625                                                         if (canWriteToPhrase)
626                                                         {
627                                                                 Change ch = emdros.createChange(
628                                                                         EmdrosChange.UPDATE,
629                                                                         "phrase", new int[]{phrase.getID_D()});
630                                                                 ch.setString("argument_name", newArg);
631                                                                 ch.execute();
632                                                         }
633