【问题标题】:List of part of speech tags per sentence with POS Tagger Stanford NPL in C#C# 中带有 POS Tagger Stanford NPL 的每个句子的词性标签列表
【发布时间】:2015-04-19 10:00:59
【问题描述】:

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

例如:“看看那边。看看那辆车!”

有/VB a/DT 看/NN over/IN there/RB ./.看/VB处/IN/DT车/NN !/.

我需要:

  • 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
        }
    } 

反射助手:

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;
}

【问题讨论】:

    标签: c# stanford-nlp pos-tagger


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-23
      • 1970-01-01
      • 2017-11-05
      • 1970-01-01
      • 1970-01-01
      • 2015-06-08
      • 1970-01-01
      • 2013-09-27
      相关资源
      最近更新 更多