1- from typing import Any
2-
3- import numpy as np
4- from scipy .sparse import csr_matrix , dia_matrix , diags
5-
6-
7- def _as_diag (px : np .ndarray , alpha : float ) -> dia_matrix :
8- px_diag : dia_matrix = diags (px .tolist ()[0 ])
9- px_diag .data [0 ] = np .asarray ([0 if v == 0 else 1 / (v + alpha ) for v in px_diag .data [0 ]])
10- return px_diag
1+ """Deprecated: soynlp.word.pmi → soynlp.utils.pmi로 이동되었습니다."""
112
3+ import warnings
124
13- def _logarithm_and_ppmi (exp_pmi : Any , min_exp_pmi : float ) -> csr_matrix :
14- n , m = exp_pmi .shape
15-
16- rows , cols = exp_pmi .nonzero ()
17- data = exp_pmi .data
5+ import numpy as np
6+ from scipy .sparse import csr_matrix
187
19- indices = np .where (data >= min_exp_pmi )[0 ]
20- rows = rows [indices ]
21- cols = cols [indices ]
22- data = data [indices ]
8+ from soynlp .utils .pmi import _as_diag , _logarithm_and_ppmi
9+ from soynlp .utils .pmi import pmi as _pmi_impl
2310
24- data = np .log (data )
25- exp_pmi_ = csr_matrix ((data , (rows , cols )), shape = (n , m ))
26- return exp_pmi_
11+ __all__ = ["pmi" , "_as_diag" , "_logarithm_and_ppmi" ]
2712
2813
2914def pmi (
@@ -33,44 +18,10 @@ def pmi(
3318 alpha : float = 0.0 ,
3419 beta : float = 1 ,
3520) -> tuple [csr_matrix , np .ndarray , np .ndarray ]:
36- """Transform `X` to Positive-PMI matrix (CSR sparse matrix)
37-
38- Args:
39- X (scipy.sparse.csr_matrix) :
40- shape = (n items, n features)
41- py (numpy.ndarray, optional) :
42- shape = (1, word), probability of context words.
43- If `py` is None, `pmi` function uses normalized row sum of `X`
44- min_pmi (float) :
45- Minimum value of pmi.
46- alpha (float) :
47- Smoothing factor. Default is `0.0`
48- beta (float) :
49- Smoothing factor. Default is `1.0`
50-
51- Returns:
52- pmi (scipy.sparse.csr_matrix)
53- px (numpy.ndarray)
54- py (numpy.ndarray)
55- """
56-
57- assert 0 < beta <= 1
58-
59- px = np .asarray ((X .sum (axis = 1 ) / X .sum ()).reshape (- 1 ))
60- pxy = X / X .sum ()
61- if py is None :
62- py = np .asarray ((X .sum (axis = 0 ) / X .sum ()).reshape (- 1 ))
63- py_arr : np .ndarray = py
64- if beta < 1 :
65- py_arr = py_arr ** beta
66- py_arr /= py_arr .sum ()
67- assert py_arr .shape [1 ] == pxy .shape [1 ] # type: ignore[index]
68-
69- px_diag = _as_diag (px , 0 )
70- py_diag = _as_diag (py_arr , alpha )
71- exp_pmi = px_diag .dot (pxy ).dot (py_diag )
72-
73- min_exp_pmi = 1 if min_pmi == 0 else np .exp (min_pmi )
74- pmi_mat = _logarithm_and_ppmi (exp_pmi , min_exp_pmi )
75-
76- return pmi_mat , px , py_arr
21+ """.. deprecated:: soynlp.utils.pmi를 사용하세요."""
22+ warnings .warn (
23+ "soynlp.word.pmi is deprecated. Use soynlp.utils.pmi instead." ,
24+ DeprecationWarning ,
25+ stacklevel = 2 ,
26+ )
27+ return _pmi_impl (X , py = py , min_pmi = min_pmi , alpha = alpha , beta = beta )
0 commit comments