Linux | Mac |
---|---|
Spacy-cpp is a C++ wrapper library for the NLP library spaCy. This project is not affiliated with spaCy, it is however distributed under the same type of license (MIT).
The goal of spacy-cpp is to expose the functionality of spaCy to C++ applications, and to provide an API that is similar to that of spaCy, enabling rapid development in Python and simple porting to C++.
Spacy-cpp is under development and does not yet support all API's of spaCy, refer to the API Documentation section below.
Simple POS tagging example using spacy-cpp:
Spacy::Spacy spacy;
auto nlp = spacy.load("en_core_web_sm");
auto doc = nlp.parse("This is a sentence.");
for (auto& token : doc.tokens())
std::cout << token.text() << " [" << token.pos_() << "]\n";
For reference - doing the same using the spaCy API in Python:
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp(u"This is a sentence.")
for token in doc:
print(token.text + " [" + token.pos_ + "]")
Spacy-cpp is implemented using C++11 with the intention of being portable. Current version has been tested on:
Spacy-cpp requires python development library, pip, spaCy and typically a spaCy model.
Install build dependencies:
brew install cmake python
Install spaCy and an English model:
pip3 install -U spacy
python3 -m spacy download en_core_web_sm
Install build dependencies:
sudo apt install cmake python3-pip libpython3-dev
Install spaCy and an English model:
pip3 install -U spacy
python3 -m spacy download en_core_web_sm
Spacy-cpp can be used either as a shared library or as a header-only library.
Build and install spacy-cpp:
mkdir -p build && cd build && cmake .. && make && sudo make install
Link library:
-lspacy
Include header (convenience header including all modules):
#include <spacy/spacy>
Copy the src/spacy directory to the source directory of your project. Then define SPACY_HEADER_ONLY and include headers needed (spacy/spacy includes all headers):
#define SPACY_HEADER_ONLY
#include <spacy/spacy>
The source tree includes two CMake project examples:
If a system has more than one Python installation, each of the installations
will have its own set of pip-installed Python packages. One must ensure that
spacy is installed for the Python version used by spacy-cpp (alternatively
point spacy-cpp to the desired Python installation). When building spacy-cpp
using CMake (example: ./make.sh tests
) the Python version used will be
output, for example PYTHON_EXECUTABLE="/usr/local/bin/python3.11"
. Use this
interpreter to ensure spacy works correctly in Python, example:
/usr/local/bin/python3.11 ./examples/python-spacy-usage.py
. If not working,
use this Python version to install spacy and a language model:
/usr/local/bin/python3.11 -m pip install -U spacy
/usr/local/bin/python3.11 -m spacy download en_core_web_sm
Spacy-cpp is under development and does not support the complete spaCy API yet.
Attrs supports all attribute constants.
Doc supports the following methods / attributes:
MorphAnalysis supports the following methods / attributes:
Nlp supports the following methods / attributes:
Spacy supports the following methods / attributes:
Span supports the following methods / attributes:
StringStore supports the following methods / attributes:
Token supports the following methods / attributes:
Vocab supports the following methods / attributes:
Spacy-cpp uses cmake for its tests. Commands to build and execute the test suite:
mkdir -p build && cd build && cmake -DSPACYCPP_BUILD_TESTS=ON .. && make && ctest --output-on-failure ; cd -
Spacy-cpp is distributed under the MIT license. See LICENSE file.
Bugs, PRs, etc are welcome on the GitHub project page https://github.com/d99kris/spacy-cpp
c++, c++11, natural language processing, nlp, spacy.