Skip to content

Commit e3d64e2

Browse files
committed
Updates the doc Quickstart
1 parent 03db974 commit e3d64e2

File tree

3 files changed

+96
-61
lines changed

3 files changed

+96
-61
lines changed

docs/source/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
# The theme to use for HTML and HTML Help pages. See the documentation for
4747
# a list of builtin themes.
4848
#
49-
html_theme = 'classic'
49+
html_theme = 'sphinx_rtd_theme'
5050

5151
# Add any paths that contain custom static files (such as style sheets) here,
5252
# relative to this directory. They are copied after the builtin static files,

docs/source/index.rst

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
.. pyldl documentation master file, created by
2-
sphinx-quickstart on Mon Nov 15 19:11:20 2021.
3-
You can adapt this file completely to your liking, but it should at least
4-
contain the root `toctree` directive.
1+
.. discriminative_lexicon_model documentation master file
52
6-
.. automodule:: pyldl
3+
.. automodule:: discriminative_lexicon_model
74

85
.. toctree::
96
:maxdepth: 3

docs/source/quickstart.rst

Lines changed: 93 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -17,97 +17,135 @@ Installation
1717
Quick overview of the theory "Discriminative Lexicon Model (DLM)"
1818
=================================================================
1919

20-
In DLM, language processing is modelled as linear mappings between word-forms and word-meanings. Word-forms and word-meanings can be defined in any way, as long as each word form/meaning is expressed in the form of a vector (i.e., an array of numbers). Word-forms are stacked up to be a matrix called the *C* matrix. Word-meanings are stacked up to be another matrix called the *S* matrix. The comprehension process can be modelled as receiving word-forms (i.e., C) and predicting word-meanings (i.e., S). Such a matrix that approximates S as closely as possible based on C can be estimated either analytically or computationally (see [1]_ for more detail), and it is called the *F* matrix. With C and F, the approximation (prediction) of S can be derived, and it is called the :math:`\hat{S}` matrix. Similarly, the production process can be modelled as receiving word-meanings (i.e., S) and predicting word-forms (i.e., C). Such a matrix that approximates C based on S is called the *G* matrix. With S and G, the model's predictions about word-forms are obtained as yet another matrix. The matrix is called the :math:`\hat{C}` matrix. It is shown below how to set up and estimate these matrices.
20+
Short summary
21+
-------------
22+
DLM is a single model of language processing (comprehension and production both) consisting of 4 + 2 components (i.e., matrices). They are :math:`\mathbf{C}` (word-forms), :math:`\mathbf{S}` (word-meanings), :math:`\mathbf{F}` (form-meaning associations), :math:`\mathbf{G}` (meaning-form associations), :math:`\mathbf{\hat{C}}` (predicted word-forms), and :math:`\mathbf{\hat{S}}` (predicted word-meanings).
23+
24+
A little bit more detail
25+
------------------------
26+
DLM is a language processing model based on learning. DLM usually consists of four components (matrices): :math:`\mathbf{C}` (word-forms), :math:`\mathbf{S}` (word-meanings), :math:`\mathbf{F}` (form-meaning associations), and :math:`\mathbf{G}` (meaning-form associations). DLM models the comprehension as mapping from forms to meanings, namely DLM estimates :math:`\mathbf{F}` so that the product of :math:`\mathbf{C}` and :math:`\mathbf{F}`, namely :math:`\mathbf{CF}` (i.e., mapping of forms onto meanings), becomes as close as possible to :math:`\mathbf{S}`. :math:`\mathbf{CF}` is also called :math:`\mathbf{\hat{S}}`. :math:`\mathbf{\hat{S}}` is the model's predictions about word meanings, while :math:`\mathbf{S}` is the gold-standard "correct" meanings of these words. Similarly, DLM models the speech production as mapping from meanings to forms. DLM estimates :math:`\mathbf{G}` so that :math:`\mathbf{SG}` (which is also called :math:`\mathbf{\hat{C}}`) becomes as close as possible to :math:`\mathbf{C}` (i.e., the gold-standard correct form matrix). DLM is conceptually a single model containing these six components (i.e., :math:`\mathbf{C}`, :math:`\mathbf{S}`, :math:`\mathbf{F}`, :math:`\mathbf{G}`, :math:`\mathbf{\hat{C}}`, and :math:`\mathbf{\hat{S}}`). To reflect this conceptualization, *discriminative_lexicon_model* provides a class having these matrices as its attributes. The class is ``discriminative_lexicon_model.ldl.LDL``.
27+
28+
29+
30+
31+
Create a model object
32+
=====================
33+
34+
``discriminative_lexicon_model.ldl.LDL`` creates a model of DLM.
35+
36+
.. code-block:: python
37+
38+
>>> import discriminative_lexicon_model as dlm
39+
>>> mdl = dlm.ldl.LDL()
40+
>>> print(type(mdl))
41+
<class 'discriminative_lexicon_model.ldl.LDL'>
42+
>>> mdl.__dict__
43+
{}
44+
45+
With no argument, ``discriminative_lexicon_model.ldl.LDL`` creates an empty model (of DLM), which is to be populated later with some class methods (see below).
46+
2147

2248

2349
Set up the basis matrices C and S
2450
=================================
2551

52+
In order to estimate association matrices and create predictions based on them, :math:`\mathbf{C}` and :math:`\mathbf{S}` must be set up first.
53+
54+
55+
2656
C-matrix
2757
--------
2858

29-
The C matrix is a collection of form-vectors of words. You can create a C-matrix from a list of words by using discriminative_lexicon_model.mapping.gen_cmat.
59+
:math:`\mathbf{C}` is a collection of form-vectors of words. :math:`\mathbf{C}` can be created from a list of words by ``discriminative_lexicon_model.ldl.LDL.gen_cmat``.
60+
3061

3162
.. code-block:: python
3263
33-
>>> import discriminative_lexicon_model as dlm
34-
>>> words = ['walk','walked','walks']
35-
>>> cmat = dlm.mapping.gen_cmat(words)
36-
>>> cmat
37-
<xarray.DataArray (word: 3, cues: 9)>
38-
array([[1, 1, 1, 1, 0, 0, 0, 0, 0],
39-
[1, 1, 1, 0, 1, 1, 1, 0, 0],
40-
[1, 1, 1, 0, 0, 0, 0, 1, 1]])
41-
Coordinates:
42-
* word (word) <U6 'walk' 'walked' 'walks'
43-
* cues (cues) <U3 '#wa' 'wal' 'alk' 'lk#' 'lke' 'ked' 'ed#' 'lks' 'ks#'
64+
>>> mdl.gen_cmat(['walk','walked','walks'])
65+
>>> print(mdl.cmat)
66+
<xarray.DataArray (word: 3, cues: 9)>
67+
array([[1, 1, 1, 1, 0, 0, 0, 0, 0],
68+
[1, 1, 1, 0, 1, 1, 1, 0, 0],
69+
[1, 1, 1, 0, 0, 0, 0, 1, 1]])
70+
Coordinates:
71+
* word (word) <U6 'walk' 'walked' 'walks'
72+
* cues (cues) <U3 '#wa' 'wal' 'alk' 'lk#' 'lke' 'ked' 'ed#' 'lks' 'ks#'
73+
74+
4475
4576
4677
S-matrix
4778
--------
4879

49-
The S matrix is a collection of semantic vectors of words. For one method, an S-matrix can be set up by defining semantic dimensions by hand. This can be achieved by discriminative_lexicon_model.mapping.gen_smat_from_df.
80+
:math:`\mathbf{S}` is a collection of semantic vectors of words. :math:`\mathbf{S}` can be set up by means of ``discriminative_lexicon_model.ldl.LDL.gen_smat``. For its argument, semantic vectors need to be set up with ``pandas.core.frame.DataFrame`` with words as its indices and semantic dimensions as its columns. Semantic dimensions can be defined either by hand or by an embeddings algorithm such as word2vec and fastText. Regardless of the method of constructing semantics, ``discriminative_lexicon_model.ldl.LDL.gen_smat`` sets up :math:`\mathbf{S}`, as long as the dataframe given to its (first) argument follows the right format (i.e., rows = words, columns = semantic dimensions). In the example below, semantic dimensions are set up by hand.
5081

5182

5283
.. code-block:: python
5384
54-
>>> import pandas as pd
55-
>>> smat = pd.DataFrame({'WALK':[1,1,1], 'Present':[1,0,1], 'Past':[0,1,0], 'ThirdPerson':[0,0,1]}, index=['walk','walked','walks'])
56-
>>> smat = dlm.mapping.gen_smat_from_df(smat)
57-
<xarray.DataArray (word: 3, semantics: 4)>
58-
array([[1, 1, 0, 0],
59-
[1, 0, 1, 0],
60-
[1, 1, 0, 1]])
61-
Coordinates:
62-
* word (word) <U6 'walk' 'walked' 'walks'
63-
* semantics (semantics) object 'WALK' 'Present' 'Past' 'ThirdPerson'
85+
>>> import pandas as pd
86+
>>> semdf = pd.DataFrame({'WALK':[1,1,1], 'Present':[1,0,1], 'Past':[0,1,0], 'ThirdPerson':[0,0,1]}, index=['walk','walked','walks'])
87+
>>> print(semdf)
88+
WALK Present Past ThirdPerson
89+
walk 1 1 0 0
90+
walked 1 0 1 0
91+
walks 1 1 0 1
92+
>>> mdl.gen_smat(semdf)
93+
>>> print(mdl.smat)
94+
<xarray.DataArray (word: 3, semantics: 4)>
95+
array([[1, 1, 0, 0],
96+
[1, 0, 1, 0],
97+
[1, 1, 0, 1]])
98+
Coordinates:
99+
* word (word) <U6 'walk' 'walked' 'walks'
100+
* semantics (semantics) object 'WALK' 'Present' 'Past' 'ThirdPerson'
64101
65102
66103
67-
Estimation of the association matrices
68-
======================================
104+
105+
Estimation of the association matrices F and G
106+
==============================================
69107

70108
F-matrix
71109
--------
72110

73-
With C and S established, the comprehension association matrix F can be estimated by discriminative_lexicon_model.mapping.gen_fmat.
111+
With :math:`\mathbf{C}` and :math:`\mathbf{S}` established, the comprehension association matrix :math:`\mathbf{F}` can be estimated by ``discriminative_lexicon_model.ldl.LDL.gen_fmat``. It does not require any argument, because :math:`\mathbf{C}` and :math:`\mathbf{S}` are stored already as attributes of the class and therefore accessible by the model.
74112

75113
.. code-block:: python
76114
77-
>>> fmat = dlm.mapping.gen_fmat(cmat, smat)
78-
>>> fmat.round(2)
79-
<xarray.DataArray (cues: 9, semantics: 4)>
80-
array([[ 0.28, 0.23, 0.05, 0.08],
81-
[ 0.28, 0.23, 0.05, 0.08],
82-
[ 0.28, 0.23, 0.05, 0.08],
83-
[ 0.15, 0.31, -0.15, -0.23],
84-
[ 0.05, -0.23, 0.28, -0.08],
85-
[ 0.05, -0.23, 0.28, -0.08],
86-
[ 0.05, -0.23, 0.28, -0.08],
87-
[ 0.08, 0.15, -0.08, 0.38],
88-
[ 0.08, 0.15, -0.08, 0.38]])
89-
Coordinates:
90-
* cues (cues) <U3 '#wa' 'wal' 'alk' 'lk#' 'lke' 'ked' 'ed#' 'lks' 'ks#'
91-
* semantics (semantics) object 'WALK' 'Present' 'Past' 'ThirdPerson'
115+
>>> mdl.gen_fmat()
116+
>>> print(mdl.fmat.round(2))
117+
<xarray.DataArray (cues: 9, semantics: 4)>
118+
array([[ 0.28, 0.23, 0.05, 0.08],
119+
[ 0.28, 0.23, 0.05, 0.08],
120+
[ 0.28, 0.23, 0.05, 0.08],
121+
[ 0.15, 0.31, -0.15, -0.23],
122+
[ 0.05, -0.23, 0.28, -0.08],
123+
[ 0.05, -0.23, 0.28, -0.08],
124+
[ 0.05, -0.23, 0.28, -0.08],
125+
[ 0.08, 0.15, -0.08, 0.38],
126+
[ 0.08, 0.15, -0.08, 0.38]])
127+
Coordinates:
128+
* cues (cues) <U3 '#wa' 'wal' 'alk' 'lk#' 'lke' 'ked' 'ed#' 'lks' 'ks#'
129+
* semantics (semantics) object 'WALK' 'Present' 'Past' 'ThirdPerson'
92130
93131
94132
G-matrix
95133
--------
96134

97-
The production association matrix G can be obtained by discriminative_lexicon_model.mapping.gen_gmat.
135+
Similarly, with :math:`\mathbf{C}` and :math:`\mathbf{S}` established, the production association matrix :math:`\mathbf{G}` can also be estimated by ``discriminative_lexicon_model.ldl.LDL.gen_gmat``. It does not require any argument, either, because :math:`\mathbf{C}` and :math:`\mathbf{S}` are stored already as attributes of the class and therefore accessible by the model.
98136

99137
.. code-block:: python
100138
101-
>>> gmat = dlm.mapping.gen_gmat(cmat, smat)
102-
>>> gmat.round(2)
103-
<xarray.DataArray (semantics: 4, cues: 9)>
104-
array([[ 0.67, 0.67, 0.67, 0.33, 0.33, 0.33, 0.33, -0. , -0. ],
105-
[ 0.33, 0.33, 0.33, 0.67, -0.33, -0.33, -0.33, -0. , -0. ],
106-
[ 0.33, 0.33, 0.33, -0.33, 0.67, 0.67, 0.67, -0. , -0. ],
107-
[ 0. , 0. , 0. , -1. , 0. , 0. , 0. , 1. , 1. ]])
108-
Coordinates:
109-
* semantics (semantics) object 'WALK' 'Present' 'Past' 'ThirdPerson'
110-
* cues (cues) <U3 '#wa' 'wal' 'alk' 'lk#' 'lke' 'ked' 'ed#' 'lks' 'ks#'
139+
>>> mdl.gen_gmat()
140+
>>> print(mdl.gmat.round(2))
141+
<xarray.DataArray (semantics: 4, cues: 9)>
142+
array([[ 0.67, 0.67, 0.67, 0.33, 0.33, 0.33, 0.33, -0. , -0. ],
143+
[ 0.33, 0.33, 0.33, 0.67, -0.33, -0.33, -0.33, -0. , -0. ],
144+
[ 0.33, 0.33, 0.33, -0.33, 0.67, 0.67, 0.67, -0. , -0. ],
145+
[ 0. , 0. , 0. , -1. , 0. , 0. , 0. , 1. , 1. ]])
146+
Coordinates:
147+
* semantics (semantics) object 'WALK' 'Present' 'Past' 'ThirdPerson'
148+
* cues (cues) <U3 '#wa' 'wal' 'alk' 'lk#' 'lke' 'ked' 'ed#' 'lks' 'ks#'
111149
112150
113151
@@ -117,7 +155,7 @@ Prediction of the form and semantic matrices
117155
S-hat matrix
118156
------------
119157

120-
The S-hat matrix (:math:`\mathbf{\hat{S}}`) can be obtained by discriminative_lexicon_model.mapping.gen_shat.
158+
The model's predictions about word-meanings based on word-forms (i.e., :math:`\mathbf{\hat{S}}`) can be obtained by discriminative_lexicon_model.ldl.LDL.gen_shat, given that :math:`\mathbf{C}` and :math:`\mathbf{F}` are already set up and stored as attributes of the class instance.
121159

122160
.. code-block:: python
123161
@@ -135,7 +173,7 @@ The S-hat matrix (:math:`\mathbf{\hat{S}}`) can be obtained by discriminative_le
135173
C-hat matrix
136174
------------
137175

138-
The C-hat matrix (:math:`\mathbf{\hat{C}}`) can be obtained with discriminative_lexicon_model.mapping.gen_chat.
176+
Similarly, the model's predictions about word-forms based on word-meanings (i.e., :math:`\mathbf{\hat{C}}`) can be obtained with discriminative_lexicon_model.ldl.LDL.gen_chat, given that :math:`\mathbf{S}` and :math:`\mathbf{G}` are already set up and stored as attributes of the class instance.
139177

140178
.. code-block:: python
141179

0 commit comments

Comments
 (0)