Matthew Groves
Matthew Groves

Reputation: 26096

Using NLTK in C# via IronPython

I'm using Visual Studio 2010. I have an IronPython console project and a C# console project. This IronPython script works fine when I run it by itself:

import nltk

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

    tokens = nltk.word_tokenize(baconIpsumCorpus)
    text = nltk.Text(tokens)
    print text

Here is the C# console program, which does not work fine:

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

I get an ImportException: No module named nltk. What am I missing?

Upvotes: 9

Views: 8125

Answers (2)

reti
reti

Reputation: 67

Awesome news, Visual Studio 2017 is embedded with Anaconda’s Python distribution which has NTLK and other machine learning packages.

Upvotes: 2

Oren Mazor
Oren Mazor

Reputation: 4477

sounds like you need to update sys.path to point to wherever NLTK lives.

check this out: Importing external module in IronPython

Upvotes: 3

Related Questions