title: Some NLP stuff to do with grammar, tagging, stemming, and word sense disambiguation in Python
body: <h1>Background (TLDR; provided for the sake of completion)</h1>
  <p><strong>Seeking advice on an optimal solution to an odd requirement.</strong>
  I'm a (literature) student in my fourth year of college with only my own guidance in programming. I'm competent enough with Python that I won't have trouble implementing solutions I find (most of the time) and developing upon them, but because of my newbness, I'm seeking advice on the <em>best</em> ways I might tackle this peculiar problem.</p>
  <p><strong>Already using NLTK, but differently from the examples in the NLTK book.</strong> I'm already utilizing a lot of stuff from NLTK, particularly WordNet, so that material is not foreign to me. I've read most of the NLTK book.</p>
  <p><strong>I'm working with fragmentary, atomic language.</strong> Users input words and sentence fragments, and WordNet is used to find connections between the inputs, and generate new words and sentences/fragments. My question is about turning an uninflected word from WordNet (a synset) into something that makes sense contextually.</p>
  <p><strong>The problem: How to inflect the result in a grammatically sensible way?</strong> Without any kind of grammatical processing, the results are just a list of dictionary-searchable words, without agreement between words. First step is for my application to stem/pluralize/conjugate/inflect root-words according to context. (The &quot;root words&quot; I'm speaking of are synsets from WordNet and/or their human-readable equivalents.)</p>
  <h1>Example scenario</h1>
  <p>Let's assume we have a chunk of a poem, to which users are adding new inputs to. The new results need to be inflected in a grammatically sensible way.</p>
  <pre><code>The river bears no empty bottles, sandwich papers,   
  Silk handkerchiefs, cardboard boxes, cigarette ends  
  Or other testimony of summer nights. The sprites
  </code></pre>
  <p>Let's say now, it needs to print 1 of 4 possible next words/synsets: <code>['departure', 'to have', 'blue', 'quick']</code>. It seems to me that <code>'blue'</code> should be discarded; <code>'The sprites blue'</code> seems grammatically odd/unlikely. From there it could use either of these verbs.</p>
  <p>If it picks <code>'to have'</code> the result could be sensibly inflected as <code>'had'</code>, <code>'have'</code>, <code>'having'</code>, <code>'will have'</code>, <code>'would have'</code>, etc. (but not <code>'has'</code>). (The resulting line would be something like <code>'The sprites have'</code> and the sensibly-inflected result will provide better context for future results ...)</p>
  <p>I'd like for <code>'depature'</code> to be a valid possibility in this case; while <code>'The sprites departure'</code> doesn't make sense (it's not <code>&quot;sprites'&quot;</code>), <code>'The sprites departed'</code> (or other <em>verb</em> conjugations) would.</p>
  <p>Seemingly <code>'The sprites quick'</code> wouldn't make sense, but something like <code>'The sprites quickly [...]'</code> or <code>'The sprites quicken'</code> could, so <code>'quick'</code> is also a possibility for sensible inflection.</p>
  <h2>Breaking down the tasks</h2>
  <ol>
  <li><strong>Tag part of speech, plurality, tense, etc.</strong> -- of original inputs. Taking note of this could help to select from the several possibilities (i.e. choosing between had/have/having could be more directed than random if a user had inputted <code>'having'</code> rather than some other tense). I've heard the Stanford POS tagger is good, which has an implementation in NLTK. I am not sure how to handle tense detection here.</li>
  <li><strong>Consider context in order to rule out grammatically peculiar possibilities.</strong> Consider the last couple words and their part-of-speech tags (and tense?), as well as sentence boundaries if any, and from that, drop things that wouldn't make sense. After <code>'The sprites'</code> we don't want another article (or determiner, as far as I can tell), nor an adjective, but an adverb or verb could work. Comparison of the current stuff with sequences in tagged corpora (and/or Markov chains?) -- or consultation of grammar-checking functions -- could provide a solution for this.</li>
  <li><strong>Select a word from the remaining possibilities</strong> (those that could be inflected sensibly). This isn't something I need an answer for -- I've got my methods for this. Let's say it's randomly selected.</li>
  <li><strong>Transform the selected word as needed</strong>. If the information from #1 can be folded in (for example, perhaps the &quot;pluralize&quot; flag was set to True), do so. If there are several possibilities (e.g. picked word is a verb, but a few tenses are possible) select, randomly. Regardless I'm going to need to morph/inflect the word.</li>
  </ol>
  <p>I'm looking for advice on the soundness of this routine, as well as suggestions for steps to add. Ways to break down these steps further would also be helpful. Finally I'm looking for suggestions on what tool might best accomplish each task.</p>