| 2365 | return Classifier.load(path) |
| 2366 | |
| 2367 | def _on_load(self, path): |
| 2368 | # Called from Classifier.load(). |
| 2369 | # The actual SVM model was stored as a string. |
| 2370 | # 1) Import pattern.vector.svm. |
| 2371 | # 2) Extract the model string. |
| 2372 | # 3) Save it as a temporary file. |
| 2373 | # 4) Use pattern.vector.svm's LIBSVM or LIBLINEAR to load the file. |
| 2374 | # 5) Delete the temporary file. |
| 2375 | import svm |
| 2376 | self._svm = svm # 1 |
| 2377 | if self._model is not None: |
| 2378 | f = open(path + ".tmp", "w") |
| 2379 | f.write(self._model[0]) # 2 + 3 |
| 2380 | f.close() |
| 2381 | _load = self.extension == LIBLINEAR and \ |
| 2382 | svm.liblinearutil.load_model or \ |
| 2383 | svm.libsvmutil.svm_load_model |
| 2384 | self._model = (_load(path + ".tmp"),) + self._model[1:] # 4 |
| 2385 | os.remove(f.name) # 5 |
| 2386 | |
| 2387 | #--------------------------------------------------------------------------------------------------- |
| 2388 | # "Nothing beats SVM + character n-grams." |