简体   繁体   English

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

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

Using the POS Tagger of Stanford NPL .NET, I'm trying to extract a detailed list of part of speech tags per sentence. 我试图使用Stanford NPL .NET的POS Tagger,提取每个句子的部分语音标签的详细列表。

eg: "Have a look over there. Look at the car!" 例如:“去那儿看看。看看车!”

Have/VB a/DT look/NN over/IN there/RB ./. 具有/ VB a / DT外观/ NN over / IN那里/ RB ./。 Look/VB at/IN the/DT car/NN !/. 在/ DT车/ NN!/处/查看VB。

I need: 我需要:

  • POS Text: "Have" POS文本:“有”
  • POS tag: "VB" POS标签:“ VB”
  • Position in the original text 原文位置

I managed to achieve this by accessing the private fields of the result via reflection. 我设法通过反射访问结果的私有字段来实现这一点。

I know it's ugly, not efficient and very bad, but that's the only I found until know. 我知道这很丑陋,效率不高而且非常糟糕,但这是我唯一发现的,直到知道。 Hence my question; 因此,我的问题是: is there any built-in way to access such information? 有没有内置的方式来访问这些信息?

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

The solution is pretty easy. 解决方案非常简单。 Just cast the part of speech (taggedSentence[index]) to a TaggedWord. 只需将词性(taggedSentence [index])转换为TaggedWord。 You can then easily access these properties from the getters beginPosition(), endPosition(), tag() and value(). 然后,您可以从getter beginPosition(),endPosition(),tag()和value()轻松访问这些属性。

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

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