Skip to content

semiring.py

from pymwp.semiring import KEYS, ZERO_MWP, UNIT_MWP, prod_mwp, sum_mwp

KEYS: List[str] = ['o', 'm', 'w', 'p', 'i'] module-attribute

Different scalar values: "o", "m", "w", "p", "i" where o = 0 and i = \(\infty\).

UNIT_MWP: str = 'm' module-attribute

Scalar that represents m in the analysis ('m').

ZERO_MWP: str = 'o' module-attribute

Scalar that represents 0 in the analysis ('o').

prod_mwp(scalar1, scalar2)

Compute product of two scalars.

\(\times\) \(0\) \(m\) \(w\) \(p\) \(\infty\)
\(0\) \(0\) \(0\) \(0\) \(0\) \(\infty\)
\(m\) \(0\) \(m\) \(w\) \(p\) \(\infty\)
\(w\) \(0\) \(w\) \(w\) \(p\) \(\infty\)
\(p\) \(0\) \(p\) \(p\) \(p\) \(\infty\)
\(\infty\) \(\infty\) \(\infty\) \(\infty\) \(\infty\) \(\infty\)

Parameters:

Name Type Description Default
scalar1 str

scalar value

required
scalar2 str

scalar value

required

Raises:

Type Description
Exception

if scalar1 or scalar2 is not in KEYS

Returns:

Type Description
str

product of scalar1 * scalar2

Source code in pymwp/semiring.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def prod_mwp(scalar1: str, scalar2: str) -> str:
    """Compute product of two scalars.

    | $\\times$  | $0$ | $m$       | $w$        | $p$       | $\\infty$ |
    | ---        | --- | ---       | ---        | ---       | --- |
    | $0$        | $0$ | $0$       | $0$        | $0$       | $\\infty$ |
    | $m$        | $0$ | $m$       | $w$        | $p$       | $\\infty$ |
    | $w$        | $0$ | $w$       | $w$        | $p$       | $\\infty$ |
    | $p$        | $0$ | $p$       | $p$        | $p$       | $\\infty$ |
    | $\\infty$  | $\\infty$ | $\\infty$ | $\\infty$  | $\\infty$ | $\\infty$ |

    Arguments:
        scalar1: scalar value
        scalar2: scalar value

    Raises:
          Exception: if `scalar1` or `scalar2` is not in KEYS

    Returns:
        product of scalar1 * scalar2
    """
    if scalar1 in KEYS and scalar2 in KEYS:
        return __DICT_PROD[scalar1][scalar2]
    else:
        raise Exception(
            f"trying to use {scalar1} and {scalar2} as keys for prod…")

sum_mwp(scalar1, scalar2)

Compute sum of two scalars.

\(+\) \(0\) \(m\) \(w\) \(p\) \(\infty\)
\(0\) \(0\) \(m\) \(w\) \(p\) \(\infty\)
\(m\) \(m\) \(m\) \(w\) \(p\) \(\infty\)
\(w\) \(w\) \(w\) \(w\) \(p\) \(\infty\)
\(p\) \(p\) \(p\) \(p\) \(p\) \(\infty\)
\(\infty\) \(\infty\) \(\infty\) \(\infty\) \(\infty\) \(\infty\)

Parameters:

Name Type Description Default
scalar1 str

scalar value

required
scalar2 str

scalar value

required

Raises:

Type Description
Exception

if scalar1 or scalar2 is not in KEYS

Returns:

Type Description
str

sum of scalar1 + scalar2

Source code in pymwp/semiring.py
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
def sum_mwp(scalar1: str, scalar2: str) -> str:
    """Compute sum of two scalars.

    | $+$        | $0$       | $m$       | $w$        | $p$       | $\\infty$ |
    | ---        | ---       | ---       | ---        | ---       | --- |
    | $0$        | $0$       | $m$       | $w$        | $p$       | $\\infty$ |
    | $m$        | $m$       | $m$       | $w$        | $p$       | $\\infty$ |
    | $w$        | $w$       | $w$       | $w$        | $p$       | $\\infty$ |
    | $p$        | $p$       | $p$       | $p$        | $p$       | $\\infty$ |
    | $\\infty$  | $\\infty$ | $\\infty$ | $\\infty$  | $\\infty$ | $\\infty$ |

    Arguments:
        scalar1: scalar value
        scalar2: scalar value

    Raises:
          Exception: if `scalar1` or `scalar2` is not in KEYS

    Returns:
        sum of scalar1 + scalar2
    """
    if scalar1 in KEYS and scalar2 in KEYS:
        return __DICT_SUM[scalar1][scalar2]
    else:
        raise Exception(
            f"trying to use {scalar1} and {scalar2} as keys for sum…")