A first adventure in Windows batch scripting.

Scripting on a Unix terminal is so darn easy – the tools are documented in the man pages, online or from a buddy on the phone. Windows batch scripting was another beast entirely. It took me nearly an hour to craft this simple script, which honestly would have been as easy as a more/grep/awk pipe on Unix.

Anyway, here is my script to search for a string in a file, and print out the key to said file. Before I reveal the script, this is what my data looks like. I’m trying to pull out and echo just aedfc45.

    IP:port                 : 0.0.0.0
    Target                  : aedfc45
    Application ID          : {00000000-0000-0000-0000-000000000000}
    Certificate Store Name  : MY

I’m sure my solution is not the best (this is my first batch script, ever). Note, I broke this single line in two in case you try running it with your own inputs.

more test.txt | for /F "tokens=4 delims= " %X in ('findstr /c:"Target"') 
    do echo %X

What was difficult to understand was that the for command was handling much more than I felt it should be handling. It was not only looping over input, applying the command in single quotes to each line, it was also delimiting and tokenizing the output with the “tokens” and “delims” commands contained in double quotes. What a strange implementation of for.

Posted in Programming | Tagged | Comments closed

Wordnet Vocabulary & Definitions

This was taken from http://en.wikipedia.org/wiki/WordNet on September 7th, 2010 because, well, I just needed it quickly accessible.

Nouns

hypernyms Y is a hypernym of X if every X is a (kind of) Y canine is a hypernym of dog, because every dog is a member of the larger category of canines
hyponyms Y is a hyponym of X if every Y is a (kind of) X dog is a hyponym of canine
holonym Y is a holonym of X if X is a part of Y building is a holonym of window
meronym Y is a meronym of X if Y is a part of X window is a meronym of building

Verbs

hypernym the verb Y is a hypernym of the verb X if the activity X is a (kind of) Y to perceive is an hypernym of to listen
troponym the verb Y is a troponym of the verb X if the activity Y is doing X in some manner to lisp is a troponym of to talk
entailment the verb Y is entailed by X if by doing X you must be doing Y to sleep is entailed by to snore
coordinate term those verbs sharing a common hypernym to lisp and to yell
Posted in NLP | Tagged , | Comments closed

Serializing Stanford Parser Objects.

Recently, I found the need to serialize Stanford Parser objects to a file. Though I was familiar with the concept of serialization, I had never done such a thing in Java before. The following is an example on how to do just that: serializing Stanford Parser Tree objects to a file.

The example accomplishes the simple task of lexing and parsing a simple sentence. Instead of printing the tree right now, though, we’ll serialize it to a file for use later on.

import java.io.CharArrayReader;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.List;
import edu.stanford.nlp.trees.*;
import edu.stanford.nlp.ling.HasWord;
import edu.stanford.nlp.parser.lexparser.LexicalizedParser;
import edu.stanford.nlp.process.Tokenizer;
 
class Serialize {
   public static void main(String[] args) {
      LexicalizedParser lp = 
        new LexicalizedParser("englishPCFG.ser.gz");
      TreebankLanguagePack tlp =
        new PennTreebankLanguagePack();
      lp.setOptionFlags(
        new String[] { "-maxLength", "80", "-retainTmpSubcategories" });
      Tree parse = null;
 
      CharArrayReader reader = 
        new CharArrayReader("Cows eat grain.".toCharArray());
      Tokenizer<? extends HasWord> toke = 
        tlp.getTokenizerFactory().getTokenizer(reader);
      List<? extends HasWord> wordList = toke.tokenize();
 
      if (lp.parse(wordList)) {
         parse = lp.getBestParse();
      }
 
      try {
         /*
          * The following serializes the Tree parse object
          *  to the file serialized.obj.  Later on, we'll
          *  read this file in another JVM.
          */
         FileOutputStream fos = new FileOutputStream("serialized.obj");
         ObjectOutputStream oos = new ObjectOutputStream(fos);
         oos.writeObject(parse);
         oos.flush();
         oos.close();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

At this point we’ve created a binary file named serialized.obj. This file contains the contents of the Stanford Parser Tree object. We can deserialize the object and continue using it as before.

import java.io.FileInputStream;
import java.io.ObjectInputStream;
import edu.stanford.nlp.trees.*;
class Deserialize {
   public static void main(String[] args) {
      Tree parse = null;
      try {
         /*
          * The following deserializes a Stanford Parser
          *  Tree object that we created previously.
          */
         FileInputStream fis = new FileInputStream("object.bin");
         ObjectInputStream ois = new ObjectInputStream(fis);
         parse = (Tree) ois.readObject();
         ois.close();
         parse.pennPrint();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}
Posted in NLP, Programming | Tagged , , | Comments closed