繁体   English   中英

使用C#的POS Tagger Stanford NPL每个句子的部分语音标签列表

[英]List of part of speech tags per sentence with POS Tagger Stanford NPL in C#

我试图使用Stanford NPL .NET的POS Tagger,提取每个句子的部分语音标签的详细列表。

例如:“去那儿看看。看看车!”

具有/ VB a / DT外观/ NN over / IN那里/ RB ./。 在/ DT车/ NN!/处/查看VB。

我需要:

  • POS文本:“有”
  • POS标签:“ VB”
  • 原文位置

我设法通过反射访问结果的私有字段来实现这一点。

我知道这很丑陋,效率不高而且非常糟糕,但这是我唯一发现的,直到知道。 因此,我的问题是: 有没有内置的方式来访问这些信息?

using (var streamReader = new StringReader(rawText))
{
    var tokenizedSentences = MaxentTagger.tokenizeText(streamReader).toArray();

    foreach (ArrayList tokenizedSentence in tokenizedSentences)
    {
        var taggedSentence = _posTagger.tagSentence(tokenizedSentence).toArray();

        for (int index = 0; index < taggedSentence.Length; index++)
        {
            var partOfSpeech = ((StringLabel) (taggedSentence[index]));
            var posText = partOfSpeech.value();

            var posTag = ReflectionHelper.GetInstanceField(typeof (TaggedWord), partOfSpeech, "tag") as string;
            var posBeginPosition = (int)ReflectionHelper.GetInstanceField(typeof (StringLabel), partOfSpeech, "beginPosition");
            var posEndPosition = (int)ReflectionHelper.GetInstanceField(typeof (StringLabel), partOfSpeech, "endPosition");

            // process the pos
        }
    } 

ReflectionHelper:

public static object GetInstanceField<T>(T instance, string fieldName)
{
    const BindingFlags bindFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static;

    object result = null;
    var field = typeof(T).GetField(fieldName, bindFlags);
    if (field != null)
    {
        result = field.GetValue(instance);
    }
    return result;
}

解决方案非常简单。 只需将词性(taggedSentence [index])转换为TaggedWord。 然后,您可以从getter beginPosition(),endPosition(),tag()和value()轻松访问这些属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM