Example #1
0
  private void loadFromXml(String fileName)
      throws ParserConfigurationException, SAXException, IOException, ParseException {
    System.out.println("NeuralNetwork : loading network topology from file " + fileName);
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder parser = factory.newDocumentBuilder();
    Document doc = parser.parse(fileName);

    Node nodeNeuralNetwork = doc.getDocumentElement();
    if (!nodeNeuralNetwork.getNodeName().equals("neuralNetwork"))
      throw new ParseException(
          "[Error] NN-Load: Parse error in XML file, neural network couldn't be loaded.", 0);
    // nodeNeuralNetwork ok
    // indexNeuralNetworkContent -> indexStructureContent -> indexLayerContent -> indexNeuronContent
    // -> indexNeuralInputContent
    NodeList nodeNeuralNetworkContent = nodeNeuralNetwork.getChildNodes();
    for (int innc = 0; innc < nodeNeuralNetworkContent.getLength(); innc++) {
      Node nodeStructure = nodeNeuralNetworkContent.item(innc);
      if (nodeStructure.getNodeName().equals("structure")) { // for structure element
        NodeList nodeStructureContent = nodeStructure.getChildNodes();
        for (int isc = 0; isc < nodeStructureContent.getLength(); isc++) {
          Node nodeLayer = nodeStructureContent.item(isc);
          if (nodeLayer.getNodeName().equals("layer")) { // for layer element
            NeuralLayer neuralLayer = new NeuralLayer(this);
            this.listLayers.add(neuralLayer);
            NodeList nodeLayerContent = nodeLayer.getChildNodes();
            for (int ilc = 0; ilc < nodeLayerContent.getLength(); ilc++) {
              Node nodeNeuron = nodeLayerContent.item(ilc);
              if (nodeNeuron.getNodeName().equals("neuron")) { // for neuron in layer
                Neuron neuron =
                    new Neuron(
                        Double.parseDouble(((Element) nodeNeuron).getAttribute("threshold")),
                        neuralLayer);
                neuralLayer.listNeurons.add(neuron);
                NodeList nodeNeuronContent = nodeNeuron.getChildNodes();
                for (int inc = 0; inc < nodeNeuronContent.getLength(); inc++) {
                  Node nodeNeuralInput = nodeNeuronContent.item(inc);
                  // if (nodeNeuralInput==null) System.out.print("-"); else System.out.print("*");

                  if (nodeNeuralInput.getNodeName().equals("input")) {
                    //                                        System.out.println("neuron at
                    // STR:"+innc+" LAY:"+isc+" NEU:"+ilc+" INP:"+inc);
                    NeuralInput neuralInput =
                        new NeuralInput(
                            Double.parseDouble(((Element) nodeNeuralInput).getAttribute("weight")),
                            neuron);
                    neuron.listInputs.add(neuralInput);
                  }
                }
              }
            }
          }
        }
      }
    }
  }
Example #2
0
 /**
  * Honey, can you just check on the kids? Thanks.
  *
  * <p>Internal function; not included in reference.
  */
 protected void checkChildren() {
   if (children == null) {
     NodeList kids = node.getChildNodes();
     int childCount = kids.getLength();
     children = new XML[childCount];
     for (int i = 0; i < childCount; i++) {
       children[i] = new XML(this, kids.item(i));
     }
   }
 }
Example #3
0
 /**
  * Gets the definitions of a word, sets the main one if necessary, and returns it.
  *
  * @param overwrite if true, then function will attempt to retrieve from online even if the
  *     definition is already set.
  * @return String containing the main definition of the word.
  */
 public static Word[] getWordsFromFile(ByteArrayInputStream inputStream) {
   try {
     // now to parse the XML
     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
     DocumentBuilder db = dbf.newDocumentBuilder();
     Document doc = db.parse(inputStream);
     removeEmptyTextNodes(doc);
     NodeList wordList = doc.getElementsByTagName("WordDefinitions");
     int wordCount = wordList.getLength();
     Word[] returnWords = new Word[wordCount];
     for (int wordi = 0; wordi < wordCount; wordi++) {
       NodeList list = ((Element) wordList.item(wordi)).getElementsByTagName("Definition");
       Element wordElement = (Element) wordList.item(wordi);
       String word = wordElement.getAttribute("word");
       String mainDefinition = wordElement.getAttribute("mainDefinition");
       int numDefinitions = list.getLength();
       Definition[] wordOtherDefinitions = new Definition[numDefinitions];
       for (int i = 0; i < numDefinitions; i++) {
         Node node = list.item(i);
         NodeList children = node.getChildNodes(); // gets each child node of the definition
         // get all data
         String definition = children.item(XML_DEFINITION_LOC).getTextContent();
         String theWord = children.item(XML_WORD_LOC).getTextContent();
         NodeList dictionaryNode = children.item(XML_DICTIONARY_BRANCH_LOC).getChildNodes();
         String dictionaryId = dictionaryNode.item(XML_DICTIONARY_ID_LOC).getTextContent();
         String dictionaryName = dictionaryNode.item(XML_DICTIONARY_NAME_LOC).getTextContent();
         // now we've got all the data lets add it to the array
         wordOtherDefinitions[i] =
             new Definition(dictionaryName, dictionaryId, definition, theWord);
       }
       if (wordOtherDefinitions.length == 0) {
         wordOtherDefinitions =
             new Definition[] {new Definition("nil", "nil", "NO DEFINITION FOUND", word)};
       }
       Word currentWord = new Word(word, mainDefinition);
       currentWord.setOtherDefinitions(wordOtherDefinitions);
       returnWords[wordi] = currentWord;
     }
     return returnWords;
   } catch (Exception ex) {
     ex.printStackTrace();
     return null;
   }
 }
Example #4
0
 /**
  * Gets the definitions of a word, sets the main one if necessary, and returns it.
  *
  * @param overwrite if true, then function will attempt to retrieve from online even if the
  *     definition is already set.
  * @return String containing the main definition of the word.
  */
 public String getDefinitions(ByteArrayInputStream inputStream, boolean overwrite) {
   if (overwrite || (definition == null || definition.isEmpty())) {
     // get definition from online service
     // if not able to access internet, then do something
     try {
       // now to parse the XML
       DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
       DocumentBuilder db = dbf.newDocumentBuilder();
       Document doc = db.parse(inputStream);
       removeEmptyTextNodes(doc);
       NodeList list = doc.getElementsByTagName("Definition");
       int numDefinitions = list.getLength();
       otherDefinitions = new Definition[numDefinitions];
       for (int i = 0; i < numDefinitions; i++) {
         Node node = list.item(i);
         NodeList children = node.getChildNodes(); // gets each child node of the definition
         // get all data
         String definition = children.item(XML_DEFINITION_LOC).getTextContent();
         String word = children.item(XML_WORD_LOC).getTextContent();
         NodeList dictionaryNode = children.item(XML_DICTIONARY_BRANCH_LOC).getChildNodes();
         String dictionaryId = dictionaryNode.item(XML_DICTIONARY_ID_LOC).getTextContent();
         String dictionaryName = dictionaryNode.item(XML_DICTIONARY_NAME_LOC).getTextContent();
         // now we've got all the data lets add it to the array
         otherDefinitions[i] = new Definition(dictionaryName, dictionaryId, definition, word);
       }
       if (otherDefinitions.length == 0) {
         otherDefinitions =
             new Definition[] {new Definition("nil", "nil", "NO DEFINITION FOUND", word)};
       }
       this.setMainDefinition(otherDefinitions[0].getDefinition());
       return otherDefinitions[0].getDefinition();
     } catch (Exception ex) {
       ex.printStackTrace();
       return "ERROR! " + ex;
     }
   } else {
     return this.definition;
   }
 }