Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Statistical parsing of English sentences

, 13 Dec 2006
Shows how to generate parse trees for English language sentences, using a C# port of OpenNLP, a statistical natural language parsing library.
englishparsing_bin.zip
englishparsing_net2_0_bin.zip
englishparsing_net2_0_src.zip
Lithium
Collections
Delegates
Enums
Interfaces
IO
Lithium.csproj.vspscc
LithiumControl.bmp
Shapes
UI
Visitors
ModelConverter
App.ico
ModelConverter.csproj.vspscc
ParseTree
App.ico
ParseTree.csproj.vspscc
ToolsExample
App.ico
ToolsExample.csproj.vspscc
OpenNLP
OpenNLP.csproj.vspscc
SharpEntropy.dll
Tools
Chunker
NameFind
Parser
PosTagger
SentenceDetect
Tokenize
Util
englishparsing_src.zip
Lithium.csproj.user
LithiumControl.bmp
App.ico
ModelConverter.csproj.user
OpenNLP.csproj.user
SharpEntropy.dll
vssver.scc
vssver.scc
vssver.scc
vssver.scc
vssver.scc
vssver.scc
vssver.scc
App.ico
ParseTree.csproj.user
App.ico
ToolsExample.csproj.user
//Copyright (C) 2005 Richard J. Northedge
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

//This file is based on the CheckContextGenerator.java source file found in the
//original java implementation of OpenNLP.  That source file contains the following header:

//Copyright (C) 2003 Thomas Morton
// 
//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.
// 
//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU Lesser General Public License for more details.
// 
//You should have received a copy of the GNU Lesser General Public
//License along with this program; if not, write to the Free Software
//Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

using System;
using System.Collections;
using System.Text;

namespace OpenNLP.Tools.Parser
{
	/// <summary>
	/// Class for generating predictive context for deciding when a constituent is complete.
	/// </summary>
	public class CheckContextGenerator : SharpEntropy.IContextGenerator
	{
		private const string mEndOfSentence = "eos";
		
		/// <summary>
		/// Creates a new context generator for generating predictive context for deciding when a constituent is complete.
		/// </summary>
		public CheckContextGenerator() : base()
		{
		}
		
		public virtual string[] GetContext(object input)
		{
			object[] parameters = (object[]) input;
			return GetContext((Parse[]) parameters[0], (string) parameters[1], ((int) parameters[2]), ((int) parameters[3]));
		}
		
		private void Surround(Parse inputParse, int index, string type, ArrayList features)
		{
			StringBuilder feature = new StringBuilder(20);
			feature.Append("s").Append(index).Append("=");
			if (inputParse != null)
			{
				feature.Append(inputParse.Head.ToString()).Append("|").Append(type).Append("|").Append(inputParse.Head.Type);
			}
			else
			{
				feature.Append(mEndOfSentence).Append("|").Append(type).Append("|").Append(mEndOfSentence);
			}
			features.Add(feature.ToString());
			feature.Length = 0;
			feature.Append("s").Append(index).Append("*=");
			if (inputParse != null)
			{
				feature.Append(type).Append("|").Append(inputParse.Head.Type);
			}
			else
			{
				feature.Append(type).Append("|").Append(mEndOfSentence);
			}
			features.Add(feature.ToString());
		}
		
		private void CheckConstituent(Parse inputParse, string index, string type, ArrayList features)
		{
			StringBuilder feature = new StringBuilder(20);
			feature.Append("c").Append(index).Append("=").Append(inputParse.Type).Append("|").Append(inputParse.Head.ToString()).Append("|").Append(type);
			features.Add(feature.ToString());
			feature.Length = 0;
			feature.Append("c").Append(index).Append("*=").Append(inputParse.Type).Append("|").Append(type);
			features.Add(feature.ToString());
		}
		
		private void CheckConstituent(Parse firstParse, Parse secondParse, string type, ArrayList features)
		{
			StringBuilder feature = new StringBuilder(20);
			feature.Append("cil=").Append(type).Append(",").Append(firstParse.Type).Append("|").Append(firstParse.Head.ToString()).Append(",").Append(secondParse.Type).Append("|").Append(secondParse.Head.ToString());
			features.Add(feature.ToString());
			feature.Length = 0;
			feature.Append("ci*l=").Append(type).Append(",").Append(firstParse.Type).Append(",").Append(secondParse.Type).Append("|").Append(secondParse.Head.ToString());
			features.Add(feature.ToString());
			feature.Length = 0;
			feature.Append("cil*=").Append(type).Append(",").Append(firstParse.Type).Append("|").Append(firstParse.Head.ToString()).Append(",").Append(secondParse.Type);
			features.Add(feature.ToString());
			feature.Length = 0;
			feature.Append("ci*l*=").Append(type).Append(",").Append(firstParse.Type).Append(",").Append(secondParse.Type);
			features.Add(feature.ToString());
		}
		
		/// <summary>
		/// Returns predictive context for deciding whether the specified constituents between the specified start and end index 
		/// can be combined to form a new constituent of the specified type.  
		/// </summary>
		/// <param name="constituents">
		/// The constituents which have yet to be combined into new constituents.
		/// </param>
		/// <param name="type">
		/// The type of the new constituent proposed.
		/// </param>
		/// <param name="firstConstituent">
		/// The first constituent of the proposed constituent.
		/// </param>
		/// <param name="lastConstituent">
		/// The last constituent of the proposed constituent.
		/// </param>
		/// <returns>
		/// The predictive context for deciding whether a new constituent should be created.
		/// </returns>
		public virtual string[] GetContext(Parse[] constituents, string type, int firstConstituent, int lastConstituent)
		{
			int constituentCount = constituents.Length;
			ArrayList features = new ArrayList(100);
			
			//default 
			features.Add("default");
			
			Parse startParse = constituents[firstConstituent];
			Parse endParse = constituents[lastConstituent];
			CheckConstituent(startParse, "begin", type, features);
			CheckConstituent(endParse, "last", type, features);
			StringBuilder production = new StringBuilder(20);
			production.Append(type).Append("->");
			for (int parseIndex = firstConstituent; parseIndex < lastConstituent; parseIndex++)
			{
				Parse testParse = constituents[parseIndex];
				CheckConstituent(testParse, endParse, type, features);
				production.Append(testParse.Type).Append(",");
			}
			production.Append(endParse.Type);
			features.Add(production.ToString());
			Parse previousPreviousParse = null;
			Parse previousParse = null;
			Parse nextParse = null;
			Parse nextNextParse = null;
			if (firstConstituent - 2 >= 0)
			{
				previousPreviousParse = constituents[firstConstituent - 2];
			}
			if (firstConstituent - 1 >= 0)
			{
				previousParse = constituents[firstConstituent - 1];
			}
			if (lastConstituent + 1 < constituentCount)
			{
				nextParse = constituents[lastConstituent + 1];
			}
			if (lastConstituent + 2 < constituentCount)
			{
				nextNextParse = constituents[lastConstituent + 2];
			}
			Surround(previousParse, - 1, type, features);
			Surround(previousPreviousParse, - 2, type, features);
			Surround(nextParse, 1, type, features);
			Surround(nextNextParse, 2, type, features);
			return ((string[]) features.ToArray(typeof(string)));
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Richard Northedge
Web Developer
United Kingdom United Kingdom
Richard Northedge is a senior developer with a UK Microsoft Gold Partner company. He has a postgraduate degree in English Literature, has been programming professionally since 1998 and has been an MCSD since 2000.

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 13 Dec 2006
Article Copyright 2005 by Richard Northedge
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid