title: Searching Natural Language Sentence Structure
body: <p>What's the best way to store and search a database of natural language sentence structure  trees?</p>
  
  <p>Using <a href="http://incubator.apache.org/opennlp/" rel="noreferrer">OpenNLP's</a> English Treebank Parser, I can get fairly reliable sentence structure parsings for arbitrary sentences. What I'd like to do is create a tool that can extract all the doc strings from my source code, generate these trees for all sentences in the doc strings, store these trees and their associated function name in a database, and then allow a user to search the database using natural language queries.</p>
  
  <p>So, given the sentence <code>"This uploads files to a remote machine."</code> for the function <code>upload_files()</code>, I'd have the tree:</p>
  
  <pre><code>(TOP
    (S
      (NP (DT This))
      (VP
        (VBZ uploads)
        (NP (NNS files))
        (PP (TO to) (NP (DT a) (JJ remote) (NN machine))))
      (. .)))
  </code></pre>
  
  <p>If someone entered the query "How can I upload files?", equating to the tree:</p>
  
  <pre><code>(TOP
    (SBARQ
      (WHADVP (WRB How))
      (SQ (MD can) (NP (PRP I)) (VP (VB upload) (NP (NNS files))))
      (. ?)))
  </code></pre>
  
  <p>how would I store and query these trees in a SQL database?</p>
  
  <p>I've written a simple proof-of-concept script that can perform this search using a mix of regular expressions and network graph parsing, but I'm not sure how I'd implement this in a  scalable way.</p>
  
  <p>And yes, I realize my example would be trivial to retrieve using a simple keyword search. The idea I'm trying to test is how I might take advantage of grammatical structure, so I can weed-out entries with similar keywords, but a different sentence structure. For example, with the above query, I wouldn't want to retrieve the entry associated with the sentence <code>"Checks a remote machine to find a user that uploads files."</code> which has similar keywords, but is obviously describing a completely different behavior.</p>