4
					
						
						« on: May 24, 2008, 06:03:35 pm »
					 
					
					<<Thank you Art!  I will play with it a bit.>>
Do each of the add-ins run each time a sentence is entered into Hals input screen? If so I can see where installing a lot of add-ins without knowing what they are doing could waste a lot of system resources.   I have a few questions about the AutoWikiKnowledge.uhp file.
Where does the variable/input 'InputString' come from?  
Is this an input variable and if so what would it contain?
Is InputString the same as the MyInput variable?
Does HalXBrain.AppendFile do something different then using Scripting.FileSystemObject object to read and write to files?
I'm working through the code and rewriting it a bit in order to
understand what it is doing and make sure it won't steal to many system resources from my machine.
This is not finished code and has not been tested at all yet!
'---------------------------------------------------------------------
Rem Type=Plugin
Rem Name=Auto Wikipedia Knowledge
Rem Author=Gerald L. Blakley A.K.A OnTheCuttingEdge2005  -- Modified by forum user
Rem Host=All
'This sub setups the plug-ins option panel in Hal's options dialog
Sub OptionsPanel()
    lblPlugin(0).Caption = "Just say: please study about the universe or please study about science, you can replace the word Universe or Science with any other word or noun, all information on the word is studied from the website Wikipedia"
    lblPlugin(0).Move 120, 10, 3300, 1000
    lblPlugin(0).WordWrap = True
    lblPlugin(0).Visible = True
End Sub
Const ForReading = 1
Const ForWriting = 2
'-----------------------------------------------------------------------------
' Definitions
'-----------------------------------------------------------------------------
' MyInput        : The user input?
' InputString   : The user input again?
'-----------------------------------------------------------------------------
' Create a reference to the UltraHalAsst.Brain code library.
'-----------------------------------------------------------------------------
Set HalXBrain = CreateObject("UltraHalAsst.Brain")
'-----------------------------------------------------------------------------
' Determine if we want to process the user input by searching for a form 
' of the desired command and parsing out the specific subject to study.
'-----------------------------------------------------------------------------
WikiToLookup = HalBrain.SearchPattern(MyInput, "*PLEASE STUDY ABOUT THE *", 2)
If WikiToLookup = "" Then WikiToLookup = HalBrain.SearchPattern(MyInput, "*PLEASE STUDY ABOUT *", 2)
If WikiToLookup = "" Then WikiToLookup = HalBrain.SearchPattern(MyInput, "*DO YOU KNOW ANYTHING ABOUT THE *", 2)
If WikiToLookup = "" Then WikiToLookup = HalBrain.SearchPattern(MyInput, "*DO YOU KNOW ANYTHING ABOUT *", 2)
'-----------------------------------------------------------------------------
' If the user input matches what we want to respond to process it.
'-----------------------------------------------------------------------------
If WikiToLookup <> "" Then 
'-----------------------------------------------------------------------------
' Initialize variables
'-----------------------------------------------------------------------------
   XDir = "C:Program FilesabawareUltra Hal Assistant 6\"
   MyFile = "C:Program FilesabawareUltra Hal Assistant 6AutoText.brn"
   SourcePage = "http://en.wikipedia.org/wiki/" & WikiToLookup
   strNewContents = ""
'-----------------------------------------------------------------------------
' Clean the user input.
'-----------------------------------------------------------------------------
   MyInput = HalXBrain.AlphaNumericalOnly(InputString)
'--------------------------------------------
' Read a page off the internet
'--------------------------------------------
   objExplorer.Navigate(SourcePage)
   Set objExplorer = CreateObject("InternetExplorer.Application")
   Do Until objExplorer.ReadyState=4 : Loop
   myText = objExplorer.Document.Body.innerText
   Set objExplorer = Nothing
'--------------------------------------------
' Write internet page information to file 
'--------------------------------------------
   If Len(myText) > 0 then
      If FileSys.FileExists(MyFile) = False Then
         HalXBrain.AppendFile MyFile, "Start of Knowledge strings"
      End If
      HalXBrain.AppendFile MyFile, Trim(myText)
   End If
'--------------------------------------------
' Respond to user
'--------------------------------------------
   UltraHal = " I have looked up the study of " & WikiToLookup & ", Thanks. "
'--------------------------------------------
' The rest of the code below looks like it is  attempting to 
' cleanse the existing AutoText.brn file.
' It should be in a subroutine of it's own.
' I wouldn't want to clean the whole text file
' every time Hal get's a a new line of input.
'--------------------------------------------
'--------------------------------------------
' Read all of MyFile into variable strText
'--------------------------------------------
   strText = ReadFile(MyFile) 
'--------------------------------------------
' Clean file
'--------------------------------------------
   If InStr(1, strText, "   ", vbTextCompare) > 0 Then   strText = CleanFile(MyFile) 
'--------------------------------------------
' Write the cleansed text back to the file.
'--------------------------------------------
   If Len(strText) > 0 Then WriteFile(MyFile, strText)   
'--------------------------------------------
' Calculate the number of sentences based on the number of lines ending in periods.
' I would think this should also take into consideration question marks and exclamation points.
'--------------------------------------------
   TextCount = StrCount()
'--------------------------------------------
' I haven't worked on this yet.
' it looks like it is attempting to compare each line in the 
' file with the user input and then have Hal read back one of them.
' I didn't notice any feedback from Hal after the indicated he had studied the subject
' so I'm not sure what is going on here.
'--------------------------------------------
'--------------------------------------------
' For each sentenct retrieved from MyFile
'--------------------------------------------
   For i = 1 To TextCount
      ClipResX = HalXBrain.ChooseSentenceFromFile(MyFile)
      ClipResX = HalXBrain.AlphaNumericalOnly(ClipResX)
      ClipResX = UCase(ClipResX)
      ClipResX = Trim(ClipResX)
      MyInputString = HalXBrain.AlphaNumericalOnly(InputString)
      MyInputString = UCase(MyInputString)
      MyInputString = Trim(MyInputString)
      If InStr(1, ClipResX, MyInputString, vbTextCompare) > 0 And Len(ClipResX) > 50 Then 
         UltraHal = ClipResX
      ElseIf Len(ClipResX) < 50 Then
         UltraHal = UltraHal
      End If
   Next
End If
'********************************************************************
'*
'********************************************************************
Function StrCount()
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  Set objTextFile = objFSO.OpenTextFile("C:Program FilesabawareUltra Hal Assistant 6AutoText.brn", ReadVariable())
  strText = objTextFile.ReadAll
  objTextFile.Close
         
  Do While InStr(1, strText, "  ")
   strText = Replace(strText, "  ", " ")
   Exit Do
  Loop
  aWords = split(strText, ". ")
  StrCount = Ubound(aWords) + 1
End Function
'********************************************************************
'*
'********************************************************************
Sub WriteFile(sFilePathAndName,sFileContents)   
  Const ForWriting =2 
  Set oFS = Server.CreateObject("Scripting.FileSystemObject") 
  Set oFSFile = oFS.OpenTextFile(sFilePathAndName,ForWriting,True) 
  oFSFile.Write(sFileContents) 
  oFSFile.Close 
  Set oFSFile = Nothing 
  Set oFS = Nothing 
End Sub 
'********************************************************************
'*
'********************************************************************
Function ReadFile(sFilePathAndName) 
   dim sFileContents 
   Set oFS = Server.CreateObject("Scripting.FileSystemObject") 
   If oFS.FileExists(sFilePathAndName) = True Then 
      Set oTextStream = oFS.OpenTextFile(sFilePathAndName,1) 
      sFileContents = oTextStream.ReadAll 
      oTextStream.Close 
      Set oTextStream = nothing 
   End if 
   Set oFS = nothing 
   ReadFile = sFileContents 
End Function 
'********************************************************************
'*
'********************************************************************
Sub CleanFile(sFilePathAndName) 
'--------------------------------------------
' 1. Read a line and removing preceding and trailing spaces.
' 2' Append vbCrLf to the line and then append it to the new text variable strNewContents.
'--------------------------------------------
   Const ForReading = 1 
   Const ForWriting = 2 
   Const ForAppending = 8 
   Const TristateUseDefault = -2 
   Const TristateTrue = -1 
   Const TristateFalse = 0 
   Dim oFS 
   Dim oFile 
   Dim oStream 
   Dim sRecord
   Dim strNewContents
   Set oFS = Server.CreateObject("Scripting.FileSystemObject") 
   Set oFile = oFS.GetFile(sFilePathAndName) 
   Set oStream = oFile.OpenAsTextStream(ForReading, TristateUseDefault) 
   Do While Not oStream.AtEndOfStream 
      sRecord=oStream.ReadLine 
'--------------------------------------------
' Clean the text
'--------------------------------------------
   sRecord = Trim(sRecord)
'--------------------------------------------
' Append to new contents string
'--------------------------------------------
   If Len(sRecord) > 0 Then strNewContents = strNewContents & sRecord & vbCrLf
   Loop 
   oStream.Close 
   CleanFile = strNewContents 
  End Sub