6
6
from langkit .whylogs .unreg import unregister_udfs
7
7
8
8
9
- def sentiment_nltk (text : str , sentiment_analyzer ) -> float :
9
+ _registered : Set [str ] = set ()
10
+
11
+
12
+ _nltk_downloaded = None
13
+ _response_nltk_downloaded = None
14
+ _sentiment_analyzer = None
15
+ _response_sentiment_analyzer = None
16
+
17
+ _pipeline = None
18
+ _response_pipeline = None
19
+
20
+ _initialized = False
21
+
22
+
23
+ def sentiment_nltk (text : str , sentiment_analyzer = None ) -> float :
24
+ if not _initialized :
25
+ init ()
26
+ sentiment_analyzer = sentiment_analyzer or _sentiment_analyzer
10
27
if sentiment_analyzer is None :
11
28
raise ValueError (
12
29
"sentiment metrics must initialize sentiment analyzer before evaluation!"
@@ -30,7 +47,10 @@ def sentiment_nltk(text: str, sentiment_analyzer) -> float:
30
47
}
31
48
32
49
33
- def sentiment_multilingual (text : str , pipeline ) -> float :
50
+ def sentiment_multilingual (text : str , pipeline = None ) -> float :
51
+ if not _initialized :
52
+ init ()
53
+ pipeline = pipeline or _pipeline
34
54
if pipeline is None :
35
55
raise ValueError ("sentiment score must initialize the pipeline first" )
36
56
@@ -47,15 +67,6 @@ def _wrappee(text):
47
67
return _wrappee
48
68
49
69
50
- _registered : Set [str ] = set ()
51
-
52
-
53
- _nltk_downloaded = None
54
- _response_nltk_downloaded = None
55
- _sentiment_analyzer = None
56
- _response_sentiment_analyzer = None
57
-
58
-
59
70
def configure_nltk (config , lexicon , response_lexicon ):
60
71
import nltk
61
72
from nltk .sentiment import SentimentIntensityAnalyzer
@@ -83,10 +94,6 @@ def configure_nltk(config, lexicon, response_lexicon):
83
94
_response_sentiment_analyzer = None
84
95
85
96
86
- _pipeline = None
87
- _response_pipeline = None
88
-
89
-
90
97
def configure_hugging_face (config , sentiment_model_path , response_sentiment_model_path ):
91
98
from transformers import pipeline
92
99
@@ -112,6 +119,9 @@ def init(
112
119
sentiment_model_path : Optional [str ] = None ,
113
120
response_sentiment_model_path : Optional [str ] = None ,
114
121
):
122
+ global _initialized
123
+ _initialized = True
124
+
115
125
global _registered
116
126
unregister_udfs (_registered )
117
127
0 commit comments