Distributional Compositional Python
Project description
Distributional Compositional Python
discopy
computes natural language meaning in pictures.
Recipe
- 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)
- 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)
- 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
- Lambek (2008) From Word to Sentence: A Computational Algebraic Approach to Grammar
- Clark, Coecke, Sadrzadeh (2008) A Compositional Distributional Model of Meaning
- Grefenstette and Sadrzadeh (2010) Experimental Support for a Categorical Compositional Distributional Model of Meaning
- Coecke (2019) The Mathematics of Text Structure
- De Felice, Meichanetzidis, Toumi (2019) Functorial Question Answering
- Pregroup grammars and categorical compositional distributional semantics and on the nLab
Project details
Release history Release notifications | RSS feed
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)