Skip to main content

ufal.parsito

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

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/parsito/api-reference>.

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

  typedef vector<int> Children;

  class Node {
   public:
    int id;          // 0 is root, >0 is sentence node, <0 is 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
    int head;       // head, 0 is root, <0 is without parent
    string deprel;  // dependency relation to the head
    string deps;    // secondary dependencies
    string misc;    // miscellaneous information

    Children children;

    node(int id = -1, string form = string());
  };
  typedef std::vector<node> Nodes;


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

  class Tree {
   public:
    Tree();

    Nodes nodes;

    bool empty();
    void clear();
    node& addNode(string form);
    void setHead(int id, int head, string deprel);
    void unlinkAllNodes();

    static const std::string root_form;
  }

  class TreeInputFormat {
   public:
    virtual void setText(string text);
    virtual bool nextTree(tree& t) = 0;
    string lastError() const;

    // Static factory methods
    static TreeInputFormat* newInputFormat(string name);
    static TreeInputFormat* newConlluInputFormat();
  };

  class TreeOutputFormat {
   public:

    virtual string writeTree(const tree& t, const tree_input_format* additional_info = nullptr);

    // Static factory methods
    static TreeOutputFormat* newOutputFormat(string name);
    static TreeOutputFormat* newConlluOutputFormat();
  };

  class Parser {
   public:
    virtual void parse(tree& t, unsigned beam_size = 0) const;

    enum { NO_CACHE = 0, FULL_CACHE = 2147483647};
    static Parser* load(string file, unsigned cache = 1000);
  };

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

    static Version current();
  };

Examples

run_parsito

Simple parsing example:

from ufal.parsito import *

# 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) == 1:
  sys.stderr.write('Usage: %s parser_file\n' % sys.argv[0])
  sys.exit(1)

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

conlluInput = TreeInputFormat.newInputFormat("conllu");
conlluOutput = TreeOutputFormat.newOutputFormat("conllu");
tree = Tree()

not_eof = True
while not_eof:
  text = ''

  # Read block
  while True:
    line = sys.stdin.readline()
    not_eof = bool(line)
    if not not_eof: break
    line = line.rstrip('\r\n')
    text += line
    text += '\n';
    if not line: break


  # Parse
  conlluInput.setText(text)
  while conlluInput.nextTree(tree):
    parser.parse(tree)

    output = conlluOutput.writeTree(tree, conlluInput)
    sys.stdout.write(output)
  if conlluInput.lastError():
    sys.stderr.write("Cannot read input CoNLL-U: ")
    sys.stderr.write(conlluInput.lastError())
    sys.stderr.write("\n")
    sys.exit(1)

AUTHORS

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

Metadata

Release files for ufal.parsito 1.1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ufal.parsito 1.1.0.1
File Size Uploaded
ufal.parsito-1.1.0.1.tar.gz 114.7 kB Details

Release files / ufal.parsito-1.1.0.1.tar.gz

Download URL ufal.parsito-1.1.0.1.tar.gz
Size 114.7 kB
Tags Source
SHA-256 checksum
How to use checksums
1056672f019de326978fe144aaccf181e950fb70ae4cc1d12f221c5358e308a1
BLAKE2b-256 checksum
How to use checksums
d6129c081af96169578accec4527b1632783d2b16c15abba01265f0b178b0919
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

1.1.0.1 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page