nlp_eng.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2021 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. // NLP-ENGINE requires LINUX to be set for linux compiles.
  14. // The NLP-ENGINE needs to be updated to __linux__.
  15. #ifdef __linux__
  16. #define LINUX 1
  17. #endif
  18. #include "nlp_engine.h"
  19. #include "nlp_eng.hpp"
  20. static NLP_ENGINE *nlpEngine = NULL;
  21. static CriticalSection csNLP;
  22. NLPEng::NLPEng() {}
  23. NLPEng::~NLPEng() {}
  24. #include <sstream>
  25. int NLPEng::nlpEngAnalyze(const char *analyzerName, const char *inputText, ostringstream &sso)
  26. {
  27. string w = queryCurrentProcessPath();
  28. #ifdef NLP_DEBUG
  29. ofstream handle;
  30. handle.open(NLP_DEBUG_FILE, std::ofstream::out | std::ofstream::app);
  31. handle << "[queryCurrentProcessPath: " << w << "]" << endl;
  32. #endif
  33. // This is where the nlp analyzers reside that can be called by the user.
  34. // The path logic here is hacked for now for the first version given that
  35. // how these files will get to the server has yet to be determined.
  36. size_t pos = w.find_last_of("/");
  37. size_t pos2 = w.find_last_of("/",pos-1);
  38. string parent = w.substr(0,pos2);
  39. VStringBuffer workingFolder("%s/%s",parent.c_str(),"plugins/nlp/nlp-engine");
  40. #ifdef NLP_DEBUG
  41. handle << "[parent: " << parent << "]" << endl;
  42. handle << "[workingFolder nlp: " << workingFolder.str() << "]" << endl;
  43. handle.close();
  44. #endif
  45. {
  46. CriticalBlock block(csNLP);
  47. if (nlpEngine == NULL) {
  48. nlpEngine = new NLP_ENGINE(workingFolder.str());
  49. }
  50. }
  51. istrstream ssi(inputText);
  52. #ifdef NLP_DEBUG
  53. clock_t s_time, e_time;
  54. s_time = clock();
  55. #endif
  56. nlpEngine->analyze((char *)analyzerName,&ssi,&sso);
  57. #ifdef NLP_DEBUG
  58. e_time = clock();
  59. handle.open(NLP_DEBUG_FILE, std::ofstream::out | std::ofstream::app);
  60. handle << "===============================================" << endl;
  61. handle << "[Analyzer: " << analyzerName << "]" << endl;
  62. handle << "[Text: " << inputText << "]" << endl;
  63. handle << "[Exec analyzer time="
  64. << (double) (e_time - s_time)/CLOCKS_PER_SEC
  65. << " sec]" << endl;
  66. handle << sso.str() << endl;
  67. handle.close();
  68. #endif
  69. sso.seekp(0, ios_base::end);
  70. return sso.tellp();
  71. }