Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

Feature Code Submission

2006-02-03
2013-04-29
  • Aaron Powers
    Aaron Powers
    2006-02-03

    Thanks for JWNL -- it works great for our project.

    I have a simple feature I'd like to submit, along with the source to fix it.

    The problem I ran into is that we need a full properties file even when the only change needed is the location of the dictionary -- that can be done with a single initialize call with one parameter.

    So, here's an alternative set of initialize calls that will work, along with a default properties file that goes in net/didion/jwnl. (Even if you choose not to put this in as full, you may want to change JWNL.initialize(InputStream) to JWNL(InputSource), which is much more flexible and then users can do this type of thing.

    -----------------------------------------------
    Source code (all goes into JWNL instead of the existing initialize(InputStream) method -- no changes except the lines directly relating to InputStream):

    public static String getTextFromStream(InputStream inputStream) throws IOException {

            BufferedReader fileCheck;

            fileCheck = new BufferedReader(new InputStreamReader(inputStream));

            String fileText = "";

            String thing;

            thing = fileCheck.readLine();

            while (thing != null) {

                fileText += thing+"\n";

                thing = fileCheck.readLine();

            }

            try {

                fileCheck.close();

            } catch (IOException e) {

                // doesn't matter.

            }

            return fileText;

        }

       

        public static void initialize(InputStream propertiesStream) throws JWNLException {

            try {

                // find the properties file

                if (propertiesStream == null || propertiesStream.available() <= 0) {

                    throw new JWNLException("JWNL_EXCEPTION_001");

                }

            } catch (IOException ex) {

                throw new JWNLException("JWNL_EXCEPTION_001", ex);

            }

           

            initialize(new InputSource(propertiesStream));

           

            // do this in a separate try/catch since parse can also throw an IOException

            try {

                propertiesStream.close();

            } catch (IOException ex) {

            }

        }

       

        public static void initialize(String wordNetDictionaryPath) throws JWNLException {

            try {

                String properties = getTextFromStream(JWNL.class.getResourceAsStream("file_properties.xml"));

                properties = properties.replace("${DICT_VARIABLE}", wordNetDictionaryPath);

                //System.out.println(properties);

                initialize(new InputSource(new StringReader(properties)));

            } catch (IOException e) {

                throw new JWNLException("IO error opening properties file", e);

            }

        }

       

        public static void initialize(InputSource properties) throws JWNLException {

            checkInitialized(UNINITIALIZED);

            _initStage = START;

            // find the properties file

            if (properties == null) {

                throw new JWNLException("JWNL_EXCEPTION_001");

            }

            // parse the properties file

            Document doc = null;

            try {

                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

                factory.setValidating(false);

                DocumentBuilder docBuilder = factory.newDocumentBuilder();

                doc = docBuilder.parse(properties);

            } catch (Exception ex) {

                throw new JWNLException("JWNL_EXCEPTION_002", ex);

            }

           

            org.w3c.dom.Element root = doc.getDocumentElement();

            // set the locale

            _bundle.setLocale(getLocale(

                    getAttribute(root, LANGUAGE_ATTRIBUTE),

                    getAttribute(root, COUNTRY_ATTRIBUTE)));

            // add additional resources

            NodeList resourceNodes = root.getElementsByTagName(RESOURCE_TAG);

            for (int i = 0; i < resourceNodes.getLength(); i++) {

                String resource = getAttribute(resourceNodes.item(i), CLASS_ATTRIBUTE);

                if (resource != null) {

                    _bundle.addResource(resource);

                }

            }

            // initialize bundle-dependant resources

            PointerType.initialize();

            Adjective.initialize();

            VerbFrame.initialize();

            // parse version information

            NodeList versionNodes = root.getElementsByTagName(VERSION_TAG);

            if (versionNodes.getLength() == 0) {

                throw new JWNLException("JWNL_EXCEPTION_003");

            }

            Node version = versionNodes.item(0);

            _initStage = DICTIONARY_PATH_SET;

            String number = getAttribute(version, NUMBER_ATTRIBUTE);

            _version = new Version(

                    getAttribute(version, PUBLISHER_ATTRIBUTE),

                    (number == null) ? 0.0 : Double.parseDouble(number),

                    getLocale(getAttribute(version, LANGUAGE_ATTRIBUTE), getAttribute(version, COUNTRY_ATTRIBUTE)));

            _initStage = VERSION_SET;

            // parse dictionary

            NodeList dictionaryNodeList = root.getElementsByTagName(DICTIONARY_TAG);

            if (dictionaryNodeList.getLength() == 0) {

                throw new JWNLException("JWNL_EXCEPTION_005");

            }

            createElementFromNode(dictionaryNodeList.item(0)).install();

            _initStage = INITIALIZED;

        }

    Properties File, goes into net/didion/jwnl/file_properties.xml-------------------------------------------------
    <?xml version="1.0" encoding="UTF-8"?>
    <jwnl_properties language="en">
        <version publisher="Princeton" number="2.0" language="en"/>
        <dictionary class="net.didion.jwnl.dictionary.FileBackedDictionary">
            <param name="morphological_processor" value="net.didion.jwnl.dictionary.morph.DefaultMorphologicalProcessor">
                <param name="operations">
                    <param value="net.didion.jwnl.dictionary.morph.LookupExceptionsOperation"/>
                    <param value="net.didion.jwnl.dictionary.morph.DetachSuffixesOperation">
                        <param name="noun" value="|s=|ses=s|xes=x|zes=z|ches=ch|shes=sh|men=man|ies=y|"/>
                        <param name="verb" value="|s=|ies=y|es=e|es=|ed=e|ed=|ing=e|ing=|"/>
                        <param name="adjective" value="|er=|est=|er=e|est=e|"/>
                        <param name="operations">
                            <param value="net.didion.jwnl.dictionary.morph.LookupIndexWordOperation"/>
                            <param value="net.didion.jwnl.dictionary.morph.LookupExceptionsOperation"/>
                        </param>
                    </param>
                    <param value="net.didion.jwnl.dictionary.morph.TokenizerOperation">
                        <param name="delimiters">
                            <param value=" "/>
                            <param value="-"/>
                        </param>
                        <param name="token_operations">
                            <param value="net.didion.jwnl.dictionary.morph.LookupIndexWordOperation"/>
                            <param value="net.didion.jwnl.dictionary.morph.LookupExceptionsOperation"/>
                            <param value="net.didion.jwnl.dictionary.morph.DetachSuffixesOperation">
                                <param name="noun" value="|s=|ses=s|xes=x|zes=z|ches=ch|shes=sh|men=man|ies=y|"/>
                                <param name="verb" value="|s=|ies=y|es=e|es=|ed=e|ed=|ing=e|ing=|"/>
                                <param name="adjective" value="|er=|est=|er=e|est=e|"/>
                                <param name="operations">
                                    <param value="net.didion.jwnl.dictionary.morph.LookupIndexWordOperation"/>
                                    <param value="net.didion.jwnl.dictionary.morph.LookupExceptionsOperation"/>
                                </param>
                            </param>
                        </param>
                    </param>
                </param>
            </param>
            <param name="dictionary_element_factory" value="net.didion.jwnl.princeton.data.PrincetonWN17FileDictionaryElementFactory"/>
            <param name="file_manager" value="net.didion.jwnl.dictionary.file_manager.FileManagerImpl">
                <param name="file_type" value="net.didion.jwnl.princeton.file.PrincetonRandomAccessDictionaryFile"/>
                <param name="dictionary_path" value="${DICT_VARIABLE}"/>
    <!--            <param name="dictionary_path" value="/usr/local/WordNet-2.1/dict"/>-->
            </param>
        </dictionary>
        <resource class="PrincetonResource"/>
    </jwnl_properties>

     
    • Aaron Powers
      Aaron Powers
      2006-02-03

      Of course, if you want these source files by email, etc, because the formatting on these boards is poor, just send me a message and I'll pass them on.