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
using System;
using System.Diagnostics;
using System.Collections;
namespace Netron.Lithium
{
	/// <summary>
	/// 
	/// </summary>
	public class GraphAbstract
	{

		#region Fields

		/// <summary>
		/// the description of the diagram,
		/// can be expanded to much more and whatever you wish to store about author etc...
		/// </summary>
		protected string description = "No description";

		/// <summary>
		/// the collection of shapes on the canvas
		/// </summary>
		protected ShapeCollection shapes;

		/// <summary>
		/// the collection of connections on the canvas
		/// </summary>
		protected ConnectionCollection connections;

		/// <summary>
		/// the root of the diagram
		/// </summary>
		protected ShapeBase root;

		#endregion

		#region Properties

		/// <summary>
		/// Gets or sets the root of the diagram
		/// </summary>
		public ShapeBase Root
		{
			get{return root;}
			set{root = value;}
		}

		/// <summary>
		/// The description of the graph
		/// </summary>
		public string Description
		{
			get{return description;}
			set{description = value;}
		}

		/// <summary>
		/// Gets or sets the shape collection
		/// </summary>
		public ShapeCollection Shapes
		{
			get{return shapes;}
			set{shapes = value;}
		}

		/// <summary>
		/// Gets or sets the connection collection of the control
		/// </summary>
		public ConnectionCollection Connections
		{
			get{return connections;}
			set{connections = value;}
		}

		#endregion

		#region Constructor

		/// <summary>
		/// Default ctor
		/// </summary>
		public GraphAbstract()
		{
			connections = new ConnectionCollection();
			shapes = new ShapeCollection();
		}

		#endregion

		#region Methods
		#region Traversals

		/// <summary>
		/// BFT of the diagram with the given visitor, starting from the given shape
		/// </summary>
		/// <param name="visitor"></param>
		/// <param name="shape"></param>
		public void BreadthFirstTraversal(IVisitor visitor, ShapeBase shape)
		{
			for (int i = 0; i < shapes.Count; i++)
			{
				shapes[i].visited = false;
			}
			BFT(visitor, shape);
		}
		/// <summary>
		/// BFT of the diagram starting from the root
		/// </summary>
		/// <param name="visitor"></param>
		public void BreadthFirstTraversal(IVisitor visitor)
		{
		
			if(root==null) return;

			for (int i = 0; i < shapes.Count; i++)
			{
				shapes[i].visited = false;
			}
			BFT(visitor, root);
		}

		/// <summary>
		/// BFT of the diagram with the given visitor, starting from the given shape
		/// </summary>
		/// <param name="visitor"></param>
		/// <param name="shape"></param>
		private  void BFT(IVisitor visitor, ShapeBase shape)
		{			
		
			Queue queue = new Queue();
			queue.Enqueue(shape);
			shape.visited = true;
			while (!(queue.Count==0 || visitor.IsDone))
			{
				ShapeBase node1 = queue.Dequeue() as ShapeBase;
				visitor.Visit(node1);		
				node1.visited = true;
				
				try
				{
					foreach(ShapeBase sh in node1.childNodes)
					{
						
						if (!sh.visited)
						{
							queue.Enqueue(sh);
							//visitor.Visit(sh);							
							//sh.visited= true;
						}
					}

				}
				catch(Exception exc)
				{
					Trace.WriteLine(exc.Message);
				}
				//visitor.PostVisit(node1);
			}
		}

		/// <summary>
		/// DFT of the (sub)graph starting from the given shape
		/// </summary>
		/// <param name="visitor"></param>
		/// <param name="shape"></param>
		private void DFT(IVisitor visitor, ShapeBase shape)
		{
			if (!visitor.IsDone)
			{
				if(typeof(IPrePostVisitor).IsInstanceOfType(visitor))
					(visitor as IPrePostVisitor).PreVisit(shape);

				visitor.Visit(shape);
				shape.visited = true;				
				try
				{
					foreach(ShapeBase sh in shape.childNodes)
					{						
						if (!sh.visited)
						{
							DepthFirstTraversal(visitor, sh);
						}
					}
				}
				catch(Exception exc)
				{
					Trace.WriteLine(exc.Message);
				}

				if(typeof(IPrePostVisitor).IsInstanceOfType(visitor))
					(visitor as IPrePostVisitor).PostVisit(shape);
			}
		}

		/// <summary>
		/// DFT of the (sub)graph starting from the given shape
		/// </summary>
		/// <param name="visitor">an IVisitor object</param>
		/// <param name="shape">the shape to start the visiting process from</param>
		public void DepthFirstTraversal(IVisitor visitor, ShapeBase shape)
		{
		
			for (int i = 0; i < shapes.Count; i++)
			{
				shapes[i].visited = false;
			}
			DFT(visitor, shape);
		}
		/// <summary>
		/// DFT of the diagram starting from the root
		/// </summary>
		/// <param name="visitor"></param>
		public void DepthFirstTraversal(IVisitor visitor)
		{
		
			if(root==null) return;

			for (int i = 0; i < shapes.Count; i++)
			{
				shapes[i].visited = false;
			}
			DFT(visitor, root);
		}
		
		#endregion
		#endregion
	}
}

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

Share

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
Web04 | 2.8.140827.1 | Last Updated 13 Dec 2006
Article Copyright 2005 by Richard Northedge
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid