Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 516
Matthew Groves Avatar asked Sep 16 '11 15:09

Matthew Groves


2 Answers

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

check this out: Importing external module in IronPython

like image 145
Oren Mazor Avatar answered Sep 28 '22 03:09

Oren Mazor


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

like image 27
reti Avatar answered Sep 28 '22 01:09

reti