Skip to main content

Distributional Compositional Python

Project description

Distributional Compositional Python

discopy computes natural language meaning in pictures.

"Alice loves Bob" in picture

Recipe

  1. Draw your picture.
from discopy import Ty, Word, Cup, Wire

s, n = Ty('s'), Ty('n')
Alice, Bob = Word('Alice', n), Word('Bob', n)
loves = Word('loves', n.r @ s @ n.l)

sentence = Alice @ loves @ Bob >> Cup(n, n.r) @ Wire(s) @ Cup(n.l, n)
  1. Define a model.
from discopy import Model

ob = {s: 1, n: 2}
ar = {Alice: [1, 0], loves: [0, 1, 1, 0], Bob: [0, 1]}
F = Model(ob, ar)
  1. Compute the meaning!
assert F(sentence)

Requirements

  • If you just want to play with free categories, there are no requirements.
  • If you want to compute matrix-valued functors, you will need numpy.
  • If you want to evaluate quantum circuits for real, you will need pytket.
  • If you want to differentiate your matrix-valued functors, you will need jax.

Getting Started

Either a) install from pip:

pip install discopy

or b) install from sources:

git clone https://github.com/oxford-quantum-group/discopy.git
cd discopy
python setup.py install

Documentation

For now all of it is in the code. You can use help if needed:

>>> help(discopy.Ty)

Help on class Ty in module discopy.pregroup:

class Ty(discopy.moncat.Ty)
 |  Ty(*t)
 |
 |  Implements pregroup types as lists of simple types.
 |
 |  >>> s, n = Ty('s'), Ty('n')
 |  >>> assert n.l.r == n == n.r.l
 |  >>> assert (s @ n).l == n.l @ s.l and (s @ n).r == n.r @ s.r

You can also checkout the notebooks for a demo!

References

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

discopy-0.1.3.tar.gz (21.7 kB view hashes)

Uploaded Source

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