本文整理汇总了C#中Parse.Insert方法的典型用法代码示例。如果您正苦于以下问题:C# Parse.Insert方法的具体用法?C# Parse.Insert怎么用?C# Parse.Insert使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Parse
的用法示例。
在下文中一共展示了Parse.Insert方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ParseLine
/// <summary>
/// Parses the specified <see cref="ISentence"/> object using a given <paramref name="parser"/>.
/// </summary>
/// <param name="sentence">The sentence to be parsed.</param>
/// <param name="parser">The parser.</param>
/// <param name="numParses">The number parses. Usually 1.</param>
/// <returns>An array with the parsed results.</returns>
/// <exception cref="System.ArgumentNullException">
/// <paramref name="sentence"/>
/// or
/// <paramref name="parser"/>
/// </exception>
/// <exception cref="System.ArgumentOutOfRangeException">numParses</exception>
/// <exception cref="System.InvalidOperationException">The sentence is not tokenized.</exception>
public static Parse[] ParseLine(ISentence sentence, IParser parser, int numParses) {
if (sentence == null)
throw new ArgumentNullException("sentence");
if (parser == null)
throw new ArgumentNullException("parser");
if (numParses < 0)
throw new ArgumentOutOfRangeException("numParses");
if (sentence.Tokens == null || sentence.Tokens.Count == 0)
throw new InvalidOperationException("The sentence is not tokenized.");
var sb = new StringBuilder(sentence.Length);
for (var i = 0; i < sentence.Tokens.Count; i++) {
sb.Append(sentence.Tokens[i].Lexeme).Append(' ');
}
sb.Remove(sb.Length - 1, 1);
var start = 0;
var p = new Parse(sb.ToString(), new Span(0, sb.Length), AbstractBottomUpParser.INC_NODE, 0, 0);
for (var i = 0; i < sentence.Tokens.Count; i++) {
p.Insert(
new Parse(
sb.ToString(),
new Span(start, start + sentence.Tokens[i].Lexeme.Length),
AbstractBottomUpParser.TOK_NODE, 0, i));
start += sentence.Tokens[i].Lexeme.Length + 1;
}
return numParses == 1 ? new[] { parser.Parse(p) } : parser.Parse(p, numParses);
}
示例2: AddParseEvents
/// <summary>
/// Produces all events for the specified sentence chunks and adds them to the specified list.
/// </summary>
/// <param name="newEvents">A list of events to be added to.</param>
/// <param name="chunks">Pre-chunked constituents of a sentence.</param>
protected override void AddParseEvents(List<Event> newEvents, Parse[] chunks) {
/** Frontier nodes built from node in a completed parse. Specifically,
* they have all their children regardless of the stage of parsing.*/
var rightFrontier = new List<Parse>();
var builtNodes = new List<Parse>();
/** Nodes which characterize what the parse looks like to the parser as its being built.
* Specifically, these nodes don't have all their children attached like the parents of
* the chunk nodes do.*/
var currentChunks = new Parse[chunks.Length];
for (var ci = 0; ci < chunks.Length; ci++) {
currentChunks[ci] = (Parse) chunks[ci].Clone();
currentChunks[ci].PreviousPunctuationSet = chunks[ci].PreviousPunctuationSet;
currentChunks[ci].NextPunctuationSet = chunks[ci].NextPunctuationSet;
currentChunks[ci].Label = AbstractBottomUpParser.COMPLETE;
chunks[ci].Label = AbstractBottomUpParser.COMPLETE;
}
for (var ci = 0; ci < chunks.Length; ci++) {
//System.err.println("parserEventStream.addParseEvents: chunks="+Arrays.asList(chunks));
var parent = chunks[ci].Parent;
var prevParent = chunks[ci];
//var off = 0;
//build un-built parents
if (!chunks[ci].IsPosTag) {
builtNodes.Add(chunks[ci]);
//builtNodes[off++] = chunks[ci];
}
//perform build stages
while (parent.Type != AbstractBottomUpParser.TOP_NODE && parent.Label == null) {
if (parent.Label == null && prevParent.Type != parent.Type) {
//build level
// if (debug) System.err.println("Build: " + parent.Type + " for: " + currentChunks[ci]);
if (Type == ParserEventTypeEnum.Build) {
newEvents.Add(new Event(parent.Type, buildContextGenerator.GetContext(currentChunks, ci)));
}
builtNodes.Add(parent);
//builtNodes[off++] = parent;
var newParent = new Parse(currentChunks[ci].Text, currentChunks[ci].Span, parent.Type, 1, 0);
newParent.Add(currentChunks[ci], Rules);
newParent.PreviousPunctuationSet = currentChunks[ci].PreviousPunctuationSet;
newParent.NextPunctuationSet = currentChunks[ci].NextPunctuationSet;
currentChunks[ci].Parent = newParent;
currentChunks[ci] = newParent;
newParent.Label = Parser.BUILT;
//see if chunk is complete
if (LastChild(chunks[ci], parent)) {
if (Type == ParserEventTypeEnum.Check) {
newEvents.Add(new Event(AbstractBottomUpParser.COMPLETE,
checkContextGenerator.GetContext(currentChunks[ci], currentChunks, ci, false)));
}
currentChunks[ci].Label = AbstractBottomUpParser.COMPLETE;
parent.Label = AbstractBottomUpParser.COMPLETE;
} else {
if (Type == ParserEventTypeEnum.Check) {
newEvents.Add(new Event(AbstractBottomUpParser.INCOMPLETE,
checkContextGenerator.GetContext(currentChunks[ci], currentChunks, ci, false)));
}
currentChunks[ci].Label = AbstractBottomUpParser.INCOMPLETE;
parent.Label = AbstractBottomUpParser.COMPLETE;
}
chunks[ci] = parent;
//System.err.println("build: "+newParent+" for "+parent);
}
//TODO: Consider whether we need to set this label or train parses at all.
parent.Label = Parser.BUILT;
prevParent = parent;
parent = parent.Parent;
}
//decide to attach
if (Type == ParserEventTypeEnum.Build) {
newEvents.Add(new Event(Parser.DONE, buildContextGenerator.GetContext(currentChunks, ci)));
}
//attach node
string attachType = null;
/** Node selected for attachment. */
Parse attachNode = null;
var attachNodeIndex = -1;
if (ci == 0) {
var top = new Parse(currentChunks[ci].Text, new Span(0, currentChunks[ci].Text.Length),
AbstractBottomUpParser.TOP_NODE, 1, 0);
top.Insert(currentChunks[ci]);
} else {
/** Right frontier consisting of partially-built nodes based on current state of the parse.*/
var currentRightFrontier = Parser.GetRightFrontier(currentChunks[0], Punctuation);
if (currentRightFrontier.Count != rightFrontier.Count) {
throw new InvalidOperationException("frontiers mis-aligned: " + currentRightFrontier.Count +
" != " + rightFrontier.Count + " " + currentRightFrontier +
" " + rightFrontier);
//System.exit(1);
//.........这里部分代码省略.........