我用C#开发了NLP程序。程序是关于从字符串中查找人名。现在我想找到像(先生)一样的人称号。
我使用useTitle和useTitle1属性,但没有成功。
这是我的代码
public class Parser
{
CRFClassifier Classifier =
CRFClassifier.getClassifierNoExceptions(
@"C:\Users\manoj.sharma\Desktop\Stanford.NLP\Stanford.NLP\bin\english.all.3class.distsim.crf.ser.gz");
public object[] Parse(string docText)
{
//SeqClassifierFlags flags = new SeqClassifierFlags();
//flags.useTitle = true;
//flags.useTitle2 = true;
//Classifier.flags = flags;
Classifier.flags.useTitle2 = true;
//Classifier.flags.useTitle = true;
var classified = Classifier.classifyToCharacterOffsets(docText).toArray();
return classified;
}
}
static void Main(string[] args)
{
try
{
var docText = "Microsoft was founded by Bill Gates and Mrs. Paul Allen and Mr Devender and Infosys and India on April 4, 1975, to develop and sell BASIC interpreters for the Altair 8800.";
var classified = new Parser().Parse(docText);
for (int i = 0; i < classified.Length; i++)
{
Triple triple = (Triple)classified[i];
int second = Convert.ToInt32(triple.second().ToString());
int third = Convert.ToInt32(triple.third().ToString());
if (triple.first().ToString() == "PERSON")
{
//second = second - 5;
if (Regex.IsMatch(docText.Substring((second - 5), 5), @"(^|\s)Mr(\s|$)"))
{
// second = second + 5;
Console.WriteLine(triple.first().ToString() + '\t' + "Mr. " + docText.Substring(second, third - second));
}
else if (Regex.IsMatch(docText.Substring((second - 5), 5), @"(^|\s)Mrs(\s|$)"))
{
// second = second + 5;
Console.WriteLine(triple.first().ToString() + '\t' + "Mrs. " + docText.Substring(second, third - second));
}
else if (Regex.IsMatch(docText.Substring((second - 5), 5), @"(^|\s)Miss(\s|$)"))
{
// second = second + 5;
Console.WriteLine(triple.first().ToString() + '\t' + "Miss. " + docText.Substring(second, third - second));
}
else
{
// second = second + 5;
Console.WriteLine(triple.first().ToString() + '\t' + docText.Substring(second, third - second));
}
}
else
{
Console.WriteLine(triple.first().ToString() + '\t' + docText.Substring(second, third - second));
}
}
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
为什么要在NLP或NER中找到人称号?如果可能的话,请提供任何示例。