The QBasic Forum     RULES     Other Subforums, Links and Downloads

  
--

 Return to Index  

QBFilter

September 29 2005 at 4:22 PM
  (no login)


Response to ProgramList Dustinian

 
'QBFilter - Dustinian Camburides - 2005
'http://www.dustinian.com/software.html

'This program, set up to run as a demo right now, is intended
'to read input strings for text-based games, remove non-alphanumeric
'characters (except spaces) from them, remove multiple spaces from
'them, lcase them, separate them into individual words, and them
'elminate unneccessary words (prepositions [on, in, etc.] and
'determiners [a, the, etc.], so that only the important words in
'the command (hopefully: verb, direct object, indirect object)
'survive to be processed. This allows text-based games to be way
'more flexible about the input it can accept. Check out the demo.

DECLARE SUB FilterCommand (strString AS STRING, strWords() AS STRING, intNumWords AS INTEGER)
DECLARE FUNCTION AlphaNumeric$ (strString AS STRING)
DECLARE FUNCTION PrepareString$ (strString AS STRING)
DECLARE FUNCTION RemoveDoubleSpaces$ (strString AS STRING)
DECLARE SUB GetWords (strString AS STRING, strWords() AS STRING, intNumWords AS INTEGER)
DECLARE SUB ResizeArrayOfStrings (strArray() AS STRING, intNewSize AS INTEGER)
DECLARE SUB FilterExtraWords (strWords() AS STRING, intNumWords AS INTEGER)
DECLARE SUB ListDeterminers (strDeterminers() AS STRING, intNumDeterminers AS INTEGER)
DECLARE SUB ListPrepositions (strPrepositions() AS STRING, intNumPrepositions AS INTEGER)
DECLARE SUB RemoveWord (strWords() AS STRING, intNumWords AS INTEGER, intWord AS INTEGER)
'DECLARE SUB ResizeArrayOfStrings (strArray() AS STRING, intNewSize AS INTEGER)

'Dimension bounds:
DIM intNumWords AS INTEGER
DIM intNumObjects AS INTEGER

'Initialize bounds:
intNumWords = 1
intNumObjects = 1

'Initialize Variables:
DIM strString AS STRING
DIM strWords(1 TO intNumWords) AS STRING

'Demo Code:

'Get a string form the user.
INPUT ">", strString

'Filter the user's string into an array of words (strWords()).
CALL FilterCommand(strString, strWords(), intNumWords)

'Output the array of words for the user to see.
DIM I AS INTEGER
FOR I = 1 TO intNumWords
PRINT I, strWords(I)
NEXT I

END

FUNCTION AlphaNumeric$ (strString AS STRING)


'SUMMARY:
'AlphaNumeric$ filters all punctuation and symbols out
'of string, leaving only letters (Aa-Zz), numerals
'(0-9), and spaces.

'INPUT:
'strString: The input string being filtered.

'OUTPUT:
'AlphaNumeric$: Returns only the input string's
'alphanumeric characters and spaces.


'Dimension Variables:
DIM intI AS INTEGER 'Increment Variable.
DIM intStringLength AS INTEGER 'The length of the string.
DIM strCharacter AS STRING * 1 'The character currently being tested.
DIM strOutput AS STRING 'The new string, with only AlphaNumeric characters.


'Preprocessing:
strOutput = ""
intStringLength = LEN(strString)

'Processing:
FOR intI = 1 TO intStringLength

'Get the next character to be tested.
strCharacter = MID$(strString, intI, 1)


'Add the current character to the output if:

' 1. It belongs to (a-z).
IF ((strCharacter >= "a") AND (strCharacter <= "z")) THEN
strOutput = strOutput + strCharacter

' 2. It belongs to (A-Z).
ELSEIF ((strCharacter >= "A") AND (strCharacter <= "Z")) THEN
strOutput = strOutput + strCharacter

' 3. It belongs to (0-9).
ELSEIF ((strCharacter >= "0") AND (strCharacter <= "9")) THEN
strOutput = strOutput + strCharacter

' 4. It is a space.
ELSEIF (strCharacter = " ") THEN
strOutput = strOutput + strCharacter
END IF
NEXT intI

'Output:
AlphaNumeric$ = strOutput

END FUNCTION

SUB FilterCommand (strString AS STRING, strWords() AS STRING, intNumWords AS INTEGER)
strString = AlphaNumeric$(strString)
strString = PrepareString$(strString)
strString = RemoveDoubleSpaces$(strString)
CALL GetWords(strString, strWords(), intNumWords)
CALL FilterExtraWords(strWords(), intNumWords)
END SUB

SUB FilterExtraWords (strWords() AS STRING, intNumWords AS INTEGER)
DIM intNumFilter AS INTEGER
intNumFilter = 1
DIM strFilter(1 TO intNumFilter) AS STRING
DIM intI AS INTEGER
DIM intJ AS INTEGER
DIM intFound AS INTEGER

CALL ListPrepositions(strFilter(), intNumFilter)

FOR intH = 1 TO 2
IF intH = 1 THEN
CALL ListPrepositions(strFilter(), intNumFilter)
ELSEIF intH = 2 THEN
CALL ListDeterminers(strFilter(), intNumFilter)
END IF

intI = 1
WHILE (intI <= intNumWords)
intJ = 1
WHILE (intJ <= intNumFilter) AND (intFound = 0)
IF strWords(intI) = strFilter(intJ) THEN
CALL RemoveWord(strWords(), intNumWords, intI)
intFound = 1
END IF
intJ = intJ + 1

WEND
IF (NOT intFound) THEN intI = intI + 1
intFound = 0
WEND
NEXT intH

END SUB

SUB GetWords (strString AS STRING, strWords() AS STRING, intNumWords AS INTEGER)
'Getwords extracts individual words from the input string.

'Dimension Variables:
DIM intLocation AS INTEGER
DIM intStart AS INTEGER

'Preprocessing:
strString = strString + " "
intNumWords = 0
intStart = 1
intLocation = INSTR(intStart, strString, " ")

'Processing:
WHILE (intLocation > 0)
intNumWords = intNumWords + 1
CALL ResizeArrayOfStrings(strWords(), intNumWords)
strWords(intNumWords) = MID$(strString, intStart, (intLocation - intStart))
intStart = intLocation + 1
intLocation = INSTR(intStart, strString, " ")
WEND

END SUB

SUB ListDeterminers (strDeterminers() AS STRING, intNumDeterminers AS INTEGER)
intNumDeterminers = 19
CALL ResizeArrayOfStrings(strDeterminers(), intNumDeterminers)
strDeterminers(1) = "the"
strDeterminers(2) = "a"
strDeterminers(3) = "an"
strDeterminers(4) = "another"
strDeterminers(5) = "no"
strDeterminers(6) = "some"
strDeterminers(7) = "any"
strDeterminers(8) = "my"
strDeterminers(9) = "our"
strDeterminers(10) = "their"
strDeterminers(11) = "her"
strDeterminers(12) = "his"
strDeterminers(13) = "its"
strDeterminers(14) = "each"
strDeterminers(15) = "every"
strDeterminers(16) = "certain"
strDeterminers(17) = "another"
strDeterminers(18) = "this"
strDeterminers(19) = "that"
END SUB

SUB ListPrepositions (strPrepositions() AS STRING, intNumPrepositions AS INTEGER)
intNumPrepositions = 43
CALL ResizeArrayOfStrings(strPrepositions(), intNumPrepositions)
strPrepositions(1) = "about"
strPrepositions(2) = "above"
strPrepositions(3) = "across"
strPrepositions(4) = "after"
strPrepositions(5) = "against"
strPrepositions(6) = "around"
strPrepositions(7) = "at"
strPrepositions(8) = "before"
strPrepositions(9) = "behind"
strPrepositions(10) = "below"
strPrepositions(11) = "beneath"
strPrepositions(12) = "beside"
strPrepositions(13) = "besides"
strPrepositions(14) = "between"
strPrepositions(15) = "beyond"
strPrepositions(16) = "down"
strPrepositions(17) = "during"
strPrepositions(18) = "except"
strPrepositions(19) = "for"
strPrepositions(10) = "from"
strPrepositions(21) = "in"
strPrepositions(22) = "inside"
strPrepositions(23) = "into"
strPrepositions(24) = "like"
strPrepositions(25) = "near"
strPrepositions(26) = "of"
strPrepositions(27) = "off"
strPrepositions(28) = "on"
strPrepositions(29) = "out"
strPrepositions(20) = "outside"
strPrepositions(31) = "over"
strPrepositions(32) = "since"
strPrepositions(33) = "through"
strPrepositions(34) = "throughout"
strPrepositions(35) = "till"
strPrepositions(36) = "to"
strPrepositions(37) = "toward"
strPrepositions(38) = "under"
strPrepositions(39) = "until"
strPrepositions(30) = "up"
strPrepositions(41) = "upon"
strPrepositions(42) = "with"
strPrepositions(43) = "without"
END SUB

FUNCTION PrepareString$ (strString AS STRING)
'Processing:
strString = LCASE$(strString)
strString = LTRIM$(strString)
strString = RTRIM$(strString)

'Output:
PrepareString$ = strString

END FUNCTION

FUNCTION RemoveDoubleSpaces$ (strString AS STRING)
'RemoveDouble" "s$ removes all " " instances until there are only " " instances.

'Dimension Variables:
DIM intLocation AS INTEGER


'Preprocessing:
intLocation = INSTR(strString, " ")

'Processing:
WHILE (intLocation > 0)
strString = MID$(strString, 1, intLocation) + MID$(strString, (intLocation + 2), (LEN(strString) - (intLocation + 1)))

intLocation = INSTR(strString, " ")
WEND

'Output:
RemoveDoubleSpaces$ = strString


END FUNCTION

SUB RemoveWord (strWords() AS STRING, intNumWords AS INTEGER, intWord AS INTEGER)
DIM intI AS INTEGER

intI = intWord
strWords(intI) = ""

WHILE (intI < intNumWords)
strWords(intI) = strWords(intI + 1)
intI = intI + 1
WEND

intNumWords = intNumWords - 1
CALL ResizeArrayOfStrings(strWords(), intNumWords)
END SUB

SUB ResizeArrayOfStrings (strArray() AS STRING, intNewSize AS INTEGER)

'DIMENSION VARIABLES:
DIM intI AS INTEGER
DIM intJ AS INTEGER

'PREPROCESSING:
'Dimension the temporary array:
DIM TempArray(1 TO intNewSize) AS STRING

'Determine the lesser bound to avoid reading/writing past the end of one of the arrays:
IF (intNewSize < UBOUND(strArray)) THEN
intJ = intNewSize
ELSE
intJ = UBOUND(strArray)
END IF

'PROCESSING:
'Copy the array to the temporary array:
FOR intI = 1 TO intJ
TempArray(intI) = strArray(intI)
NEXT intI

'Redimension the array:
REDIM strArray(1 TO intNewSize) AS STRING

'Copy the temporary array to the array:
FOR intI = 1 TO intJ
strArray(intI) = TempArray(intI)
NEXT intI
END SUB

 
 Respond to this message   
Responses

 Copyright © 1999-2008 Network54. All rights reserved.   Terms of Use   Privacy Statement