本文整理汇总了C#中Sentence.Add方法的典型用法代码示例。如果您正苦于以下问题:C# Sentence.Add方法的具体用法?C# Sentence.Add怎么用?C# Sentence.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Sentence
的用法示例。
在下文中一共展示了Sentence.Add方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RandomProduction
public Production RandomProduction(int maxProductionLength, int numNonterminals, IList<Terminal> terminals, bool useNull = true) {
var lhs = RandomNonterminal(numNonterminals);
var weight = 100 * _rand.NextDouble() + 1.0;
var productionLength = useNull ? _rand.Next(maxProductionLength + 1) + 0: _rand.Next(maxProductionLength + 0) + 1;
Sentence rhs = new Sentence();
for (int i = 0; i < productionLength; i++) {
if (_rand.Next(2) == 0) {
rhs.Add(RandomNonterminal(numNonterminals));
} else {
rhs.Add(RandomTerminal(terminals));
}
}
return new Production(lhs, rhs, weight);
}
示例2: AddWordsToSentence
private static Sentence AddWordsToSentence(List<Word> listWords)
{
Sentence sentence = new Sentence();
foreach (var word in listWords)
{
sentence.Add(word);
}
return sentence;
}
示例3: ParseSentenceString
public static Sentence ParseSentenceString(string source, PunctuationMark endMark)
{
var sentence = new Sentence();
// Get split parts of the sentence
var splitParts = source.Split(new char[] { ' ' });
foreach (var part in splitParts)
{
var sPart = part;
// Get inner punctuation marks in the split part
var innerPunctuationMarks = sPart.GetPunctuationMarks(DefaultPunctuationMarks.InternalPunctuationMarks);
if (innerPunctuationMarks.Any())
{
var sentenceParts = new List<ISingleTextElement>();
var compositeWord = new CompositeWord();
// If split part contains punctuation marks, split this part to single text elements
foreach (var mark in innerPunctuationMarks)
{
var markLenght = mark.StringValue.Length;
var index = sPart.IndexOfPunctuationMark(mark);
var leftPart = sPart.Substring(0, index);
if (!string.IsNullOrEmpty(leftPart))
{
var word = new Word() { StringValue = leftPart };
compositeWord.Add(word);
if (!sentenceParts.Contains(compositeWord))
sentenceParts.Add(compositeWord);
}
if(index == 0)
{
sentenceParts.Add(mark);
}
else if(index + markLenght < sPart.Length)
{
compositeWord.Add(mark);
if (!sentenceParts.Contains(compositeWord))
sentenceParts.Add(compositeWord);
}
else
{
sentenceParts.Add(mark);
}
sPart = sPart.Substring(index + markLenght);
}
if (!string.IsNullOrEmpty(sPart))
{
var word = new Word() { StringValue = sPart };
compositeWord.Add(word);
if (!sentenceParts.Contains(compositeWord))
sentenceParts.Add(compositeWord);
}
// Add single text elements to sentence
foreach(var sentencePart in sentenceParts)
{
if (sentencePart == sentenceParts.Last())
sentencePart.InnerOption = SingleTextElementInnerOption.RightSpace;
sentence.Add(sentencePart);
}
}
else
{
var word = new Word() { StringValue = sPart, InnerOption = SingleTextElementInnerOption.RightSpace };
sentence.Add(word);
}
}
if (endMark.HasValue)
{
if(sentence.Last().InnerOption == SingleTextElementInnerOption.RightSpace)
{
sentence.Last().InnerOption = SingleTextElementInnerOption.None;
}
sentence.Add(endMark);
}
return sentence;
}
示例4: LoadUnanalyzedCorpus
private Sentence[] LoadUnanalyzedCorpus(string resourceName, bool ignoreIncorrect = false)
{
LuMiiCorpus corpus = new LuMiiCorpus();
LuMiiMorphology morphology = new LuMiiMorphology();
Sentence[] sentences = null;
using (Stream stream = this.GetType().Assembly.GetManifestResourceStream(resourceName))
sentences = corpus.Load(stream).ToArray();
List<Sentence> goodSentences = new List<Sentence>();
List<Sentence> ignoredSentences = new List<Sentence>();
List<Token> ignoredTokens = new List<Token>();
Stopwatch watch = new Stopwatch();
watch.Start();
foreach (Sentence sentence in sentences)
{
bool ignore = false;
Sentence analyzedSentence = new Sentence();
foreach (Token token in sentence)
{
Tag[] possibleTags = morphology.Analyze(token.TextTrueCase).ToArray();
if (!possibleTags.Any(t => t.Equals(token.CorrectTag)))
ignore = true;
Token analyzedToken = new Token(token.TextTrueCase, possibleTags, token.CorrectTag, analyzedSentence);
analyzedSentence.Add(analyzedToken);
}
if (!ignoreIncorrect || !ignore)
{
goodSentences.Add(analyzedSentence);
}
else
{
ignoredSentences.Add(analyzedSentence);
}
}
watch.Stop();
Debug.WriteLine(watch.Elapsed);
return goodSentences.ToArray();
}
示例5: TagSpeed
public void TagSpeed()
{
string trainResource = Analyzed2Train;
string testResource = Analyzed2Test;
int maxTokenCount = 1000000;
double minAccuracy = 0.93;
Sentence[] train = LoadAnalyzedCorpus(trainResource);
Sentence[] test = LoadAnalyzedCorpus(testResource);
List<Sentence> all = new List<Sentence>();
int allTokenCount = 0;
while (allTokenCount < maxTokenCount)
{
Sentence s = new Sentence();
foreach (Sentence sentence in test)
{
foreach (Token token in sentence)
{
s.Add(new Token(token));
allTokenCount++;
if (allTokenCount >= maxTokenCount) break;
}
if (allTokenCount >= maxTokenCount) break;
}
all.Add(s);
if (allTokenCount >= maxTokenCount) break;
}
Assert.AreEqual(maxTokenCount, all.SelectMany(t => t).Count());
LuMiiTagger tagger = new LuMiiTagger();
tagger.Train(train);
Stopwatch timer = new Stopwatch();
timer.Start();
tagger.Tag(all);
timer.Stop();
Token[] tokens = all.SelectMany(t => t).ToArray();
double accuracy = (double)tokens.Count(t => t.IsTagCorrect) / tokens.Count();
Assert.AreEqual(maxTokenCount, tokens.Length);
Debug.WriteLine("Accuracy: {0:0.00}%", accuracy * 100);
Debug.WriteLine("Tokens: {0}%", tokens.Length);
Debug.WriteLine("Tag duration: {0} or {1:0} ms", timer.Elapsed, timer.ElapsedMilliseconds);
Debug.WriteLine("Tag speed: {0:0.00} tokens/s", tokens.Length / timer.Elapsed.TotalSeconds);
Assert.Greater(accuracy, minAccuracy);
Assert.Less(accuracy, 0.97);
}
示例6: ParseTextItem
public virtual ITextItem ParseTextItem(string sentence)
{
var result = new Sentence();
var matches = _sentenceParserRegex.Matches(sentence);
foreach (Match match in matches)
result.Add(ParseSentenceItem(match.Value));
return result;
}