【发布时间】: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