通过IronPython在C#中使用NLTK

Mat*_*ves 9 c# python ironpython nltk

我正在使用Visual Studio 2010.我有一个IronPython控制台项目和一个C#控制台项目.这个IronPython脚本在我自己运行时工作正常:

import nltk

def Simple():
    baconIpsumFile = open('baconipsum.txt', 'r')
    baconIpsumCorpus = baconIpsumFile.read()

    tokens = nltk.word_tokenize(baconIpsumCorpus)
    text = nltk.Text(tokens)
    print text
Run Code Online (Sandbox Code Playgroud)

这是C#控制台程序,它不能正常工作:

using IronPython.Hosting;

namespace IronNLTK.CSharp.Console
{
    class Program
    {
        static void Main(string[] args)
        {
            var ipy = Python.CreateRuntime();
            dynamic test = ipy.UseFile("C:\\Path\\To\\Program.py");
            test.Simple();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我得到了ImportException: No module named nltk.我错过了什么?

Ore*_*zor 3

听起来您需要更新 sys.path 以指向 NLTK 所在的位置。

看看这个:Importing external module in IronPython