Skip to content

Commit

Permalink
follow-up to 44b74b0 - add test, reformat code, update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
d99kris committed Jan 7, 2023
1 parent 528ea88 commit 204a9db
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ Supported Methods / Attributes
- dep_()
- ent_iob_()
- has_vector()
- head()
- i()
- idx()
- is_alpha()
Expand Down
2 changes: 1 addition & 1 deletion src/spacy/python.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ namespace Spacy
PyObjectPtr attr(PyObject_GetAttrString(p_obj.get(), p_attr.c_str()));
return get_vector<T>(attr);
}

template <typename T, typename U>
static std::vector<T> get_attr_vector_ctor_arg(PyObjectPtr p_obj, const std::string& p_attr, U p_arg)
{
Expand Down
12 changes: 5 additions & 7 deletions src/spacy/token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ namespace Spacy
return Python::get_attr_value<bool>(m_token, "has_vector");
}

Token Token::head() const
{
return Python::get_attr_obj<Token>(m_token, "head");
}

long Token::i() const
{
return Python::get_attr_value<long>(m_token, "i");
Expand Down Expand Up @@ -268,13 +273,6 @@ namespace Spacy
return Python::get_attr_value<std::string>(m_token, "whitespace_");
}

Token Token::head() const
{
return Python::get_attr_obj<Token>(m_token, "head");
}



Token::Token(PyObjectPtr p_token)
: m_token(p_token)
{
Expand Down
2 changes: 1 addition & 1 deletion src/spacy/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ namespace Spacy
std::string dep_() const;
std::string ent_iob_() const;
bool has_vector() const;
Token head() const;
long i() const;
long idx() const;
bool is_alpha() const;
Expand Down Expand Up @@ -78,7 +79,6 @@ namespace Spacy
std::string text() const;
std::string text_with_ws() const;
std::string whitespace_() const;
Token head() const;

private:
explicit Token(PyObjectPtr p_token);
Expand Down
8 changes: 8 additions & 0 deletions tests/test_token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ int main()
unittest::ExpectTrue(apples.has_vector());
}

// Token::head
{
Spacy::Doc doc = nlp.parse("Give it back! He pleaded.");
std::vector<Spacy::Token> give_children = doc.tokens().at(0).children();
unittest::ExpectEqual(std::string, doc.tokens().at(0).text(),
give_children.at(0).head().text());
}

// Token::i
{
Spacy::Doc doc = nlp.parse("I like apples");
Expand Down

0 comments on commit 204a9db

Please sign in to comment.