13

I want to use the nltk libraries in c++.

Is there a glue language/mechanism I can use to do this?

Reason: I havent done any serious programming in c++ for a while and want to revise NLP concepts at the same time.

Thanks

4 Answers 4

18

Although calling c++ libs from python is more normal - you can call a python module from c++ by bascially calling the python intepreter and have it execute the python source. This is called embedding

Alternatively the boost.python library makes it very easy.

Sign up to request clarification or add additional context in comments.

1 Comment

embedding doc has moved docs.python.org/2.7/extending/embedding.html, I've suggested an edit.
17

You can also try the Boost.Python library; which has this capability. This library is mainly used to expose C++ to Python, but can be used the other way around.

2 Comments

Thanks for nice resource. It solved several tiresome hours of me. Is there any concrete example without boost?
Would it also be possible to import third-party Python libraries (e.g. installed via pip system-wide) from within Boost.Python in C++?
3

Pyrex can be cleanly used for this purpose. There's an example in the source-code release.

Comments

1

I haven't tried directly calling Python functions from C++, but here are some alternative ideas...

Generally, it's easier to call C++ code from a high-level language like Python than the other way around. If you're interested in this approach, then you could create a C++ codebase and access it from Python. You could either directly use the external API provided by python [it should be described somewhere in the Python docs] or use a tool like SWIG to automate the C++-to-Python wrapping process.

Depending on how you want to use the library, you could alternatively create Python scripts which you call from C++ with the exec* functions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.