📄 parse.cs
字号:
//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 Parse.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;
using System.Text.RegularExpressions;
using System.Runtime.Serialization;
namespace OpenNLP.Tools.Parser
{
/// <summary>
/// Exception class for problems detected during parsing.
/// </summary>
[Serializable]
public class ParseException : ApplicationException
{
public ParseException()
{
}
public ParseException(string message) : base(message)
{
}
public ParseException(string message, Exception innerException) : base(message, innerException)
{
}
protected ParseException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
/// <summary>
/// Class for holding constituents.
/// </summary>
public class Parse : System.ICloneable, System.IComparable
{
/// <summary>
/// The text string on which this parse is based. This object is shared among all parses for the same sentence.
/// </summary>
private string mText;
/// <summary>
/// The character offsets into the text for this constituent.
/// </summary>
private Util.Span mSpan;
/// <summary>
/// The syntactic type of this parse.
/// </summary>
private string mType;
/// <summary>
/// The sub-constituents of this parse.
/// </summary>
private ArrayList mParts;
/// <summary>
/// The head parse of this parse. A parse can be its own head.
/// </summary>
private Parse mHead;
/// <summary>
/// The outcome assigned to this parse during cconstruction of its parent parse.
/// </summary>
private string mLabel;
/// <summary>
/// The parent parse of this parse.
/// </summary>
private Parse mParent;
/// <summary>
/// The probability associated with the syntactic type assigned to this parse.
/// </summary>
private double mProbability;
/// <summary>
/// The string buffer used to track the derivation of this parse.
/// </summary>
private StringBuilder mDerivation;
#region property accessors and methods for manipulating properties
///<summary>
///Returns the text of the sentence over which this parse was formed.
///</summary>
public virtual string Text
{
get
{
return mText;
}
}
///<summary>
///Returns the character offsets for this constituent.
///</summary>
public virtual Util.Span Span
{
get
{
return mSpan;
}
}
public virtual string Type
{
get
{
return mType;
}
set
{
mType = value;
}
}
/// <summary>
/// The sub-constituents of this parse.
/// </summary>
public virtual Parse[] GetChildren()
{
return (Parse[])mParts.ToArray(typeof(Parse));
}
/// <summary>
/// The number of children for this parse node.
/// </summary>
public int ChildCount
{
get
{
return mParts.Count;
}
}
/// <summary>
/// Replaces the child at the specified index with a new child with the specified label.
/// </summary>
/// <param name="index">
/// The index of the child to be replaced.
/// </param>
/// <param name="label">
/// The label to be assigned to the new child.
/// </param>
public void SetChild(int index, string label)
{
Parse newChild = (Parse) ((Parse)mParts[index]).Clone();
newChild.Label = label;
mParts[index] = newChild;
}
/// <summary>
/// Returns the index of this specified child.
/// </summary>
/// <param name="child">
/// A child of this parse.
/// </param>
/// <returns>
/// the index of this specified child or -1 if the specified child is not a child of this parse.
/// </returns>
public int IndexOf(Parse child)
{
return mParts.IndexOf(child);
}
public virtual Parse Head
{
get
{
return mHead;
}
}
public virtual string Label
{
get
{
return mLabel;
}
set
{
mLabel = value;
}
}
public virtual Parse Parent
{
get
{
return mParent;
}
set
{
mParent = value;
}
}
///<summary>
///Returns the log of the product of the probability associated with all the decisions which formed this constituent.
///</summary>
public virtual double Probability
{
get
{
return mProbability;
}
}
///<summary>
///Adds the specified probability log to this current log for this parse.
///</summary>
///<param name="logProbability">
///The probaility of an action performed on this parse.
///</param>
internal void AddProbability(double logProbability)
{
mProbability += logProbability;
}
public virtual string Derivation
{
get
{
return mDerivation.ToString();
}
}
internal void InitializeDerivationBuffer()
{
mDerivation = new StringBuilder(100);
}
internal void AppendDerivationBuffer(string derivationData)
{
mDerivation.Append(derivationData);
}
public virtual bool IsPosTag
{
get
{
return (mParts.Count == 1 && ((Parse)mParts[0]).Type == MaximumEntropyParser.TokenNode);
}
}
///<summary>Returns whether this parse is complete.</summary>
///<returns>Returns true if the parse contains a single top-most node.</returns>
public virtual bool IsComplete
{
get
{
return (mParts.Count == 1);
}
}
#endregion
#region IClonable implementation
public object Clone()
{
Parse clonedParse = (Parse)base.MemberwiseClone();
clonedParse.mParts = (ArrayList)mParts.Clone();
if (mDerivation != null)
{
clonedParse.InitializeDerivationBuffer();
clonedParse.AppendDerivationBuffer(mDerivation.ToString());
}
return (clonedParse);
}
#endregion
#region IComparable implementation
public virtual int CompareTo(object o)
{
if (!(o is Parse))
{
throw new ArgumentException("A Parse object is required for comparison.");
}
Parse testParse = (Parse) o;
if (this.Probability > testParse.Probability)
{
return - 1;
}
else if (this.Probability < testParse.Probability)
{
return 1;
}
return 0;
}
#endregion
#region constructors
public Parse(string parseText, Util.Span span, string type, double probability)
{
mText = parseText;
mSpan = span;
mType = type;
mProbability = probability;
mHead = this;
mParts = new ArrayList();
mLabel = null;
mParent = null;
}
public Parse(string parseText, Util.Span span, string type, double probability, Parse head) : this(parseText, span, type, probability)
{
mHead = head;
}
#endregion
#region System.Object overrides
public override string ToString()
{
return mText.Substring(mSpan.Start, (mSpan.End) - (mSpan.Start));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -