Skip to main content

Bindings to UDPipe library

Project description

ufal.udpipe

The ufal.udpipe is a Python binding to UDPipe library <http://ufal.mff.cuni.cz/udpipe>.

The bindings is a straightforward conversion of the C++ bindings API. In Python 2, strings can be both unicode and UTF-8 encoded str, and the library always produces unicode. In Python 3, strings must be only str.

Wrapped C++ API

The C++ API being wrapped follows. For a API reference of the original C++ API, see <http://ufal.mff.cuni.cz/udpipe/api-reference>.

Helper Structures
-----------------

  typedef vector<int> Children;

  typedef vector<uint8_t> Bytes;

  typedef vector<string> Comments;

  class ProcessingError {
  public:
    bool occurred();
    string message;
  };

  class Token {
   public:
    string form;
    string misc;

    Token(const string& form = string(), const string& misc = string());

    // CoNLL-U defined SpaceAfter=No feature
    bool getSpaceAfter() const;
    void setSpaceAfter(bool space_after);

    // UDPipe-specific all-spaces-preserving SpacesBefore and SpacesAfter features
    string getSpacesBefore() const;
    void setSpacesBefore(const string& spaces_before);
    string getSpacesAfter() const;
    void setSpacesAfter(const string& spaces_after);
    string getSpacesInToken() const;
    void setSpacesInToken(const string& spaces_in_token);

    // UDPipe-specific TokenRange feature
    bool getTokenRange() const;
    size_t getTokenRangeStart() const;
    size_t getTokenRangeEnd() const;
    void setTokenRange(size_t start, size_t end);
  };

  class Word : public Token {
   public:
    // form and misc are inherited from token
    int id;         // 0 is root, >0 is sentence word, <0 is undefined
    string lemma;   // lemma
    string upostag; // universal part-of-speech tag
    string xpostag; // language-specific part-of-speech tag
    string feats;   // list of morphological features
    int head;       // head, 0 is root, <0 is undefined
    string deprel;  // dependency relation to the head
    string deps;    // secondary dependencies

    Children children;

    Word(int id = -1, const string& form = string());
  };
  typedef vector<Word> Words;

  class MultiwordToken : public Token {
   public:
    // form and misc are inherited from token
    int idFirst, idLast;

    MultiwordToken(int id_first = -1, int id_last = -1, const string& form = string(), const string& misc = string());
  };
  typedef vector<MultiwordToken> MultiwordTokens;

  class EmptyNode {
   public:
    int id;          // 0 is root, >0 is sentence word, <0 is undefined
    int index;       // index for the current id, should be numbered from 1, 0=undefined
    string form;     // form
    string lemma;    // lemma
    string upostag;  // universal part-of-speech tag
    string xpostag;  // language-specific part-of-speech tag
    string feats;    // list of morphological features
    string deps;     // secondary dependencies
    string misc;     // miscellaneous information

    EmptyNode(int id = -1, int index = 0) : id(id), index(index) {}
  };
  typedef vector<empty_node> EmptyNodes;

  class Sentence {
   public:
    Sentence();

    Words words;
    MultiwordTokens multiwordTokens;
    EmptyNodes emptyNodes;
    Comments comments;
    static const string rootForm;

    // Basic sentence modifications
    bool empty();
    void clear();
    virtual Word& addWord(const char* form);
    void setHead(int id, int head, const string& deprel);
    void unlinkAllWords();

    // CoNLL-U defined comments
    bool getNewDoc() const;
    string getNewDocId() const;
    void setNewDoc(bool new_doc, const string& id = string());
    bool getNewPar() const;
    string getNewParId() const;
    void setNewPar(bool new_par, const string& id = string());

    string getSentId() const;
    void setSentId(const string& id);
    string getText() const;
    void setText(const string& id);
  };
  typedef vector<Sentence> Sentences;


Main Classes
------------

  class InputFormat {
   public:
    virtual void resetDocument(const string& id = string());
    virtual void setText(const char* text);
    virtual bool nextSentence(Sentence& s, ProcessingError* error = nullptr);

    static InputFormat* newInputFormat(const string& name);
    static InputFormat* newConlluInputFormat(const string& id = string());
    static InputFormat* newGenericTokenizerInputFormat(const string& id = string());
    static InputFormat* newHorizontalInputFormat(const string& id = string());
    static InputFormat* newVerticalInputFormat(const string& id = string());

    static InputFormat* newPresegmentedTokenizer(InputFormat tokenizer);

    static const string CONLLU_V1;
    static const string CONLLU_V2;
    static const string GENERIC_TOKENIZER_NORMALIZED_SPACES;
    static const string GENERIC_TOKENIZER_PRESEGMENTED;
    static const string GENERIC_TOKENIZER_RANGES;
  };

  class OutputFormat {
   public:
    virtual string writeSentence(const Sentence& s);
    virtual string finishDocument();

    static OutputFormat* newOutputFormat(const string& name);
    static OutputFormat* newConlluOutputFormat(const string& options = string());
    static OutputFormat* newEpeOutputFormat(const string& options = string());
    static OutputFormat* newMatxinOutputFormat(const string& options = string());
    static OutputFormat* newHorizontalOutputFormat(const string& options = string());
    static OutputFormat* newPlaintextOutputFormat(const string& options = string());
    static OutputFormat* newVerticalOutputFormat(const string& options = string());

    static const string CONLLU_V1;
    static const string CONLLU_V2;
    static const string HORIZONTAL_PARAGRAPHS;
    static const string PLAINTEXT_NORMALIZED_SPACES;
    static const string VERTICAL_PARAGRAPHS;
  };

  class Model {
   public:
    static Model* load(const char* fname);

    virtual InputFormat* newTokenizer(const string& options) const;
    virtual bool tag(Sentence& s, const string& options, ProcessingError* error = nullptr) const;
    virtual bool parse(Sentence& s, const string& options, ProcessingError* error) const;

    static const string DEFAULT;
    static const string TOKENIZER_PRESEGMENTED;
  };

  class Pipeline {
   public:
    Pipeline(const Model* m, const string& input, const string& tagger, const string& parser, const string& output);

    void setModel(const Model* m);
    void setInput(const string& input);
    void setTagger(const string& tagger);
    void setParser(const string& parser);
    void setOutput(const string& output);

    void setImmediate(bool immediate);
    void setDocumentId(const string& document_id);

    string process(const string& data, ProcessingError* error = nullptr) const;

    static const string DEFAULT;
    static const string NONE;
  };

  class Trainer {
   public:

    static Bytes* train(const string& method, const Sentences& train, const Sentences& heldout,
                        const string& tokenizer, const string& tagger, const string& parser,
                        ProcessingError* error = nullptr);

    static const string DEFAULT;
    static const string NONE;
  };

  class Evaluator {
   public:
    Evaluator(const Model* m, const string& tokenizer, const string& tagger, const string& parser);

    void setModel(const Model* m);
    void setTokenizer(const string& tokenizer);
    void setTagger(const string& tagger);
    void setParser(const string& parser);

    string evaluate(const string& data, ProcessingError* error = nullptr) const;

    static const string DEFAULT;
    static const string NONE;
  };

  class Version {
   public:
    unsigned major;
    unsigned minor;
    unsigned patch;
    string prerelease;

    // Returns current version.
    static version current();
  };

Examples

run_udpipe

Simple pipeline loading data (tokenizing on request), tagging, parsing and writing to specified output format:

import sys

from ufal.udpipe import Model, Pipeline, ProcessingError # pylint: disable=no-name-in-module

# In Python2, wrap sys.stdin and sys.stdout to work with unicode.
if sys.version_info[0] < 3:
    import codecs
    import locale
    encoding = locale.getpreferredencoding()
    sys.stdin = codecs.getreader(encoding)(sys.stdin)
    sys.stdout = codecs.getwriter(encoding)(sys.stdout)

if len(sys.argv) < 4:
    sys.stderr.write('Usage: %s input_format(tokenize|conllu|horizontal|vertical) output_format(conllu) model_file\n' % sys.argv[0])
    sys.exit(1)

sys.stderr.write('Loading model: ')
model = Model.load(sys.argv[3])
if not model:
    sys.stderr.write("Cannot load model from file '%s'\n" % sys.argv[3])
    sys.exit(1)
sys.stderr.write('done\n')

pipeline = Pipeline(model, sys.argv[1], Pipeline.DEFAULT, Pipeline.DEFAULT, sys.argv[2])
error = ProcessingError()

# Read whole input
text = ''.join(sys.stdin.readlines())

# Process data
processed = pipeline.process(text, error)
if error.occurred():
    sys.stderr.write("An error occurred when running run_udpipe: ")
    sys.stderr.write(error.message)
    sys.stderr.write("\n")
    sys.exit(1)
sys.stdout.write(processed)

AUTHORS

Milan Straka <straka@ufal.mff.cuni.cz>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ufal.udpipe-1.3.1.1.tar.gz (350.3 kB view hashes)

Uploaded Source

Built Distributions

ufal.udpipe-1.3.1.1-cp312-cp312-win_amd64.whl (894.0 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

ufal.udpipe-1.3.1.1-cp312-cp312-win32.whl (717.8 kB view hashes)

Uploaded CPython 3.12 Windows x86

ufal.udpipe-1.3.1.1-cp312-cp312-musllinux_1_1_x86_64.whl (1.5 MB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ x86-64

ufal.udpipe-1.3.1.1-cp312-cp312-musllinux_1_1_i686.whl (1.5 MB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ i686

ufal.udpipe-1.3.1.1-cp312-cp312-musllinux_1_1_aarch64.whl (1.4 MB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ ARM64

ufal.udpipe-1.3.1.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (938.5 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

ufal.udpipe-1.3.1.1-cp312-cp312-manylinux_2_17_i686.manylinux2014_i686.whl (970.4 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ i686

ufal.udpipe-1.3.1.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (910.5 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARM64

ufal.udpipe-1.3.1.1-cp312-cp312-macosx_11_0_arm64.whl (837.0 kB view hashes)

Uploaded CPython 3.12 macOS 11.0+ ARM64

ufal.udpipe-1.3.1.1-cp312-cp312-macosx_10_9_x86_64.whl (919.1 kB view hashes)

Uploaded CPython 3.12 macOS 10.9+ x86-64

ufal.udpipe-1.3.1.1-cp311-cp311-win_amd64.whl (892.6 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

ufal.udpipe-1.3.1.1-cp311-cp311-win32.whl (716.9 kB view hashes)

Uploaded CPython 3.11 Windows x86

ufal.udpipe-1.3.1.1-cp311-cp311-musllinux_1_1_x86_64.whl (1.5 MB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

ufal.udpipe-1.3.1.1-cp311-cp311-musllinux_1_1_i686.whl (1.5 MB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ i686

ufal.udpipe-1.3.1.1-cp311-cp311-musllinux_1_1_aarch64.whl (1.4 MB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ ARM64

ufal.udpipe-1.3.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (936.8 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

ufal.udpipe-1.3.1.1-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl (967.8 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ i686

ufal.udpipe-1.3.1.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (909.5 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

ufal.udpipe-1.3.1.1-cp311-cp311-macosx_11_0_arm64.whl (835.5 kB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

ufal.udpipe-1.3.1.1-cp311-cp311-macosx_10_9_x86_64.whl (915.9 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ x86-64

ufal.udpipe-1.3.1.1-cp310-cp310-win_amd64.whl (892.7 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

ufal.udpipe-1.3.1.1-cp310-cp310-win32.whl (716.9 kB view hashes)

Uploaded CPython 3.10 Windows x86

ufal.udpipe-1.3.1.1-cp310-cp310-musllinux_1_1_x86_64.whl (1.5 MB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

ufal.udpipe-1.3.1.1-cp310-cp310-musllinux_1_1_i686.whl (1.5 MB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ i686

ufal.udpipe-1.3.1.1-cp310-cp310-musllinux_1_1_aarch64.whl (1.4 MB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ ARM64

ufal.udpipe-1.3.1.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (936.8 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

ufal.udpipe-1.3.1.1-cp310-cp310-manylinux_2_17_i686.manylinux2014_i686.whl (967.8 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ i686

ufal.udpipe-1.3.1.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (909.6 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

ufal.udpipe-1.3.1.1-cp310-cp310-macosx_11_0_arm64.whl (835.8 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

ufal.udpipe-1.3.1.1-cp310-cp310-macosx_10_9_x86_64.whl (916.2 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

ufal.udpipe-1.3.1.1-cp39-cp39-win_amd64.whl (892.6 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

ufal.udpipe-1.3.1.1-cp39-cp39-win32.whl (716.8 kB view hashes)

Uploaded CPython 3.9 Windows x86

ufal.udpipe-1.3.1.1-cp39-cp39-musllinux_1_1_x86_64.whl (1.5 MB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

ufal.udpipe-1.3.1.1-cp39-cp39-musllinux_1_1_i686.whl (1.5 MB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ i686

ufal.udpipe-1.3.1.1-cp39-cp39-musllinux_1_1_aarch64.whl (1.4 MB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ ARM64

ufal.udpipe-1.3.1.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (936.7 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

ufal.udpipe-1.3.1.1-cp39-cp39-manylinux_2_17_i686.manylinux2014_i686.whl (967.9 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ i686

ufal.udpipe-1.3.1.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (909.6 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

ufal.udpipe-1.3.1.1-cp39-cp39-macosx_11_0_arm64.whl (835.6 kB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

ufal.udpipe-1.3.1.1-cp39-cp39-macosx_10_9_x86_64.whl (915.9 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

ufal.udpipe-1.3.1.1-cp38-cp38-win_amd64.whl (892.6 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

ufal.udpipe-1.3.1.1-cp38-cp38-win32.whl (716.6 kB view hashes)

Uploaded CPython 3.8 Windows x86

ufal.udpipe-1.3.1.1-cp38-cp38-musllinux_1_1_x86_64.whl (1.5 MB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

ufal.udpipe-1.3.1.1-cp38-cp38-musllinux_1_1_i686.whl (1.5 MB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ i686

ufal.udpipe-1.3.1.1-cp38-cp38-musllinux_1_1_aarch64.whl (1.4 MB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ ARM64

ufal.udpipe-1.3.1.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (937.2 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

ufal.udpipe-1.3.1.1-cp38-cp38-manylinux_2_17_i686.manylinux2014_i686.whl (968.1 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ i686

ufal.udpipe-1.3.1.1-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (909.3 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARM64

ufal.udpipe-1.3.1.1-cp38-cp38-macosx_11_0_arm64.whl (837.1 kB view hashes)

Uploaded CPython 3.8 macOS 11.0+ ARM64

ufal.udpipe-1.3.1.1-cp38-cp38-macosx_10_9_x86_64.whl (918.1 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

ufal.udpipe-1.3.1.1-cp37-cp37m-win_amd64.whl (892.5 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

ufal.udpipe-1.3.1.1-cp37-cp37m-win32.whl (716.6 kB view hashes)

Uploaded CPython 3.7m Windows x86

ufal.udpipe-1.3.1.1-cp37-cp37m-musllinux_1_1_x86_64.whl (1.5 MB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ x86-64

ufal.udpipe-1.3.1.1-cp37-cp37m-musllinux_1_1_i686.whl (1.5 MB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ i686

ufal.udpipe-1.3.1.1-cp37-cp37m-musllinux_1_1_aarch64.whl (1.4 MB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ ARM64

ufal.udpipe-1.3.1.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (936.6 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

ufal.udpipe-1.3.1.1-cp37-cp37m-manylinux_2_17_i686.manylinux2014_i686.whl (968.2 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ i686

ufal.udpipe-1.3.1.1-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (907.5 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ ARM64

ufal.udpipe-1.3.1.1-cp37-cp37m-macosx_10_9_x86_64.whl (917.4 kB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

ufal.udpipe-1.3.1.1-cp36-cp36m-win_amd64.whl (940.4 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

ufal.udpipe-1.3.1.1-cp36-cp36m-win32.whl (751.7 kB view hashes)

Uploaded CPython 3.6m Windows x86

ufal.udpipe-1.3.1.1-cp36-cp36m-musllinux_1_1_x86_64.whl (1.5 MB view hashes)

Uploaded CPython 3.6m musllinux: musl 1.1+ x86-64

ufal.udpipe-1.3.1.1-cp36-cp36m-musllinux_1_1_i686.whl (1.5 MB view hashes)

Uploaded CPython 3.6m musllinux: musl 1.1+ i686

ufal.udpipe-1.3.1.1-cp36-cp36m-musllinux_1_1_aarch64.whl (1.4 MB view hashes)

Uploaded CPython 3.6m musllinux: musl 1.1+ ARM64

ufal.udpipe-1.3.1.1-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (936.6 kB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ x86-64

ufal.udpipe-1.3.1.1-cp36-cp36m-manylinux_2_17_i686.manylinux2014_i686.whl (968.2 kB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ i686

ufal.udpipe-1.3.1.1-cp36-cp36m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (907.5 kB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ ARM64

ufal.udpipe-1.3.1.1-cp36-cp36m-macosx_10_9_x86_64.whl (917.4 kB view hashes)

Uploaded CPython 3.6m macOS 10.9+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page