VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "PC_Tools"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
' ====== Доступ к различным парсерам =======
' Shared module version: 20220801
' Tested in:
' Depends on: ParserDeclarations, DetectorListWords, DetectorRegex, DetectorMorpho, DetectorClassifier
' Required reference:
Option Explicit

Public Function Detector(iType As TDetector, sParam$) As Object
' Any Detector implements methods:
'   Public Function Test(sText$) As Boolean
'   Public Function ExtractFragments(sText$) As PC_ParsedData
  Select Case iType
    Case TDetector.T_DETECTOR_ACTION: Set Detector = CachedActionDetector
    Case TDetector.T_DETECTOR_LIST: Set Detector = CachedDetectorList(sParam)
    Case TDetector.T_DETECTOR_REGEX: Set Detector = CachedDetectorRegex(sParam)
    Case TDetector.T_DETECTOR_MORPHO: Set Detector = CachedDetectorMorpho(sParam)
    Case TDetector.T_DETECTOR_DATE:
      Set Detector = New DetectorRegex: Call Detector.Init(GlobalDateRegex)
    Case TDetector.T_DETECTOR_NPA:
      Set Detector = New DetectorRegex: Call Detector.Init(GlobalNPARegex)
    Case TDetector.T_DETECTOR_BASIC_NER: Set Detector = CachedDetectorNER(sParam)
  End Select
  If Detector Is Nothing Then _
    Call Err.Raise(CUSTOM_ERROR_DEBUG)
End Function

Public Function Parser(iType As TDetector, sParam$) As Object
' Any Parser implements methods:
'   Public Function Test(sText$) As Boolean
'   Public Function Parse(sText$) As Boolean
'   Public Function GetData() As Collection
'   Public Function GetDataDescription() As Scripting.Dictionary
'   Public Function Transform(sText$, sParam$) As String
  Select Case iType
    Case TDetector.T_DETECTOR_ACTION: Set Parser = Nothing
    Case TDetector.T_DETECTOR_LIST: Set Parser = Nothing
    Case TDetector.T_DETECTOR_REGEX: Set Parser = Nothing
    Case TDetector.T_DETECTOR_MORPHO: Set Parser = Nothing
    Case TDetector.T_DETECTOR_DATE: Set Parser = CachedParserDate
    Case TDetector.T_DETECTOR_NPA: Set Parser = CachedParserNPA
    Case TDetector.T_DETECTOR_BASIC_NER: Set Parser = Nothing
  End Select
End Function

' ========
Private Function CachedActionDetector() As DetectorListWords
  Static s_Detector As DetectorListWords
  If s_Detector Is Nothing Then
    Set s_Detector = New DetectorListWords
    Call s_Detector.Init(LOCAL_MODELS & "\" & MODEL_ACTION_VERBS)
  End If
  Set CachedActionDetector = s_Detector
End Function

Private Function CachedDetectorList(sParam$) As DetectorListWords
  Static s_Param$
  Static s_Detector As DetectorListWords
  If s_Detector Is Nothing Or sParam <> s_Param Then
    s_Param = sParam
    Set s_Detector = New DetectorListWords
    Call s_Detector.Init(sParam)
  End If
  Set CachedDetectorList = s_Detector
End Function

Private Function CachedDetectorRegex(sParam$) As DetectorRegex
  Static s_Regex As RegExp
  Static s_Param$
  Static s_Detector As DetectorRegex
  If s_Detector Is Nothing Or sParam <> s_Param Then
    s_Param = sParam
    Set s_Regex = New RegExp
    s_Regex.Global = True
    s_Regex.Pattern = sParam
    
    Set s_Detector = New DetectorRegex
    Call s_Detector.Init(s_Regex)
  End If
  Set CachedDetectorRegex = s_Detector
End Function

Private Function CachedDetectorMorpho(sParam$) As DetectorMorpho
  Static s_Param$
  Static s_Detector As DetectorMorpho
  If s_Detector Is Nothing Or sParam <> s_Param Then
    s_Param = sParam
    Set s_Detector = New DetectorMorpho
    Call s_Detector.Init(sParam)
  End If
  Set CachedDetectorMorpho = s_Detector
End Function

Private Function CachedParserDate() As ParserDate
  Static s_Parser As ParserDate
  If s_Parser Is Nothing Then
    Set s_Parser = New ParserDate
  End If
  Set CachedParserDate = s_Parser
End Function

Private Function CachedParserNPA() As ParserNPA
  Static s_Parser As ParserNPA
  If s_Parser Is Nothing Then
    Set s_Parser = New ParserNPA
  End If
  Set CachedParserNPA = s_Parser
End Function

Private Function CachedDetectorNER(sParam$) As DetectorClassifier
  Static s_Param$
  Static s_Detector As DetectorClassifier
  If s_Detector Is Nothing Or sParam <> s_Param Then
    s_Param = sParam
    Set s_Detector = New DetectorClassifier
    Call s_Detector.Init(sParam)
  End If
  Set CachedDetectorNER = s_Detector
End Function
