Using NLTK in C# via IronPython
Asked Answered
R

2

9

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?

Recent answered 16/9, 2011 at 15:29 Comment(2)
Can you interact with the ipy runtime and adjust the path there?Pettis
I've also tried that. I.e. the accepted answer in #1372494Recent
D
3

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

check this out: Importing external module in IronPython

Dermatitis answered 16/9, 2011 at 15:59 Comment(4)
if you mean something like sys.path.append(r"c:\Program Files (x86)\IronPython\Lib") that doesn't work eitherRecent
Of course that does not work. Backslash is the string escape character. If you want to put a path in a Python string, then you need to do C:/Progr... or C:\\Progr...Coonan
In addition, it is not unusual for portable software to have a problem with paths that contain spaces in the name. Have you tried installing another instance of IronPython in C:\IronPython so that you don't have any directories with space in the names?Coonan
Yes, sorry, I did use escaped backslashes, I'll look into the spaces in the path name though...Recent
N
2

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

Nnw answered 13/10, 2017 at 22:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.