@@ -8,112 +8,87 @@ const char *executableFolder() {
8
8
return folderPath.c_str ();
9
9
}
10
10
11
- double MainFunc (const char *arg) {
12
- PyObject *pName = NULL , *importlib = NULL , *importlib__import_module = NULL ;
13
- PyObject *vaderSentiment = NULL , *pFunc = NULL , *pArgs = NULL , *pArgs2 = NULL ;
14
- PyObject *pModule2 = NULL , *importlib__import_module__args = NULL , *analyser = NULL ;
15
- PyObject *result = NULL ;
16
- PyObject *pModule2_path = NULL , *pModule2_path_insert = NULL ;
17
-
18
- double score = 1.0 ;
19
-
20
- Py_Initialize ();
21
-
22
- pName = PyUnicode_DecodeFSDefault (" importlib" );
23
- if (!pName) goto error;
24
-
25
- importlib = PyImport_Import (pName);
26
- Py_DECREF (pName);
27
- if (!importlib) goto error;
28
-
29
- pModule2 = PyImport_Import (PyUnicode_DecodeFSDefault (" sys" ));
30
- if (!pModule2) goto error;
31
-
32
- pArgs2 = PyTuple_New (2 );
33
- if (!pArgs2) goto error;
34
-
35
- PyTuple_SET_ITEM (pArgs2, 0 , PyLong_FromLong (0 ));
36
- PyTuple_SET_ITEM (pArgs2, 1 , PyUnicode_DecodeFSDefault (executableFolder ()));
37
-
38
- pModule2_path = PyObject_GetAttrString (pModule2, " path" );
39
- if (!pModule2_path) goto error;
40
-
41
- pModule2_path_insert = PyObject_GetAttrString (pModule2_path, " insert" );
42
- if (!pModule2_path_insert) goto error;
43
-
44
- if (PyObject_CallObject (pModule2_path_insert, pArgs2) == NULL ) {
45
- goto error;
11
+ // Define a constant for the default/error value
12
+ const double DEFAULT_ERROR_VALUE = 0.0 ;
13
+
14
+ // Global variables to hold Python objects and initialization state
15
+ static PyObject* analyser = NULL ;
16
+ static bool isPythonInitialized = false ;
17
+
18
+ void InitializePython () {
19
+ if (!isPythonInitialized) {
20
+ Py_Initialize ();
21
+
22
+ PyObject *pName = PyUnicode_DecodeFSDefault (" importlib" );
23
+ PyObject *importlib = PyImport_Import (pName);
24
+ Py_DECREF (pName);
25
+ if (!importlib) {
26
+ PyErr_Print ();
27
+ fprintf (stderr, " Failed to load \" importlib\" module\n " );
28
+ return ;
29
+ }
30
+
31
+ PyObject *pModule2 = PyImport_Import (PyUnicode_DecodeFSDefault (" sys" ));
32
+ PyObject *pArgs2 = PyTuple_New (2 );
33
+ PyTuple_SET_ITEM (pArgs2, 0 , PyLong_FromLong (0 ));
34
+ PyTuple_SET_ITEM (pArgs2, 1 , PyUnicode_DecodeFSDefault (executableFolder ()));
35
+
36
+ PyObject *pModule2_path = PyObject_GetAttrString (pModule2, " path" );
37
+ PyObject *pModule2_path_insert = PyObject_GetAttrString (pModule2_path, " insert" );
38
+ PyObject *insertResult = PyObject_CallObject (pModule2_path_insert, pArgs2);
39
+ Py_DECREF (insertResult);
40
+ Py_DECREF (pArgs2);
41
+ Py_DECREF (pModule2_path_insert);
42
+ Py_DECREF (pModule2_path);
43
+ Py_DECREF (pModule2);
44
+
45
+ PyObject *importlib__import_module = PyObject_GetAttrString (importlib, " import_module" );
46
+ PyObject *importlib__import_module__args = PyTuple_New (1 );
47
+ PyTuple_SET_ITEM (importlib__import_module__args, 0 , PyUnicode_DecodeFSDefault (" vaderSentiment-master.vaderSentiment.vaderSentiment" ));
48
+
49
+ PyObject *vaderSentiment = PyObject_CallObject (importlib__import_module, importlib__import_module__args);
50
+ Py_DECREF (importlib__import_module__args);
51
+ Py_DECREF (importlib__import_module);
52
+ Py_DECREF (importlib);
53
+
54
+ if (vaderSentiment) {
55
+ PyObject *pFunc = PyObject_GetAttrString (vaderSentiment, " SentimentIntensityAnalyzer" );
56
+ if (pFunc && PyCallable_Check (pFunc)) {
57
+ PyObject *pArgs = PyTuple_New (0 );
58
+ analyser = PyObject_CallObject (pFunc, pArgs);
59
+ Py_DECREF (pArgs);
60
+ }
61
+ Py_XDECREF (pFunc);
62
+ Py_DECREF (vaderSentiment);
63
+ }
64
+
65
+ if (!analyser) {
66
+ PyErr_Print ();
67
+ fprintf (stderr, " Failed to create SentimentIntensityAnalyzer\n " );
68
+ }
69
+
70
+ isPythonInitialized = true ;
46
71
}
72
+ }
47
73
48
- importlib__import_module = PyObject_GetAttrString (importlib, " import_module" );
49
- if (!importlib__import_module) goto error;
50
-
51
- importlib__import_module__args = PyTuple_New (1 );
52
- if (!importlib__import_module__args) goto error;
53
-
54
- PyTuple_SET_ITEM (importlib__import_module__args, 0 ,
55
- PyUnicode_DecodeFSDefault (" vaderSentiment-master.vaderSentiment.vaderSentiment" ));
56
- vaderSentiment = PyObject_CallObject (importlib__import_module, importlib__import_module__args);
57
- if (!vaderSentiment) goto error;
58
-
59
- pFunc = PyObject_GetAttrString (vaderSentiment, " SentimentIntensityAnalyzer" );
60
- if (!pFunc || !PyCallable_Check (pFunc)) {
61
- if (PyErr_Occurred ()) PyErr_Print ();
62
- fprintf (stderr, " Cannot find function \" %s\"\n " , " SentimentIntensityAnalyzer" );
63
- goto error;
74
+ double MainFunc (const char *arg) {
75
+ if (!isPythonInitialized) {
76
+ InitializePython ();
64
77
}
65
78
66
- pArgs = PyTuple_New (0 );
67
- if (!pArgs) goto error;
68
-
69
- analyser = PyObject_CallObject (pFunc, pArgs);
70
- Py_DECREF (pArgs);
71
- if (!analyser) goto error;
79
+ if (!analyser) {
80
+ return DEFAULT_ERROR_VALUE;
81
+ }
72
82
73
- result = PyObject_CallMethod (analyser, " polarity_scores" , " (s)" , arg);
83
+ PyObject * result = PyObject_CallMethod (analyser, " polarity_scores" , " (s)" , arg);
74
84
if (!result) {
75
85
PyErr_Print ();
76
- fprintf (stderr, " Call failed\n " );
77
- goto error ;
86
+ fprintf (stderr, " Call to polarity_scores failed\n " );
87
+ return DEFAULT_ERROR_VALUE ;
78
88
}
79
89
80
- score = PyFloat_AsDouble (PyDict_GetItemString (result, " compound" ));
81
-
90
+ double score = PyFloat_AsDouble (PyDict_GetItemString (result, " compound" ));
82
91
Py_DECREF (result);
83
- Py_DECREF (analyser);
84
- Py_DECREF (importlib__import_module__args);
85
- Py_DECREF (pFunc);
86
- Py_DECREF (vaderSentiment);
87
- Py_DECREF (importlib__import_module);
88
- Py_DECREF (importlib);
89
- Py_DECREF (pModule2);
90
- Py_DECREF (pModule2_path);
91
- Py_DECREF (pModule2_path_insert);
92
- Py_DECREF (pArgs2);
93
-
94
- if (Py_FinalizeEx () < 0 ) {
95
- return 120 ;
96
- }
97
-
98
- return score;
99
-
100
- error:
101
- Py_XDECREF (result);
102
- Py_XDECREF (analyser);
103
- Py_XDECREF (importlib__import_module__args);
104
- Py_XDECREF (pFunc);
105
- Py_XDECREF (vaderSentiment);
106
- Py_XDECREF (importlib__import_module);
107
- Py_XDECREF (importlib);
108
- Py_XDECREF (pModule2);
109
- Py_XDECREF (pModule2_path);
110
- Py_XDECREF (pModule2_path_insert);
111
- Py_XDECREF (pArgs2);
112
-
113
- if (Py_FinalizeEx () < 0 ) {
114
- return 120 ;
115
- }
116
-
117
92
return score;
118
93
}
119
94
@@ -138,4 +113,4 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) {
138
113
return exports;
139
114
}
140
115
141
- NODE_API_MODULE (hello , Init);
116
+ NODE_API_MODULE (NODE_GYP_MODULE_NAME , Init);
0 commit comments