how to integrate nltk with C# using ironpython

1,889 views
Skip to first unread message

suralk

unread,
Feb 28, 2012, 1:03:03 AM2/28/12
to nltk-users
Hi All,
I am trying to integrate nltk features with my framework written in
C#. I am trying to use Ironpython for this. But this integration dos
not work and Ironpython complains that nltk module cannot be found.
I searched for this and seems like it has something to do with the
python subprocess module. However, I could not find a way to fix this
problem.
Hope someone can help me out.

Thanks
/Sura

surangika ranathunga

unread,
Feb 28, 2012, 4:21:30 PM2/28/12
to nltk-...@googlegroups.com, Alexis Dimitriadis
Normal python integration works just fine for me.
I think it is some specific python code that nltk uses, that fails this integration.
See this previous post in nltk forum. It does not give a clear answer to the problem though.

http://groups.google.com/group/nltk-users/browse_thread/thread/78469a621a3d6b55/9a71416450d7a566?lnk=gst&q=ironpython#9a71416450d7a566

--- On Tue, 28/2/12, Alexis Dimitriadis <A.Dimi...@uu.nl> wrote:

From: Alexis Dimitriadis <A.Dimi...@uu.nl>
Subject: Re: [nltk-users] how to integrate nltk with C# using ironpython
To: nltk-...@googlegroups.com
Cc: "suralk" <lady_...@yahoo.com>
Received: Tuesday, 28 February, 2012, 2:32 AM

Sounds like you have a C# integration problem, not an nltk problem. If you can integrate the following python module into your code (as module hello), I would expect the nltk to work too.

---  hello.py

print "hello world"
--------

Alexis
-- _____________________________________________

Alexis Dimitriadis
A.Dimi...@uu.nl
+31-30-253-6568

Utrecht Institute of Linguistics OTS
Trans 10
3512 JK Utrecht
The Netherlands

Alexis Dimitriadis

unread,
Feb 29, 2012, 4:10:15 AM2/29/12
to surangika ranathunga, nltk-...@googlegroups.com
Sorry if I misunderstood your problem. The thread talks about failure to import the subprocess module. But if Ironpython "complains that the nltk module cannot be found", it hasn't gotten far enough to have any problems with its contents.

Please give a more specific description of your problem, and maybe someone can help you.

Alexis

suralk

unread,
Feb 29, 2012, 5:16:06 PM2/29/12
to nltk-users
Just to see whether NLTK would work with C#, I wrote a very simple
python file that imports nltk, and tried to call a method in that
python file. I am using Ironpython 2.
I got the following error stack, and have no idea of how to fix it


IronPython.Runtime.Exceptions.ImportException: No module named nltk
at Microsoft.Scripting.Runtime.LightExceptions.CheckAndThrow(Object
value)
at
Microsoft.Scripting.Interpreter.FuncCallInstruction`2.Run(InterpretedFrame
frame)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame
frame)
at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0
arg0, T1 a
rg1)
at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)
at IronPython.Compiler.PythonScriptCode.Run(Scope scope)
at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)
at IronPython.Compiler.RuntimeScriptCode.Run(Scope scope)
at Microsoft.Scripting.SourceUnit.Execute(Scope scope, ErrorSink
errorSink)
at Microsoft.Scripting.Hosting.ScriptSource.Execute(ScriptScope
scope)
at Microsoft.Scripting.Hosting.ScriptEngine.ExecuteFile(String
path)
at Microsoft.Scripting.Hosting.ScriptRuntime.ExecuteFile(String
path)
at Microsoft.Scripting.Hosting.ScriptRuntime.UseFile(String path)
at readerwriterforOVH.Program.Main(String[] args) in D:\PhD Work
\Research\lib
omv\test\readerwriterforOVH\readerwriterforOVH\Program.cs:line 105


Steven Bird

unread,
Feb 29, 2012, 5:40:42 PM2/29/12
to nltk-...@googlegroups.com
It looks like you need to tell IronPython where to find its libraries,
or else install NLTK in a place where IronPython looks.

> --
> You received this message because you are subscribed to the Google Groups "nltk-users" group.
> To post to this group, send email to nltk-...@googlegroups.com.
> To unsubscribe from this group, send email to nltk-users+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/nltk-users?hl=en.
>

suralk

unread,
Mar 7, 2012, 1:14:59 AM3/7/12
to nltk-users
Hi Steven, yes you were right. I had to manually set sys.path in my
python script to include the path to nltk. I also had (for some
reason) to include the path to Ironpython in sys.path as well.
Also generic numpy used by nltk does not work with Ironpython. They
have their own implementation of numpy and you to use this for nltk to
work with C#. you also have to download subprocess.py.

On Mar 1, 11:40 am, Steven Bird <stevenbi...@gmail.com> wrote:
> It looks like you need to tell IronPython where to find its libraries,
> or else install NLTK in a place where IronPython looks.
>
Reply all
Reply to author
Forward
0 new messages