Grammar.au3

Last modified:   Tuesday, 23 December 2008

;*******************************************************************************
;Functions to assist with grammar.
;
;   Function List
;         _Grammar_AintRight()
;         _Grammar_RemoveContractions()
;*******************************************************************************

#Include-once

;===============================================================================
; Function Name:   _Grammar_AintRight()
; Description:     Rids the world of the (non)word Ain't
; Syntax:          _Grammar_AintRight($sStr)
; Parameter(s):    $sStr - String or file to test
; Requirement(s):  
; Return Value(s): - Success = Corrected string, @Extended contains the number of changes made
;                  - Failure = Sets @Error to 2 if $sStr is not valid
;                              returns the string ($sStr) if aint or ain't are not found in the string
; Author(s):       George (GEOSoft) Gedye
; Modification(s): 
; Note(s):    
; Example(s):   
#cs

#ce
;===============================================================================

Func _Grammar_AintRight($sStr)
   If FileExists($sStr) Then $sStr = FileRead($sStr)
   If NOT $sStr Then Return SetError(2,0)
   If Not StringRegExp($sStr, "(?i)\bain'?t\b") Then Return $sStr
   Local $iCount - 0
   $sStr = StringRegExpReplace($sStr, "(?i)\b(he|she|it|that|mine|what|one|" & _
   "here|whom?|someone|something|other)\sain'?t\b", "$1 is not")
   $iCount += @Extended
   $sStr = StringRegExpReplace($sStr, "\bus\sain'?t\b", "we are not")
   $iCount += @Extended
   $sStr = StringRegExpReplace($sStr, "(?i)\b(th)em\sain'?t\b", "$1ose are not")
   $iCount += @Extended
      $sStr = StringRegExpReplace($sStr, "(?i)\b(they|we|you|those|ours|many|others)\b\s\bain'?t\b", "$1 are not")
   $iCount += @Extended
   $sStr = StringRegExpReplace($sStr, "(?i)\b(i)\sain'?t\b", "$1 am not")
   $iCount += @Extended
   SetExtended($iCount)
   Return $sStr
EndFunc   ;<==> _Grammar_AintRight()

;===============================================================================
; Function Name:   _Grammar_RemoveContractions()
; Description:     Remove common English Contractions
; Syntax:          _Grammar_RemoveContractions($sStr[, use shall instead of will])
; Parameter(s):    $sStr - String or file to test
;                  $iShall - If not set to 0 then 'll will he replaced with shall. Default is will.
; Requirement(s):  
; Return Value(s): - Success = Corrected string, @Extended contains the number of changes made
;                  - Failure = Sets @Error to 1 if cancelled from language warning or 2 if $sStr is not valid.
; Author(s):       George (GEOSoft) Gedye
; Modification(s): 
; Note(s):    For English contractions only!!
; Example(s):   
#cs
   $string = "'tis,'twas,he ain't,aint,aren't,can't,could've,couldn't,didn't,doesn't,don't,"
   $string &= "hasn't,he'd,he'll,he's,how'd,how'll,how's,i'd,i'll,"i'm,i've,"
   $string &= "isn't,it's,might've,mightn't,must've,mustn't,shan't,they're,"
   $string &= "they've,wasn't,we'll,we'd,she'd,she'll,she's,should've,"
   $string &= "shouldn't,that'll,that's,there's,they'd,they'll,we're,weren't,what'd,what's,"
   $string &= "when,when'd,when'll,when's,where'd,where'll,where's,who'd,who'll,who's,"
   $string &= "why'd,why'll,why's,won't,would've,wouldn't,you'd,you'll,you're,you've"
   $string = _Grammar_RemoveContractions($string)
   $wrds = @Extended
   $string = StringReplace($string,",", @CRLF)
   If Not @Error Then MsgBox(4096, "Results " & @Extended,"Word count = " & $Wrds & @CRLF & $string)
#ce
;===============================================================================

Func _Grammar_RemoveContractions($sStr, $iShall = 0)
   If StringRight(@OSLang, 2) <> 9 Then
      If MsgBox(262180, "Warning", "Your system language is not English" & @CRLF & @CRLF & _
      "Are you sure you wish to continue") = 7 Then Return SetError(1,0)
   EndIf
   If FileExists($sStr) Then $sStr = FileRead($sStr)
   If NOT $sStr Then Return SetError(2,0)
   Local $iCount = 0
   Local $sShall = "will"
   If $iShall <> 0 Then $sShall = "shall"
   If StringRegExp($sStr, "(?i)\bain'?t\b") Then _Grammar_AintRight($sStr)
   $sStr = StringReplace($sStr, "'tis", "it is")
   $iCount += @Extended
   $sStr = StringRegExpReplace($sStr, "(?i)\b(can)'t\b", "$1 not")
   $iCount += @Extended
   $sStr = StringReplace($sStr, "'twas", "it was")
   $iCount += @Extended
   $sStr = StringRegExpReplace($sStr, "(?i)\b(i)'m\b", "$1 am")
   $iCount += @Extended
   $sStr = StringRegExpReplace($sStr, "(?i)\b([a-z]+)'re\b", "$1 are")
   $iCount += @Extended
   $sStr = StringRegExpReplace($sStr, "(?i)\b(s)han't\b", "$1hall not")
   $iCount += @Extended
   $sStr = StringRegExpReplace($sStr, "(?i)\b([a-z]+)'?ll\b", "$1 "& $sShall)
   $iCount += @Extended
   $sStr = StringRegExpReplace($sStr, "(?i)\b([a-z]+)'d\b", "$1 would")
   $iCount += @Extended
   $sStr = StringRegExpReplace($sStr, "(?i)\b([a-z]+)n'?t\b", "$1 not")
   $iCount += @Extended
   $sStr = StringRegExpReplace($sStr, "(?i)\b([a-z]+)'?ve\b", "$1 have")
   $iCount += @Extended
   $sStr = StringRegExpReplace($sStr, "(?i)\b([a-z]+)'?s\b", "$1 is")
   $iCount += @Extended
   SetExtended($iCount)
   Return $sStr
EndFunc   ;<==> _Grammar_RemoveContractions()