From f968d42b559678f7649d86a8b439e27c657bffa7 Mon Sep 17 00:00:00 2001 From: eg3p Date: Thu, 3 Oct 2002 19:28:09 +0000 Subject: [PATCH] Andres Montano's Translation Tool code --- source/org/thdl/tib/scanner/AboutDialog.java | 119 ++++++ .../AlmostDefaultTableCellRenderer.java | 149 +++++++ .../thdl/tib/scanner/AppletScannerFilter.java | 264 ++++++++++++ .../thdl/tib/scanner/BinaryFileGenerator.java | 332 +++++++++++++++ .../tib/scanner/CachedSyllableListTree.java | 114 +++++ .../tib/scanner/ConsoleScannerFilter.java | 122 ++++++ source/org/thdl/tib/scanner/Definitions.java | 75 ++++ .../DictionaryListSelectionListener.java | 53 +++ .../thdl/tib/scanner/DictionarySource.java | 137 ++++++ .../org/thdl/tib/scanner/DictionaryTable.java | 106 +++++ .../tib/scanner/DictionaryTableModel.java | 125 ++++++ .../thdl/tib/scanner/DuffCellRenderer.java | 139 ++++++ .../thdl/tib/scanner/DuffScannerPanel.java | 203 +++++++++ .../tib/scanner/FileSyllableListTree.java | 161 +++++++ source/org/thdl/tib/scanner/Link.java | 105 +++++ source/org/thdl/tib/scanner/LinkedList.java | 117 +++++ source/org/thdl/tib/scanner/ListIterator.java | 49 +++ .../thdl/tib/scanner/LocalTibetanScanner.java | 398 ++++++++++++++++++ .../tib/scanner/MemorySyllableListTree.java | 192 +++++++++ .../thdl/tib/scanner/OnLineScannerFilter.java | 300 +++++++++++++ .../org/thdl/tib/scanner/PunctuationMark.java | 32 ++ .../thdl/tib/scanner/RemoteScannerFilter.java | 96 +++++ .../tib/scanner/RemoteTibetanScanner.java | 153 +++++++ source/org/thdl/tib/scanner/ScannerPanel.java | 179 ++++++++ .../thdl/tib/scanner/SimpleScannerPanel.java | 89 ++++ .../thdl/tib/scanner/SyllableListTree.java | 37 ++ .../org/thdl/tib/scanner/TibetanScanner.java | 43 ++ source/org/thdl/tib/scanner/Token.java | 28 ++ .../thdl/tib/scanner/WindowScannerFilter.java | 354 ++++++++++++++++ source/org/thdl/tib/scanner/Word.java | 122 ++++++ 30 files changed, 4393 insertions(+) create mode 100644 source/org/thdl/tib/scanner/AboutDialog.java create mode 100644 source/org/thdl/tib/scanner/AlmostDefaultTableCellRenderer.java create mode 100644 source/org/thdl/tib/scanner/AppletScannerFilter.java create mode 100644 source/org/thdl/tib/scanner/BinaryFileGenerator.java create mode 100644 source/org/thdl/tib/scanner/CachedSyllableListTree.java create mode 100644 source/org/thdl/tib/scanner/ConsoleScannerFilter.java create mode 100644 source/org/thdl/tib/scanner/Definitions.java create mode 100644 source/org/thdl/tib/scanner/DictionaryListSelectionListener.java create mode 100644 source/org/thdl/tib/scanner/DictionarySource.java create mode 100644 source/org/thdl/tib/scanner/DictionaryTable.java create mode 100644 source/org/thdl/tib/scanner/DictionaryTableModel.java create mode 100644 source/org/thdl/tib/scanner/DuffCellRenderer.java create mode 100644 source/org/thdl/tib/scanner/DuffScannerPanel.java create mode 100644 source/org/thdl/tib/scanner/FileSyllableListTree.java create mode 100644 source/org/thdl/tib/scanner/Link.java create mode 100644 source/org/thdl/tib/scanner/LinkedList.java create mode 100644 source/org/thdl/tib/scanner/ListIterator.java create mode 100644 source/org/thdl/tib/scanner/LocalTibetanScanner.java create mode 100644 source/org/thdl/tib/scanner/MemorySyllableListTree.java create mode 100644 source/org/thdl/tib/scanner/OnLineScannerFilter.java create mode 100644 source/org/thdl/tib/scanner/PunctuationMark.java create mode 100644 source/org/thdl/tib/scanner/RemoteScannerFilter.java create mode 100644 source/org/thdl/tib/scanner/RemoteTibetanScanner.java create mode 100644 source/org/thdl/tib/scanner/ScannerPanel.java create mode 100644 source/org/thdl/tib/scanner/SimpleScannerPanel.java create mode 100644 source/org/thdl/tib/scanner/SyllableListTree.java create mode 100644 source/org/thdl/tib/scanner/TibetanScanner.java create mode 100644 source/org/thdl/tib/scanner/Token.java create mode 100644 source/org/thdl/tib/scanner/WindowScannerFilter.java create mode 100644 source/org/thdl/tib/scanner/Word.java diff --git a/source/org/thdl/tib/scanner/AboutDialog.java b/source/org/thdl/tib/scanner/AboutDialog.java new file mode 100644 index 0000000..6626b61 --- /dev/null +++ b/source/org/thdl/tib/scanner/AboutDialog.java @@ -0,0 +1,119 @@ +/* +The contents of this file are subject to the AMP Open Community License +Version 1.0 (the "License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License on the AMP web site +(http://www.tibet.iteso.mx/Guatemala/). + +Software distributed under the License is distributed on an "AS IS" basis, +WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +License for the specific terms governing rights and limitations under the +License. + +The Initial Developer of this software is Andres Montano Pellegrini. Portions +created by Andres Montano Pellegrini are Copyright 2001 Andres Montano +Pellegrini. All Rights Reserved. + +Contributor(s): ______________________________________. +*/ + +package org.thdl.tib.scanner; + +import java.awt.*; +import java.awt.event.*; + +class AboutDialog extends Dialog implements ActionListener, WindowListener +{ + public AboutDialog(Frame parent) + { + super(parent, "About...", true); + Button close = new Button("Close this window"); + add(close, BorderLayout.NORTH); + close.addActionListener(this); + TextArea ta = new TextArea(TibetanScanner.aboutUnicode,0,0,TextArea.SCROLLBARS_VERTICAL_ONLY); + ta.setEditable(false); + addWindowListener(this); + add(ta, BorderLayout.CENTER); + setSize(240,300); // the size ipaq's window. + } + + public void actionPerformed(ActionEvent e) + { + hide(); + } + + /** + * Cierra la ventana. Invocado unicamente cuando el programa corre + * como aplicacion, para que el programa pare su ejecucion cuando + * el usuario cierre la ventana principal. + */ + public void windowClosing(WindowEvent e) + { + hide(); + } + + /** + * Sin cuerpo, no hace nada. Esta incluido solo para satisfacer + * la interfase WindowListener, la cual es implementada + * para tener el metodo windowClosing. + * + * @see #windowClosing + */ + public void windowActivated(WindowEvent e) + { + } + + /** + * Sin cuerpo, no hace nada. Esta incluido solo para satisfacer + * la interfase WindowListener, la cual es implementada + * para tener el metodo windowClosing. + * + * @see #windowClosing + */ + public void windowClosed(WindowEvent e) + { + } + + /** + * Sin cuerpo, no hace nada. Esta incluido solo para satisfacer + * la interfase WindowListener, la cual es implementada + * para tener el metodo windowClosing. + * + * @see #windowClosing + */ + public void windowDeactivated(WindowEvent e) + { + } + + /** + * Sin cuerpo, no hace nada. Esta incluido solo para satisfacer + * la interfase WindowListener, la cual es implementada + * para tener el metodo windowClosing. + * + * @see #windowClosing + */ + public void windowDeiconified(WindowEvent e) + { + } + + /** + * Sin cuerpo, no hace nada. Esta incluido solo para satisfacer + * la interfase WindowListener, la cual es implementada + * para tener el metodo windowClosing. + * + * @see #windowClosing + */ + public void windowIconified(WindowEvent e) + { + } + + /** + * Sin cuerpo, no hace nada. Esta incluido solo para satisfacer + * la interfase WindowListener, la cual es implementada + * para tener el metodo windowClosing. + * + * @see #windowClosing + */ + public void windowOpened(WindowEvent e) + { + } +} \ No newline at end of file diff --git a/source/org/thdl/tib/scanner/AlmostDefaultTableCellRenderer.java b/source/org/thdl/tib/scanner/AlmostDefaultTableCellRenderer.java new file mode 100644 index 0000000..0c29dd7 --- /dev/null +++ b/source/org/thdl/tib/scanner/AlmostDefaultTableCellRenderer.java @@ -0,0 +1,149 @@ +/* +The contents of this file are subject to the AMP Open Community License +Version 1.0 (the "License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License on the AMP web site +(http://www.tibet.iteso.mx/Guatemala/). + +Software distributed under the License is distributed on an "AS IS" basis, +WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +License for the specific terms governing rights and limitations under the +License. + +The Initial Developer of this software is Andres Montano Pellegrini. Portions +created by Andres Montano Pellegrini are Copyright 2001 Andres Montano +Pellegrini. All Rights Reserved. + +Contributor(s): ______________________________________. +*/ +package org.thdl.tib.scanner; + +import java.awt.*; +import javax.swing.table.TableCellRenderer; +import javax.swing.border.*; +import javax.swing.*; +import java.io.Serializable; + +/** Used by DictionaryTable to display multiple lines of + text (in Roman script) in a single cell. + + @author Andrés Montano Pellegrini + @see DictionaryTable +*/ +public class AlmostDefaultTableCellRenderer extends JTextArea + implements TableCellRenderer, Serializable +{ + + protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1); + + // We need a place to store the color the JTextArea should be returned + // to after its foreground and background colors have been set + // to the selection background color. + // These ivars will be made protected when their names are finalized. + private Color unselectedForeground; + private Color unselectedBackground; + + + public AlmostDefaultTableCellRenderer() + { + super(); + setLineWrap(true); + setWrapStyleWord(true); + + setOpaque(true); + setBorder(noFocusBorder); + } + + /** + * Overrides JComponent.setForeground to assign + * the unselected-foreground color to the specified color. + * + * @param c set the foreground color to this value + */ + public void setForeground(Color c) { + super.setForeground(c); + unselectedForeground = c; + } + + /** + * Overrides JComponent.setForeground to assign + * the unselected-background color to the specified color. + * + * @param c set the background color to this value + */ + public void setBackground(Color c) { + super.setBackground(c); + unselectedBackground = c; + } + + /** + * Notification from the UIManager that the look and feel + * [L&F] has changed. + * Replaces the current UI object with the latest version from the + * UIManager. + * + * @see JComponent#updateUI + */ + public void updateUI() { + super.updateUI(); + setForeground(null); + setBackground(null); + } + + + + public Component getTableCellRendererComponent(JTable table, Object value, + boolean isSelected, boolean hasFocus, int row, int column) + { + if (isSelected) + { + super.setForeground(table.getSelectionForeground()); + super.setBackground(table.getSelectionBackground()); + } + else + { + super.setForeground((unselectedForeground != null) ? unselectedForeground : table.getForeground()); + super.setBackground((unselectedBackground != null) ? unselectedBackground : table.getBackground()); + } + + if (hasFocus) { + setBorder( UIManager.getBorder("Table.focusCellHighlightBorder") ); + if (table.isCellEditable(row, column)) { + super.setForeground( UIManager.getColor("Table.focusCellForeground") ); + super.setBackground( UIManager.getColor("Table.focusCellBackground") ); + } + } else { + setBorder(noFocusBorder); + } + + + setValue(value); + + // ---- begin optimization to avoid painting background ---- + Color back = getBackground(); + boolean colorMatch = (back != null) && ( back.equals(table.getBackground()) ) && table.isOpaque(); + setOpaque(!colorMatch); + // ---- end optimization to aviod painting background ---- + + return this; + } + + /** + Este metodo no funciona, pero es llamado. + que hago para devolver el valor correcto! + */ + public int getLineCount(Object value, int width) + { + setValue(value); + setSize(width, Integer.MAX_VALUE); + return getLineCount(); + } + + + protected void setValue(Object value) + { + setText((value==null)? "" : value.toString()); + } + +} + + diff --git a/source/org/thdl/tib/scanner/AppletScannerFilter.java b/source/org/thdl/tib/scanner/AppletScannerFilter.java new file mode 100644 index 0000000..37a76ef --- /dev/null +++ b/source/org/thdl/tib/scanner/AppletScannerFilter.java @@ -0,0 +1,264 @@ +/* +The contents of this file are subject to the AMP Open Community License +Version 1.0 (the "License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License on the AMP web site +(http://www.tibet.iteso.mx/Guatemala/). + +Software distributed under the License is distributed on an "AS IS" basis, +WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +License for the specific terms governing rights and limitations under the +License. + +The Initial Developer of this software is Andres Montano Pellegrini. Portions +created by Andres Montano Pellegrini are Copyright 2001 Andres Montano +Pellegrini. All Rights Reserved. + +Contributor(s): ______________________________________. +*/ + +package org.thdl.tib.scanner; + +import java.awt.*; +import java.applet.Applet; +import javax.swing.*; +import javax.swing.text.JTextComponent; +import java.awt.event.*; +import java.awt.datatransfer.*; +import org.thdl.tib.input.DuffPane; +import org.thdl.tib.text.TibetanDocument; + +/** Inputs a Tibetan text and displays the words with + their definitions through through a graphical interfase using a + Browser over the Internet. The graphical interfase is provided + by implementations of the ScannerPanel. + +

Parameter URL should contain the URL of the + servlet which is going to handle to the + looking up of the words in the server.

+

Since the applet uses Swing for the THDL inputting + system, run the HTML file through Sun's Java Plug-in + HTML Converter to ensure that the browser will + use a JVM to run the applet.

+ + @author Andrés Montano Pellegrini + @see RemoteScannerFilter + @see ScannerPannel +*/ +public class AppletScannerFilter extends JApplet implements ActionListener, FocusListener, ItemListener +{ + private JMenuItem mnuSelectAll, aboutItem, mnuClear; // mnuCut, mnuCopy, mnuPaste, mnuDelete + private JCheckBoxMenuItem tibScript; + + // private JMenu mnuEdit; + private Object objModified; + private Dialog diagAbout; + ScannerPanel sp; + + public void init() + { + try { + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); + } + catch (Exception e) + { + } + String url = getParameter("url"); + if (url.indexOf("http://")<0) + { + url = getCodeBase() + url; + } + + // panel = new SimpleScannerPanel(url); + sp = new DuffScannerPanel(url); + sp.addFocusListener(this); + + setContentPane(sp); + + // setup the menu. Almost identical to WindowScannerFilter, but using swing. + JMenuBar mb = new JMenuBar(); + // mnuEdit + JMenu m = new JMenu ("Edit"); +/* mnuCut = new JMenuItem("Cut"); + mnuEdit.add(mnuCut); + mnuCut.addActionListener(this); + mnuCopy = new JMenuItem("Copy"); + mnuEdit.add(mnuCopy); + mnuCopy.addActionListener(this); + mnuPaste = new JMenuItem("Paste"); + mnuEdit.add(mnuPaste); + mnuPaste.addActionListener(this); + mnuDelete = new JMenuItem("Delete"); + mnuEdit.add(mnuDelete); + mnuDelete.addActionListener(this); + mnuEdit.addSeparator();*/ + mnuSelectAll = new JMenuItem("Select all"); + m.add(mnuSelectAll); + mnuSelectAll.addActionListener(this); + mnuClear = new JMenuItem("Clear all"); + m.add(mnuClear); + mnuClear.addActionListener(this); + mb.add(m); + + m = new JMenu("View"); + tibScript = new JCheckBoxMenuItem("Tibetan Script", true); + m.add(tibScript); + tibScript.addItemListener(this); + mb.add(m); + + //JMenuItem + aboutItem = new JMenuItem("About..."); + aboutItem.addActionListener(this); + + m = new JMenu("Help"); + m.add(aboutItem); + mb.add(m); + setJMenuBar(mb); + //mnuEdit.setEnabled(false); + } + + /** Added to update the Edit menu in dependence upon + which textbox the keyboard focus is at. + */ + public void focusGained(FocusEvent e) + { + objModified = e.getSource(); +/* boolean isEditable=false; + + if (objModified instanceof TextComponent) + { + TextComponent t = (TextComponent) objModified; + isEditable = t.isEditable(); + } + else if (objModified instanceof JTextComponent) + { + JTextComponent j = (JTextComponent) objModified; + isEditable = j.isEditable(); + } + + //mnuEdit.setEnabled(true); + mnuCut.setEnabled(isEditable); + if (isEditable) + { + if (Toolkit.getDefaultToolkit().getSystemClipboard().getContents(this)!=null) + mnuPaste.setEnabled(true); + } + else mnuPaste.setEnabled(false); + mnuDelete.setEnabled(isEditable);*/ + } + + /** Added to update the Edit menu in dependence upon + which textbox the keyboard focus is at. + */ + public void focusLost(FocusEvent e) + { + /* mnuEdit.setEnabled(false); + objModified=null;*/ + } + public void actionPerformed(ActionEvent e) + { + Object clicked = e.getSource(); +/* StringSelection ss; + String s = null; + int start, end;*/ + + if (clicked==aboutItem) + { + JOptionPane.showMessageDialog(AppletScannerFilter.this, TibetanScanner.aboutUnicode, "About", JOptionPane.PLAIN_MESSAGE); + } + else if (clicked == mnuClear) + { + sp.clear(); + } + else + { + if (objModified==null) return; + + if (objModified instanceof TextArea) + { + TextArea t = (TextArea) objModified; + + /*if (clicked == mnuCut) + { + ss = new StringSelection(t.getSelectedText()); + Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss,ss); + t.replaceRange("", t.getSelectionStart(), t.getSelectionEnd()); + } + else if (clicked == mnuCopy) + { + ss = new StringSelection(t.getSelectedText()); + Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss,ss); + } + else if (clicked == mnuPaste) + { + Transferable data = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(this); + try + { + s = (String)(data.getTransferData(DataFlavor.stringFlavor)); + } + catch (Exception ex) + { + s = data.toString(); + } + + JOptionPane.showMessageDialog(AppletScannerFilter.this, s, "About", JOptionPane.PLAIN_MESSAGE); + t.insert(s, t.getCaretPosition()); + } + else if (clicked == mnuDelete) + { + t.replaceRange("", t.getSelectionStart(), t.getSelectionEnd()); + } + else*/ if (clicked == mnuSelectAll) + { + t.selectAll(); + } + } + else if (objModified instanceof DuffPane) + { + DuffPane t = (DuffPane) objModified; + + /*if (clicked == mnuCut) + { + t.copy(t.getSelectionStart(), t.getSelectionEnd(), true); + } + else if (clicked == mnuCopy) + { + t.copy(t.getSelectionStart(), t.getSelectionEnd(), false); + } + else if (clicked == mnuPaste) + { + Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); + Transferable data = clipboard.getContents(this); + + s = null; + try { + s = (String)(data.getTransferData(DataFlavor.stringFlavor)); + } + catch (Exception ex) { + s = data.toString(); + } + JOptionPane.showMessageDialog(AppletScannerFilter.this, s, "About", JOptionPane.PLAIN_MESSAGE); + t.paste(t.getCaret().getDot()); + } + else if (clicked == mnuDelete) + { + try + { + ((TibetanDocument)t.getDocument()).remove(t.getSelectionStart(), t.getSelectionEnd()); + } + catch (Exception ex) + { + System.out.println(ex); + } + } + else*/ if (clicked == mnuSelectAll) + { + t.selectAll(); + } + } + } + } + public void itemStateChanged(ItemEvent e) + { + sp.setEnableTibetanScript(e.getStateChange()==ItemEvent.SELECTED); + } +} diff --git a/source/org/thdl/tib/scanner/BinaryFileGenerator.java b/source/org/thdl/tib/scanner/BinaryFileGenerator.java new file mode 100644 index 0000000..d55e81d --- /dev/null +++ b/source/org/thdl/tib/scanner/BinaryFileGenerator.java @@ -0,0 +1,332 @@ +/* +The contents of this file are subject to the AMP Open Community License +Version 1.0 (the "License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License on the AMP web site +(http://www.tibet.iteso.mx/Guatemala/). + +Software distributed under the License is distributed on an "AS IS" basis, +WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +License for the specific terms governing rights and limitations under the +License. + +The Initial Developer of this software is Andres Montano Pellegrini. Portions +created by Andres Montano Pellegrini are Copyright 2001 Andres Montano +Pellegrini. All Rights Reserved. + +Contributor(s): ______________________________________. +*/ +package org.thdl.tib.scanner; + +import java.io.*; + +/** Converts Tibetan dictionaries stored in text files + into a binary file tree structure format, to be used + by some implementations of the SyllableListTree. + +

The text files must be in the format used by the + The Rangjung Yeshe Tibetan-English Dictionary of Buddhist Culture.

+ + @author Andrés Montano Pellegrini + @see SyllableListTree + @see FileSyllableListTree + @see CachedSyllableListTree +*/ +public class BinaryFileGenerator extends LinkedList +{ + private long posHijos; + private String sil, def[]; + private static char delimiter; + + /** Number of dictionary. If 0, partial word (no definition). + */ + private DictionarySource sourceDef; + public static RandomAccessFile wordRaf; + private static RandomAccessFile defRaf; + + static + { + wordRaf = null; + defRaf = null; + delimiter = '-'; + } + + public BinaryFileGenerator() + { + super(); + sil = null; + def = null; + posHijos=-1; + sourceDef = null; + } + + public BinaryFileGenerator(String sil, String def, int numDef) + { + super(); + int marker = sil.indexOf(" "); + this.sourceDef = new DictionarySource(); + + if (marker<0) + { + this.sil = sil; + this.def = new String[1]; + this.def[0] = def; + this.sourceDef.add(numDef); + } + else + { + this.sil = sil.substring(0, marker); + this.def = null; + addLast(new BinaryFileGenerator(sil.substring(marker+1).trim(), def, numDef)); + } + posHijos=-1; + } + + public String toString() + { + return sil; + } + + private static String deleteQuotes(String s) + { + int length = s.length(); + if (length>2) + { + if ((s.charAt(0)=='\"') && (s.charAt(length-1)=='\"')) + return s.substring(1,length-2); + } + return s; + } + + public void addFile(String archivo, int defNum) throws Exception + { + int marker, linea; + BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(archivo))); + String entrada, s1, s2; + + linea=1; + while ((entrada = br.readLine())!=null) + { + entrada = entrada.trim(); + if (!entrada.equals("")) + { + marker = entrada.indexOf(delimiter); + if (marker<0) + { + System.out.println("Error loading line " + linea + ", in file " + archivo + ":"); + System.out.println(entrada); + } + else + { + s1 = deleteQuotes(entrada.substring(0,marker).trim()); + s2 = deleteQuotes(entrada.substring(marker+1).trim()); + add(s1, s2 , defNum); + } + } + linea++; + } + } + + + private void add(String word, String def, int defNum) + { + Link link, newLink; + BinaryFileGenerator ultimo; + String firstSillable; + int pos, marker = word.indexOf(" "), comp; + + if (marker<0) + firstSillable = word; + else firstSillable = word.substring(0,marker); + + /* usa orden alfabetico */ + if (isEmpty() || ((comp = firstSillable.compareTo((ultimo = (BinaryFileGenerator) getLast()).toString()))<0)) + { + super.addLast(new BinaryFileGenerator(word, def, defNum)); + } + else + { + if (comp==0) + if (marker<0) ultimo.addMoreDef(def, defNum); + else ultimo.add(word.substring(marker+1).trim(), def, defNum); + else + { + link = cabeza; + while(link.siguiente!=null) + { + comp = firstSillable.compareTo(link.siguiente.toString()); + if (comp<0) + { + newLink = new Link(new BinaryFileGenerator(word, def, defNum)); + newLink.siguiente = link.siguiente; + link.siguiente = newLink; + return; + } + else + if (comp==0) + { + ultimo = (BinaryFileGenerator) link.siguiente.get(); + if (marker<0) ultimo.addMoreDef(def, defNum); + else ultimo.add(word.substring(marker+1).trim(), def, defNum); + return; + } + link = link.siguiente; + } + newLink = new Link(new BinaryFileGenerator(word, def, defNum)); + link.siguiente = newLink; + } + } + } + + private void addMoreDef(String def, int numDef) + { + if (this.def==null) + { + this.def = new String[1]; + this.def[0] = def; + sourceDef.add(numDef); + } + else + { + // if the word is repeated in the same dictionary + if (sourceDef.contains(numDef)) + this.def[this.def.length-1] = this.def[this.def.length-1] + ". " + def; + else + { + int i=0; + String newDef[] = new String[this.def.length+1]; + while(i2) + { + printSintax(); + return; + } + sl.addFile(args[1] + ".txt",0); + a=1; + } + else + { + a=0; + if (args.length==1) + { + sl.addFile(args[0] + ".txt",0); + } + else + { + i=1; + + while(i< args.length) + { + if (args[i].charAt(0)=='-') + { + if (args[i].equals("-tab")) + delimiter='\t'; + else + delimiter=args[i].charAt(1); + i++; + } + else delimiter='-'; + sl.addFile(args[i] + ".txt", n); + n++; i++; + } + } + } + File wordF = new File(args[a] + ".wrd"), defF = new File(args[a] + ".def"); + wordF.delete(); + defF.delete(); + wordRaf = new RandomAccessFile(wordF,"rw"); + defRaf = new RandomAccessFile(defF,"rw"); + sl.print(); + wordRaf.writeInt((int)sl.posHijos); + } +} \ No newline at end of file diff --git a/source/org/thdl/tib/scanner/CachedSyllableListTree.java b/source/org/thdl/tib/scanner/CachedSyllableListTree.java new file mode 100644 index 0000000..3b06fea --- /dev/null +++ b/source/org/thdl/tib/scanner/CachedSyllableListTree.java @@ -0,0 +1,114 @@ +/* +The contents of this file are subject to the AMP Open Community License +Version 1.0 (the "License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License on the AMP web site +(http://www.tibet.iteso.mx/Guatemala/). + +Software distributed under the License is distributed on an "AS IS" basis, +WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +License for the specific terms governing rights and limitations under the +License. + +The Initial Developer of this software is Andres Montano Pellegrini. Portions +created by Andres Montano Pellegrini are Copyright 2001 Andres Montano +Pellegrini. All Rights Reserved. + +Contributor(s): ______________________________________. +*/ +package org.thdl.tib.scanner; + +import java.io.*; + +/** Provides recommended implementation of the SyllableListTree + (currently most efficient memory-speed combination) loading + into memory only the "trunk" of the tree, and + searching the rest of the tree directly in the file. + +

The words must be stored in a binary file tree structure format. + This can be done using the BinaryFileGenerator.

+ + @author Andrés Montano Pellegrini + @see TibetanScanner + @see BinaryFileGenerator +*/ +public class CachedSyllableListTree implements SyllableListTree +{ + SyllableListTree syllables[]; + + public CachedSyllableListTree(String archivo) throws Exception + { + String sil; + long pos, defSources[]; + DictionarySource sourceDef; + int i; + FileSyllableListTree.openFiles(archivo); + long posLista = FileSyllableListTree.wordRaf.length() - 4; + LinkedList syllables = new LinkedList(); + + FileSyllableListTree.wordRaf.seek(posLista); + posLista = (long) FileSyllableListTree.wordRaf.readInt(); + FileSyllableListTree.wordRaf.seek(posLista); + do + { + pos = (long) FileSyllableListTree.wordRaf.readInt(); + sil = FileSyllableListTree.wordRaf.readUTF(); + sourceDef = DictionarySource.read(FileSyllableListTree.wordRaf); + if (sourceDef.isEmpty()) defSources = null; + else + { + defSources = new long[sourceDef.countDefs()]; + for (i=0; i= 0) + in = new BufferedReader(new InputStreamReader(new BufferedInputStream((new URL(args[1])).openStream()))); + else + in = new BufferedReader(new InputStreamReader(new FileInputStream(args[1]))); + } + + new ConsoleScannerFilter(args[0], in, out).run(); + } + + public void printIfSomething(String s) + { + if (s!=null && !s.equals("")) + { + out.println(s); + out.flush(); + } + } + + public void printWords() + { + LinkedList words = scanner.getTokenLinkedList(); + ListIterator i = words.listIterator(); + Token token; + while (i.hasNext()) + { + token = (Token)i.next(); + if (token instanceof Word) + out.println(token); + } + out.flush(); + } + + public void run() throws Exception + { + String inStr, outStr; + + while ((inStr=in.readLine())!=null) + { + if (inStr.equals("")) + scanner.finishUp(); + else + scanner.scanBody(inStr); + + printWords(); + scanner.clearTokens(); + } + scanner.finishUp(); + printWords(); + scanner.clearTokens(); + } +} + diff --git a/source/org/thdl/tib/scanner/Definitions.java b/source/org/thdl/tib/scanner/Definitions.java new file mode 100644 index 0000000..bb1f968 --- /dev/null +++ b/source/org/thdl/tib/scanner/Definitions.java @@ -0,0 +1,75 @@ +/* +The contents of this file are subject to the AMP Open Community License +Version 1.0 (the "License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License on the AMP web site +(http://www.tibet.iteso.mx/Guatemala/). + +Software distributed under the License is distributed on an "AS IS" basis, +WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +License for the specific terms governing rights and limitations under the +License. + +The Initial Developer of this software is Andres Montano Pellegrini. Portions +created by Andres Montano Pellegrini are Copyright 2001 Andres Montano +Pellegrini. All Rights Reserved. + +Contributor(s): ______________________________________. +*/ + +/** Stores the multiple definitions (corresponding to + various dictionaries) for a single Tibetan word. + + @author Andrés Montano Pellegrini +*/ +package org.thdl.tib.scanner; + +public class Definitions +{ + public String[] def; + public int[] source; + public static String[] defTags; + + static + { + defTags=null; + } + + public static void setTags(String tags[]) + { + defTags = tags; + } + + public Definitions(String[] def, int[] source) + { + this.def = def; + this.source = source; + } + + public Definitions(String def) + { + source = null; + this.def = new String[1]; + this.def[0] = def; + } + + public String getTag(int i) + { + if (source==null) return null; + if (defTags==null) return Integer.toString(source[i]+1); + return defTags[source[i]]; + } + + public String toString() + { + int i; + String s; + if (def==null) return null; + if (source==null) return def[0]; + + s = "(" + getTag(0) + ") " + def[0]; + for (i=1; i0; + } + + public void add(int dict) + { + dicts|= getBits(dict); + } + + /** Write to file using BinaryFileGenerator */ + public void print(boolean hasNext, DataOutput raf) throws IOException + { + int numDict; + if (hasNext) numDict = lastBit | dicts; + else numDict = dicts; + raf.writeShort(numDict); + } + + public static DictionarySource read(DataInput raf) throws IOException + { + DictionarySource ds = new DictionarySource(); + ds.setDicts(raf.readShort()); + return ds; + } + + public boolean hasBrothers() + { + return (dicts & lastBit)>0; + } + + public int countDefs() + { + int n, source; + for (n=0, source = dicts & allDicts; source>0; source>>=1) + if (source%2==1) n++; + return n; + } + + public DictionarySource intersection(DictionarySource dsO) + { + DictionarySource ds = new DictionarySource(); + ds.setDicts(this.dicts & dsO.dicts); + return ds; + } + + public int[] untangleDefs(int n) + { + int arr[], i, pos, source; + arr = new int[n]; + for (i=0, pos=0, source=dicts & allDicts; pos>=1) + if (source%2==1) + arr[pos++]=i; + return arr; + } + + public int[] untangleDefs() + { + return untangleDefs(countDefs()); + } + + public boolean isEmpty() + { + return (dicts & allDicts)<=0; + } + + public void reset() + { + dicts = 0; + } +} \ No newline at end of file diff --git a/source/org/thdl/tib/scanner/DictionaryTable.java b/source/org/thdl/tib/scanner/DictionaryTable.java new file mode 100644 index 0000000..631a4c1 --- /dev/null +++ b/source/org/thdl/tib/scanner/DictionaryTable.java @@ -0,0 +1,106 @@ +/* +The contents of this file are subject to the AMP Open Community License +Version 1.0 (the "License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License on the AMP web site +(http://www.tibet.iteso.mx/Guatemala/). + +Software distributed under the License is distributed on an "AS IS" basis, +WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +License for the specific terms governing rights and limitations under the +License. + +The Initial Developer of this software is Andres Montano Pellegrini. Portions +created by Andres Montano Pellegrini are Copyright 2001 Andres Montano +Pellegrini. All Rights Reserved. + +Contributor(s): ______________________________________. +*/ +package org.thdl.tib.scanner; + +import javax.swing.*; +import javax.swing.table.*; +import javax.swing.event.*; +import java.awt.*; + +/** Table of two columns that displays a Tibetan word or + phrase (in either Tibetan or Roman script) and the + first couple of lines of its definitions. Clicking on + a word displays full definition on a separate text box. + + @author Andrés Montano Pellegrini +*/ +public class DictionaryTable extends JTable +{ + private DuffScannerPanel padre; + private int tibetanHeight, normalHeight; + private TableCellRenderer duffRenderer, normalRenderer; + public DictionaryTable(DictionaryTableModel td, TextArea fullDef) + { + this.setModel(td); + this.setRowHeight(40); + this.setColumnSelectionAllowed(false); + this.setRowSelectionAllowed(true); + //tableHeader = null; + + setSelectionMode(ListSelectionModel.SINGLE_SELECTION); + + ListSelectionModel rowSM = getSelectionModel(); + rowSM.addListSelectionListener(new DictionaryListSelectionListener(td, fullDef)); + + TableColumnModel tcm = this.getColumnModel(); + normalRenderer = new AlmostDefaultTableCellRenderer(); + duffRenderer = new DuffCellRenderer(); + + TableColumn tc = tcm.getColumn(0); + tc.setPreferredWidth(10); + tc.setCellRenderer(normalRenderer); + tc.setHeaderValue("Word"); + + tc = tcm.getColumn(1); + tc.setCellRenderer(normalRenderer); + tc.setHeaderValue("Definition"); + tibetanHeight = normalHeight = -1; + } + + public void resizeRows() + { + int i, n = getRowCount(), visibleHeight, width; + JLabel jl; + AlmostDefaultTableCellRenderer ja; + FontMetrics fm; + + if (n>0) + { + ja = (AlmostDefaultTableCellRenderer) normalRenderer; + if (tibetanHeight<0) + { + jl = (JLabel) duffRenderer; + fm = jl.getFontMetrics(jl.getFont()); + tibetanHeight = fm.getHeight(); + fm = ja.getFontMetrics(ja.getFont()); + normalHeight = fm.getHeight(); + } + + width = getColumnModel().getColumn(1).getWidth(); + + for (i=0; i tibetanHeight) + setRowHeight(i, visibleHeight); + } + } + } + + public void activateTibetan(boolean activate) + { + TableColumnModel tcm = this.getColumnModel(); + TableColumn tc = tcm.getColumn(0); + if (activate) + tc.setCellRenderer(duffRenderer); + else + tc.setCellRenderer(normalRenderer); + DictionaryTableModel dtm = (DictionaryTableModel) getModel(); + dtm.activateTibetan(activate); + } +} \ No newline at end of file diff --git a/source/org/thdl/tib/scanner/DictionaryTableModel.java b/source/org/thdl/tib/scanner/DictionaryTableModel.java new file mode 100644 index 0000000..1202b77 --- /dev/null +++ b/source/org/thdl/tib/scanner/DictionaryTableModel.java @@ -0,0 +1,125 @@ +/* +The contents of this file are subject to the AMP Open Community License +Version 1.0 (the "License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License on the AMP web site +(http://www.tibet.iteso.mx/Guatemala/). + +Software distributed under the License is distributed on an "AS IS" basis, +WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +License for the specific terms governing rights and limitations under the +License. + +The Initial Developer of this software is Andres Montano Pellegrini. Portions +created by Andres Montano Pellegrini are Copyright 2001 Andres Montano +Pellegrini. All Rights Reserved. + +Contributor(s): ______________________________________. +*/ +package org.thdl.tib.scanner; + +import javax.swing.*; +import javax.swing.table.*; +import org.thdl.tib.text.TibetanDocument; +import org.thdl.tib.text.TibetanDocument.DuffData; + +/** Stores the words being displayed in a DictionaryTable. + + @author Andrés Montano Pellegrini + @see DictionaryTable +*/ +public class DictionaryTableModel extends AbstractTableModel +{ + private Word []array; + private DuffData arrayTibetan[][]; + private boolean tibetanActivated; + + public DictionaryTableModel(Word[] array) + { + this.newSearch(array); + tibetanActivated=false; + } + + public int getRowCount() + { + if (array!=null) + return array.length; + else return 0; + } + + public int getColumnCount() + { + return 2; + } + + public void activateTibetan(boolean activate) + { + tibetanActivated = activate; + } + + + public Object getValueAt(int row, int column) + { + if (column==0) + { + if (tibetanActivated) + return arrayTibetan[row]; + else + return array[row].getWylie(); + } + else + return array[row].getDef(); + } + + + public Class getColumnClass(int c) + { + return getValueAt(0, c).getClass(); + } + + public void newSearch(Token[] token) + { + int i, n=0; + if (token==null) + array = null; + else + { + for (i=0; iJComponent.setForeground to assign + * the unselected-foreground color to the specified color. + * + * @param c set the foreground color to this value + */ + public void setForeground(Color c) { + super.setForeground(c); + unselectedForeground = c; + } + + /** + * Overrides JComponent.setForeground to assign + * the unselected-background color to the specified color. + * + * @param c set the background color to this value + */ + public void setBackground(Color c) { + super.setBackground(c); + unselectedBackground = c; + } + + /** + * Notification from the UIManager that the look and feel + * [L&F] has changed. + * Replaces the current UI object with the latest version from the + * UIManager. + * + * @see JComponent#updateUI + */ + public void updateUI() { + super.updateUI(); + setForeground(null); + setBackground(null); + } + + public Component getTableCellRendererComponent(JTable table, Object value, + boolean isSelected, boolean hasFocus, int row, int column) + { + if (isSelected) + { + super.setForeground(table.getSelectionForeground()); + super.setBackground(table.getSelectionBackground()); + } + else + { + super.setForeground((unselectedForeground != null) ? unselectedForeground : table.getForeground()); + super.setBackground((unselectedBackground != null) ? unselectedBackground : table.getBackground()); + } + + if (hasFocus) { + setBorder( UIManager.getBorder("Table.focusCellHighlightBorder") ); + if (table.isCellEditable(row, column)) { + super.setForeground( UIManager.getColor("Table.focusCellForeground") ); + super.setBackground( UIManager.getColor("Table.focusCellBackground") ); + } + } else { + setBorder(noFocusBorder); + } + + setValue(value); + + // ---- begin optimization to avoid painting background ---- + Color back = getBackground(); + boolean colorMatch = (back != null) && ( back.equals(table.getBackground()) ) && table.isOpaque(); + setOpaque(!colorMatch); + // ---- end optimization to aviod painting background ---- + + return this; + } + + public void setValue(Object value) + { + TibetanDocument doc = (TibetanDocument) getDocument(); + try + { + doc.remove(0, doc.getLength()); + } + catch (Exception e) + { + System.out.println(e); + } + doc.insertDuff(0, (DuffData []) value); + } +} \ No newline at end of file diff --git a/source/org/thdl/tib/scanner/DuffScannerPanel.java b/source/org/thdl/tib/scanner/DuffScannerPanel.java new file mode 100644 index 0000000..f3f0bad --- /dev/null +++ b/source/org/thdl/tib/scanner/DuffScannerPanel.java @@ -0,0 +1,203 @@ +/* +The contents of this file are subject to the AMP Open Community License +Version 1.0 (the "License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License on the AMP web site +(http://www.tibet.iteso.mx/Guatemala/). + +Software distributed under the License is distributed on an "AS IS" basis, +WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +License for the specific terms governing rights and limitations under the +License. + +The Initial Developer of this software is Andres Montano Pellegrini. Portions +created by Andres Montano Pellegrini are Copyright 2001 Andres Montano +Pellegrini. All Rights Reserved. + +Contributor(s): ______________________________________. +*/ +package org.thdl.tib.scanner; + +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; +import javax.swing.event.*; +import javax.swing.table.*; +import org.thdl.tib.input.DuffPane; +import org.thdl.tib.text.TibetanDocument; + +/** Graphical interfase to be used by applications and applets + to input a Tibetan text (in Roman or Tibetan script) and + displays the words (in Roman or Tibetan script) with their + definitions (in Roman script). Uses the THDL inputting system. + + @author Andrés Montano Pellegrini +*/ +public class DuffScannerPanel extends ScannerPanel +{ + private TextArea fullDef, txtInput; + private DuffPane duffInput; + private JPanel inputPanel; + + private JScrollPane listDef; +// private Font tibetanFont; + private DictionaryTable table; + + private DictionaryTableModel model; + + boolean showingTibetan; + + public DuffScannerPanel(String file) + { + super(file); + Panel panel1; + panel1 = new Panel(new GridLayout(3,1)); + /* Looks up tibcodes in directory of applet. In order + to work through a proxy store all the applet classes toghether + with tibcodes.ini in a jar file. */ + duffInput = new DuffPane(); + duffInput.disableRoman(); + + JPanel jpanel = new JPanel(new GridLayout(1,1)); + JScrollPane jsp = new JScrollPane(duffInput, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); + jpanel.add(jsp); + inputPanel = new JPanel(new CardLayout()); + inputPanel.add(jpanel, "1"); + + txtInput = new TextArea("",0,0,TextArea.SCROLLBARS_VERTICAL_ONLY); + inputPanel.add(txtInput, "2"); + panel1.add(inputPanel); + + fullDef = new TextArea("",0,0,TextArea.SCROLLBARS_VERTICAL_ONLY); + fullDef.setEditable(false); + + model = new DictionaryTableModel(null); + table = new DictionaryTable(model, fullDef); + table.activateTibetan(true); + listDef = new JScrollPane(table); + + panel1.add(listDef); + panel1.add(fullDef); + add(panel1, BorderLayout.CENTER); + showingTibetan = true; + +// tibetanFont = new Font("TibetanMachine",Font.PLAIN,36); + } + + public void addFocusListener(FocusListener fl) + { + txtInput.addFocusListener(fl); + duffInput.addFocusListener(fl); + fullDef.addFocusListener(fl); + } + +/* public void printAllDefs() + { + + Word word; + int i; + GridBagConstraints gb1 = new GridBagConstraints(), gb2 = new GridBagConstraints(); + JTextArea jtext; + TextArea text; + GridBagLayout grid = new GridBagLayout(); + + if (panelOutput!=null) + defPane.remove(panelOutput); + + panelOutput = new Panel(grid); + gb1.weightx = 1; + gb2.weightx = 4; + gb1.gridwidth = GridBagConstraints.RELATIVE; + gb2.gridwidth = GridBagConstraints.REMAINDER; + + for (i=0; i 0) + s = s.substring(0,posEnter);*/ + duffInput.newDocument(); + if (!s.equals("")) + duffInput.toTibetanMachineWeb(s, 0); + table.activateTibetan(true); + cl.first(inputPanel); + showingTibetan = true; + } + if (!enabled && showingTibetan) + { + txtInput.setText(((TibetanDocument)duffInput.getDocument()).getWylie()); + table.activateTibetan(false); + cl.last(inputPanel); + showingTibetan = false; + } + table.repaint(); + } +} diff --git a/source/org/thdl/tib/scanner/FileSyllableListTree.java b/source/org/thdl/tib/scanner/FileSyllableListTree.java new file mode 100644 index 0000000..75a7728 --- /dev/null +++ b/source/org/thdl/tib/scanner/FileSyllableListTree.java @@ -0,0 +1,161 @@ +/* +The contents of this file are subject to the AMP Open Community License +Version 1.0 (the "License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License on the AMP web site +(http://www.tibet.iteso.mx/Guatemala/). + +Software distributed under the License is distributed on an "AS IS" basis, +WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +License for the specific terms governing rights and limitations under the +License. + +The Initial Developer of this software is Andres Montano Pellegrini. Portions +created by Andres Montano Pellegrini are Copyright 2001 Andres Montano +Pellegrini. All Rights Reserved. + +Contributor(s): ______________________________________. +*/ +package org.thdl.tib.scanner; + +import java.io.*; + +/** Searches the words directly in a file; not the prefered + implementation. The search is too slow! + The prefered implementation is the CachedSyllableList. + +

The words must be stored in a binary file tree structure format. + This can be done using the BinaryFileGenerator.

+ + @author Andrés Montano Pellegrini + @see TibetanScanner + @see CachedSyllableList + @see BinaryFileGenerator +*/ + +public class FileSyllableListTree implements SyllableListTree +{ + private String sil; + private long def[]; + private long posLista; + private DictionarySource defSource; + public static DictionarySource defSourcesWanted; + public static RandomAccessFile wordRaf=null; + private static RandomAccessFile defRaf=null; + + /** Creates the root */ + public FileSyllableListTree(String archivo, int defSourcesWanted) throws Exception + { + sil = null; + def = null; + this.defSource = new DictionarySource(); + openFiles(archivo); + posLista = wordRaf.length() - 4; + wordRaf.seek(posLista); + posLista = (long)wordRaf.readInt(); + } + + /** Used to create each node (except the root) + */ + public FileSyllableListTree(String sil, long []def, DictionarySource defSource, long posLista) + { + this.sil=sil; + this.def=def; + this.defSource = defSource; + this.posLista=posLista; + } + + public String toString() + { + return sil; + } + + public DictionarySource getDictionarySource() + { + return defSource; + } + + public static void openFiles(String archivo) throws Exception + { + wordRaf = new RandomAccessFile(archivo + ".wrd", "r"); + defRaf = new RandomAccessFile(archivo + ".def", "r"); + defSourcesWanted = DictionarySource.getAllDictionaries(); + } + + public String getDef() + { + return getDefs().toString(); + } + + public Definitions getDefs() + { + if (def==null) return null; + DictionarySource defSourceAvail = defSource.intersection(defSourcesWanted); + + int defsAvail[] = defSourceAvail.untangleDefs(), defsFound[] = defSource.untangleDefs(def.length); + + + String defs[] = new String[defsAvail.length]; + int i, n=0; + try + { + for (i=0; i0) + return null; + if (sil.equals(silStr)) + return new FileSyllableListTree(sil, defSource, sourceDef, pos); + + }while(sourceDef.hasBrothers()); + } + catch (Exception e) + { + } + return null; + } +} \ No newline at end of file diff --git a/source/org/thdl/tib/scanner/Link.java b/source/org/thdl/tib/scanner/Link.java new file mode 100644 index 0000000..d34266c --- /dev/null +++ b/source/org/thdl/tib/scanner/Link.java @@ -0,0 +1,105 @@ +/* +The contents of this file are subject to the AMP Open Community License +Version 1.0 (the "License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License on the AMP web site +(http://www.tibet.iteso.mx/Guatemala/). + +Software distributed under the License is distributed on an "AS IS" basis, +WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +License for the specific terms governing rights and limitations under the +License. + +The Initial Developer of this software is Andres Montano Pellegrini. Portions +created by Andres Montano Pellegrini are Copyright 2001 Andres Montano +Pellegrini. All Rights Reserved. + +Contributor(s): ______________________________________. +*/ + +package org.thdl.tib.scanner; +import java.io.*; + +class Link +{ + private Object obj; + public Link siguiente; + + public Link(Object obj) + { + this.obj = obj; + siguiente = null; + } + + public String toString() + { + return obj.toString(); + } + + public Link createNext(Object obj) + { + return siguiente = new Link(obj); + } + + public Link createPrevious(Object obj) + { + Link l = new Link(obj); + l.siguiente=this; + return l; + } + + public Object get() + { + return obj; + } + + public Link next() + { + return this.siguiente; + } + public int size() + { + int n=0; + Link actual = this; + while (actual != null) + { + n++; + actual = actual.next(); + } + return n; + } + + public Object clone() + { + return new Link(obj); + } + + public void insertSorted(Link link) + { + if (siguiente==null) + siguiente = link; + else + if (link.toString().compareTo(siguiente.toString())<=0) + { + link.siguiente = siguiente; + siguiente = link; + } + else siguiente.insertSorted(link); + } + + public Link sort() + { + Link newCabeza = (Link) clone(), next = next(), newLink; + while (next!=null) + { + newLink = (Link) next.clone(); + if (newLink.toString().compareTo(newCabeza.toString())<=0) + { + newLink.siguiente = newCabeza; + newCabeza = newLink; + } + else newCabeza.insertSorted(newLink); + next = next.next(); + } + return newCabeza; + } +} \ No newline at end of file diff --git a/source/org/thdl/tib/scanner/LinkedList.java b/source/org/thdl/tib/scanner/LinkedList.java new file mode 100644 index 0000000..21bb388 --- /dev/null +++ b/source/org/thdl/tib/scanner/LinkedList.java @@ -0,0 +1,117 @@ +/* +The contents of this file are subject to the AMP Open Community License +Version 1.0 (the "License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License on the AMP web site +(http://www.tibet.iteso.mx/Guatemala/). + +Software distributed under the License is distributed on an "AS IS" basis, +WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +License for the specific terms governing rights and limitations under the +License. + +The Initial Developer of this software is Andres Montano Pellegrini. Portions +created by Andres Montano Pellegrini are Copyright 2001 Andres Montano +Pellegrini. All Rights Reserved. + +Contributor(s): ______________________________________. +*/ + +package org.thdl.tib.scanner; +import java.util.*; + +public class LinkedList +{ + protected Link cabeza; + public LinkedList() + { + cabeza=null; + } + + private LinkedList(Link cabeza) + { + this.cabeza = cabeza; + } + + public void addLast(Object o) + { + if (cabeza==null) + { + cabeza = new Link(o); + } + else cabeza = cabeza.createPrevious(o); + } + + public Object getLast() + { + if (cabeza==null) return null; + else return cabeza.get(); + } + + public ListIterator listIterator() + { + return new ListIterator(cabeza); + } + + public ListIterator listIterator(int n) + { + return new ListIterator(cabeza, n); + } + + public boolean isEmpty() + { + return cabeza==null; + } + + public boolean contains(Object o) + { + Link current=cabeza; + while (current!=null) + { + if (current.get().equals(o)) return true; + current = current.next(); + } + return false; + } + + public int size() + { + if (cabeza==null) return 0; + return cabeza.size(); + } + + public LinkedList sort() + { + return new LinkedList(cabeza.sort()); + } + + public Object[] toArray() + { + int n = size(); + if (n==0) return null; + Object array[] = new Object[n]; + ListIterator li = listIterator(); + while (li.hasNext()) + { + n--; + array[n] = li.next(); + } + return array; + } + + public String[] toStringArray() + { + int n = size(); + if (n==0) return null; + Object o; + String array[] = new String[n]; + ListIterator li = listIterator(); + while (li.hasNext()) + { + n--; + o = li.next(); + if (o==null) array[n]=null; + else array[n] = o.toString(); + } + return array; + } +} diff --git a/source/org/thdl/tib/scanner/ListIterator.java b/source/org/thdl/tib/scanner/ListIterator.java new file mode 100644 index 0000000..0b27b84 --- /dev/null +++ b/source/org/thdl/tib/scanner/ListIterator.java @@ -0,0 +1,49 @@ +/* +The contents of this file are subject to the AMP Open Community License +Version 1.0 (the "License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License on the AMP web site +(http://www.tibet.iteso.mx/Guatemala/). + +Software distributed under the License is distributed on an "AS IS" basis, +WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +License for the specific terms governing rights and limitations under the +License. + +The Initial Developer of this software is Andres Montano Pellegrini. Portions +created by Andres Montano Pellegrini are Copyright 2001 Andres Montano +Pellegrini. All Rights Reserved. + +Contributor(s): ______________________________________. +*/ + +package org.thdl.tib.scanner; +import java.util.*; + +public class ListIterator +{ + Link current; + + public ListIterator(Link current) + { + this.current=current; + } + + public ListIterator(Link current, int n) + { + this(current); + int i; + for (i=0; i-1); + } + + public void scanLine(String linea) + { + int init = 0, fin, i; + char ch; + String sil; + boolean doNotFinishUp; + + if (linea.equals("")) + { + finishUp(); + wordList.addLast(new PunctuationMark('\n')); + return; + } + +outAHere: + while(true) + { + doNotFinishUp=true; + + // Make init skip all punctuation marks + while (true) + { + if (init>=linea.length()) + break outAHere; + ch = linea.charAt(init); + if (endOfParagraphMarks.indexOf(ch)>=0) + { + if (doNotFinishUp) + { + finishUp(); + doNotFinishUp=false; + } + wordList.addLast(new PunctuationMark(ch)); + } + else if (endOfSyllableMarks.indexOf(ch)<0) + break; + + init++; + } + + doNotFinishUp = true; + + /* move fin to the end of the next syllable. If finishing + up is necessary it is done after scanSyllable + */ + + fin = init+1; + while (true) + { + ch = linea.charAt(fin); + if (endOfParagraphMarks.indexOf(ch)>=0) + { + doNotFinishUp = false; + break; + } + else if (endOfSyllableMarks.indexOf(ch)>=0) + { + break; + } + else + { + fin++; + if (fin>=linea.length()) + break; + } + } + + sil = linea.substring(init, fin); + scanSyllable(sil); + + if (!doNotFinishUp) + { + finishUp(); + wordList.addLast(new PunctuationMark(ch)); + } + init = fin+1; + } + } + + public LinkedList getTokenLinkedList() + { + return wordList; + } + + public Token[] getTokenArray() + { + int i=wordList.size(); + Token token[] = new Token[i]; + Object obj; + ListIterator li = wordList.listIterator(); + while(li.hasNext()) + token[--i] = (Token)li.next(); + return token; + } + + private static boolean isVowel(char ch) + { + return (ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u'); + } + + /** Looks for .dic file, and returns the dictionary descriptions. + Also updates the definitionTags in the Definitions class. + */ + public String[] getDictionaryDescriptions() + { + int n; + try + { + BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(archivo + ".dic"))); + LinkedList ll1 = new LinkedList(), ll2 = new LinkedList(); + int i; + String s; + while ((s=br.readLine())!=null) + { + n = s.indexOf(","); + if (n < 0) + { + ll1.addLast(null); + ll2.addLast(s); + } + else + { + ll1.addLast(s.substring(0,n).trim()); + ll2.addLast(s.substring(n+1).trim()); + } + } + Definitions.defTags = ll2.toStringArray(); + return ll1.toStringArray(); + } + catch (Exception e) + { + return null; + } + } +} \ No newline at end of file diff --git a/source/org/thdl/tib/scanner/MemorySyllableListTree.java b/source/org/thdl/tib/scanner/MemorySyllableListTree.java new file mode 100644 index 0000000..0a87d53 --- /dev/null +++ b/source/org/thdl/tib/scanner/MemorySyllableListTree.java @@ -0,0 +1,192 @@ +/* +The contents of this file are subject to the AMP Open Community License +Version 1.0 (the "License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License on the AMP web site +(http://www.tibet.iteso.mx/Guatemala/). + +Software distributed under the License is distributed on an "AS IS" basis, +WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +License for the specific terms governing rights and limitations under the +License. + +The Initial Developer of this software is Andres Montano Pellegrini. Portions +created by Andres Montano Pellegrini are Copyright 2001 Andres Montano +Pellegrini. All Rights Reserved. + +Contributor(s): ______________________________________. +*/ + +package org.thdl.tib.scanner; +import java.util.*; +import java.io.*; + +public class MemorySyllableListTree extends LinkedList implements SyllableListTree +{ + protected String sil, def; + + public String toString() + { + return sil; + } + + /** Null because it does not support multiple dictionaries. + */ + public DictionarySource getDictionarySource() + { + return null; + } + + + public MemorySyllableListTree(String sil, String def) + { + super(); + int marker = sil.indexOf(" "); + + if (marker<0) + { + this.sil = sil; + this.def = def; + } + else + { + this.sil = sil.substring(0, marker); + this.def = null; + addLast(new MemorySyllableListTree(sil.substring(marker+1).trim(), def)); + } + } + + public MemorySyllableListTree(String archivo) throws Exception + { + super(); + sil = null; + def = null; + + BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(archivo))); + String entrada; + + while ((entrada = br.readLine())!=null) + { + entrada = entrada.trim(); + if (!entrada.equals("")) + add(entrada); + } + } + + private void add(String entrada) + { + int marker = entrada.indexOf("-"); + if (marker<0) + throw new RuntimeException("Error de sintaxis; diccionario no pudo ser cargado!"); + + add(entrada.substring(0,marker).trim(), entrada.substring(marker+1).trim()); + } + + private void add(String word, String def) + { + MemorySyllableListTree prefijo; + String firstSillable; + int pos, marker = word.indexOf(" "); + + if (marker<0) + firstSillable = word; + else firstSillable = word.substring(0,marker); + +/* + Para orden "normal" con sanscrito transliterado traslapado + entre el tibetano + if (isEmpty() || (prefijo = lookUp(firstSillable))==null) */ + +// Para orden modificado de silabas + if (isEmpty() || !(prefijo = (MemorySyllableListTree)getLast()).equals(firstSillable)) + super.addLast(new MemorySyllableListTree(word, def)); + else + if (marker<0) // ya estaba en el diccionario! + prefijo.addMoreDef(def); + else + prefijo.add(word.substring(marker+1).trim(), def); + } + + private void addMoreDef(String def) + { + if (this.def==null || this.def.equals("")) + this.def=def; + else this.def = this.def + ". " + def; + } + + public boolean equals (Object o) + { + if (o instanceof String) + { + return sil.equals((String)o); + } + else return false; + } + + public String getDef() + { + return def; + } + + public Definitions getDefs() + { + return new Definitions(def); + } + + public boolean hasDef() + { + return def!=null; + } + + private void println(PrintStream ps) + { + println(ps, ""); + } + + private void println(PrintStream ps, String prefijo) + { + + if (sil!=null) + { + if (prefijo.equals("")) + prefijo = sil; + else + prefijo = prefijo + " " + sil; + + if (def!=null) + ps.println(prefijo + " - " + def); + } + + MemorySyllableListTree silHijos; + ListIterator i = listIterator(); + while (i.hasNext()) + { + silHijos = (MemorySyllableListTree) i.next(); + silHijos.println(ps, prefijo); + } + } + + public SyllableListTree lookUp(String silStr) + { + MemorySyllableListTree sil; + ListIterator i = listIterator(); + while (i.hasNext()) + { + sil = (MemorySyllableListTree) i.next(); + if (sil.equals(silStr)) + return sil; + } + return null; + } + + public static void main(String args[]) throws Exception + { + if (args.length!=1) + { + System.out.println("Programa de prueba para verificar almacenamiento de diccionario."); + System.out.println("Sintaxis: java MemorySyllableListTree arch-dict"); + return; + } + MemorySyllableListTree sl = new MemorySyllableListTree(args[0]); + sl.println(System.out); + } +} \ No newline at end of file diff --git a/source/org/thdl/tib/scanner/OnLineScannerFilter.java b/source/org/thdl/tib/scanner/OnLineScannerFilter.java new file mode 100644 index 0000000..5edeede --- /dev/null +++ b/source/org/thdl/tib/scanner/OnLineScannerFilter.java @@ -0,0 +1,300 @@ +/* +The contents of this file are subject to the AMP Open Community License +Version 1.0 (the "License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License on the AMP web site +(http://www.tibet.iteso.mx/Guatemala/). + +Software distributed under the License is distributed on an "AS IS" basis, +WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +License for the specific terms governing rights and limitations under the +License. + +The Initial Developer of this software is Andres Montano Pellegrini. Portions +created by Andres Montano Pellegrini are Copyright 2001 Andres Montano +Pellegrini. All Rights Reserved. + +Contributor(s): ______________________________________. +*/ + +package org.thdl.tib.scanner; +import java.util.*; +import java.io.*; +import java.text.*; +import java.util.*; +import javax.servlet.*; +import javax.servlet.http.*; +import org.thdl.tib.text.TibetanHTML; + +public class OnLineScannerFilter extends HttpServlet { + + ResourceBundle rb; + private TibetanScanner scanner; + + public OnLineScannerFilter() throws Exception + { + + rb = ResourceBundle.getBundle("dictionary"); + scanner = new LocalTibetanScanner(rb.getString("onlinescannerfilter.dict-file-name")); + } + + public void doGet(HttpServletRequest request, + HttpServletResponse response) + throws IOException, ServletException + { + response.setContentType("text/html"); + PrintWriter out = response.getWriter(); + String parrafo = request.getParameter("parrafo"), checkboxName, script; + DictionarySource ds=null; + boolean checkedDicts[], allUnchecked, wantsTibetan; + int percent=100; + + out.println(""); + out.println(""); + out.println(""); + out.println(""); + out.println(""); + out.println("The Online Tibetan to English Translation/Dictionary Tool"); + script = request.getParameter("script"); + wantsTibetan = (script==null || script.equals("tibetan")); + if (wantsTibetan) + { + out.println(""); + out.println(""); + } + out.println(""); + out.println(""); + out.println("

The Online Tibetan to English Translation/Dictionary Tool

"); + out.println("
"); + out.println(""); + out.println(" "); + out.println(" "); + out.println(" "); + out.println(" "); + out.println(" "); + out.println("
"); + out.println("

Display results in:

"); + out.println("

Tibetan script (using Tibetan Machine Web font)
"); + out.println(" Roman script

"); + out.println("

"); + out.println("
"); + + String dictionaries[] = scanner.getDictionaryDescriptions(); + if (dictionaries!=null) + { + int i; + ds = scanner.getDictionarySource(); + ds.reset(); + checkedDicts = new boolean[dictionaries.length]; +/* out.println(" "); + out.println("Search in dictionaries:");*/ + out.println("

Search in dictionaries: "); + allUnchecked=true; + for (i=0; i"); + out.print("" + dictionaries[i] + " (" + Definitions.defTags[i] + ")   "); + else + out.print(">" + Definitions.defTags[i] + "   "); +// out.println(" + ""); + } +// out.println(" "); + } + else ds = DictionarySource.getAllDictionaries(); +// out.println(""); + out.println("

"); + out.println("

Input text:

"); + out.println(""); + out.println("
"); + + if (parrafo != null) + if (ds!=null && !ds.isEmpty()) + desglosar(parrafo, out, wantsTibetan); + + out.println(TibetanScanner.copyrightHTML); + out.println(""); + out.println(""); + } + + public void doPost(HttpServletRequest request, + HttpServletResponse response) + throws IOException, ServletException + { + doGet(request, response); + } + + synchronized public void desglosar(String in, PrintWriter pw, boolean tibetan) + { + boolean hayMasLineas=true; + //int init = 0, fin; + //String linea; + Object words[]; + + if (!in.equals("")) + { + /* while (hayMasLineas) + { + fin = in.indexOf("\n",init); + if (fin<0) + { + linea = in.substring(init).trim(); + hayMasLineas=false; + } + else + linea = in.substring(init, fin).trim(); + + scanner.scanBody(linea); + + init = fin+1; + } */ + scanner.scanBody(in); + scanner.finishUp(); + words = scanner.getTokenArray(); + printText(pw, words, tibetan); + printAllDefs(pw, words, tibetan); + scanner.clearTokens(); + } + } + + public void printText(PrintWriter pw, Object words[], boolean tibetan) + { + Token token; + Word word; + char pm; + int i; + + pw.print("

"); + for (i=0; i < words.length; i++) + { + token = (Token) words[i]; + if (token instanceof Word) + { + word = (Word) token; + pw.print(word.getLink()); + } + else + { + if (token instanceof PunctuationMark) + { + pm = token.toString().charAt(0); + switch (pm) + { + case '\n': + pw.println("

"); + pw.print("

"); + break; + case '<': + pw.print("< "); + break; + case '>': + pw.print("> "); + break; + default: + pw.print(pm + " "); + } + } + + } + } + pw.println("

"); + } + + public void printAllDefs(PrintWriter pw, Object words[], boolean tibetan) + { + LinkedList temp = new LinkedList(); + int i; + Word word; + Definitions defs; + + for (i=words.length-1; i >= 0; i--) + { + if (words[i] instanceof Word) + { + if (!temp.contains(words[i])) + { + temp.addLast(words[i]); + } + } + } + + ListIterator li = temp.listIterator(); + String tag; + pw.println(""); + while (li.hasNext()) + { + word = (Word)li.next(); + defs = word.getDefs(); + pw.println(" "); + tag = defs.getTag(0); + if (tag!=null) + { + pw.println(" "); + pw.println(" "); + pw.println(" "); + } + else + { + pw.println(" "); + pw.println(" "); + } + pw.println(" "); + for (i=1; i"); + tag = defs.getTag(i); + if (tag!=null) + { + pw.println(" "); + pw.println(" "); + } + else pw.println(" "); + pw.println(" "); + } + } + pw.println("
"+ word.getBookmark(tibetan) +""+ tag +"" + defs.def[0] + ""+ word.getBookmark(tibetan) +"" + defs.def[0] + "
"+ tag +"" + defs.def[i] + "" + defs.def[i] + "
"); + } +} \ No newline at end of file diff --git a/source/org/thdl/tib/scanner/PunctuationMark.java b/source/org/thdl/tib/scanner/PunctuationMark.java new file mode 100644 index 0000000..9aa8d08 --- /dev/null +++ b/source/org/thdl/tib/scanner/PunctuationMark.java @@ -0,0 +1,32 @@ +/* +The contents of this file are subject to the AMP Open Community License +Version 1.0 (the "License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License on the AMP web site +(http://www.tibet.iteso.mx/Guatemala/). + +Software distributed under the License is distributed on an "AS IS" basis, +WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +License for the specific terms governing rights and limitations under the +License. + +The Initial Developer of this software is Andres Montano Pellegrini. Portions +created by Andres Montano Pellegrini are Copyright 2001 Andres Montano +Pellegrini. All Rights Reserved. + +Contributor(s): ______________________________________. +*/ + +package org.thdl.tib.scanner; + +class PunctuationMark extends Token +{ + public PunctuationMark(char ch) + { + super(String.valueOf(ch)); + } + + public String toString() + { + return super.token; + } +} \ No newline at end of file diff --git a/source/org/thdl/tib/scanner/RemoteScannerFilter.java b/source/org/thdl/tib/scanner/RemoteScannerFilter.java new file mode 100644 index 0000000..0b642cc --- /dev/null +++ b/source/org/thdl/tib/scanner/RemoteScannerFilter.java @@ -0,0 +1,96 @@ +/* +The contents of this file are subject to the AMP Open Community License +Version 1.0 (the "License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License on the AMP web site +(http://www.tibet.iteso.mx/Guatemala/). + +Software distributed under the License is distributed on an "AS IS" basis, +WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +License for the specific terms governing rights and limitations under the +License. + +The Initial Developer of this software is Andres Montano Pellegrini. Portions +created by Andres Montano Pellegrini are Copyright 2001 Andres Montano +Pellegrini. All Rights Reserved. + +Contributor(s): ______________________________________. +*/ +package org.thdl.tib.scanner; + +import java.util.*; +import java.io.*; +import java.text.*; +import java.util.*; +import javax.servlet.*; +import javax.servlet.http.*; + +public class RemoteScannerFilter extends GenericServlet +{ + private TibetanScanner scanner; + private DictionarySource ds; + + public RemoteScannerFilter() throws Exception + { + ResourceBundle rb = ResourceBundle.getBundle("dictionary"); + scanner = new LocalTibetanScanner(rb.getString("onlinescannerfilter.dict-file-name")); + ds = scanner.getDictionarySource(); + } + + public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException + { + BufferedReader br; + res.setContentType ("text/plain"); + LinkedList words; + Word word; + PrintWriter out = res.getWriter(); + int i; + String linea, dicts = req.getParameter("dicts"), dicDescrip[]; + if (dicts!=null) + { + if (dicts.equals("names")) + { + dicDescrip = scanner.getDictionaryDescriptions(); + if (dicDescrip==null) + { + out.close(); + return; + } + + for (i=0; i