Jump to content

_StringProper and Apostrophes


Recommended Posts

I have been using the function to capitalise lots of strings and have the issue where a word has an apostrophe to mark possession or contract words. I only noticed recently after browsing said files that the letter following the  '  was uppercase i.e.  Autoit'S.

I don't know if there is a reason for this, maybe for different languages or just an oversight but the function can be altered to stop this . I don't know which would be more encompassing using Asc($sChr) <> 39 or  $sChr <> "'" but went with the Asc

Func _StringProperNew($sString)
    Local $bCapNext = True, $sChr = "", $sReturn = ""
    Local $sPattern = '[a-zA-ZÀ-ÿšœžŸ]' ; Pattern for letter characters

    For $i = 1 To StringLen($sString)
        $sChr = StringMid($sString, $i, 1)
        Select
            Case $bCapNext = True
                If StringRegExp($sChr, $sPattern) Then
                    $sChr = StringUpper($sChr)
                    $bCapNext = False
                EndIf
            Case Not StringRegExp($sChr, $sPattern)
                ; If $sChr <> "'" Then
                If Asc($sChr) <> 39 Then $bCapNext = True ; U+0027 is 39 in ASCII
            Case Else
                $sChr = StringLower($sChr)
        EndSelect
        $sReturn &= $sChr
    Next

    Return $sReturn
EndFunc   ;==>_StringProper

Does anyone have any insight if this is by design and I missed something?

Link to comment
Share on other sites

12 minutes ago, benners said:

I have been using the function to capitalise lots of strings and have the issue where a word has an apostrophe to mark possession or contract words. I only noticed recently after browsing said files that the letter following the  '  was uppercase i.e.  Autoit'S.

Same behavior of PROPER function in Excel. I never used this function but it's good to know about this behavior.

When the words fail... music speaks.

Link to comment
Share on other sites

Proper is a very (VERY) complex function is you insist at making it solid. In fact, almost impossible to code correctly. This page only scratches the surface of the issues (read to the end)!

https://en.wikipedia.org/wiki/Apostrophe

Nevertheless one can achieve a mostly satisfying result efficiently in many use cases this way:

Local $a = [ _
    "Greengrocers’ apostrophes", _
    "St James’s Park", _
    "ladies’ hats", _
    "Le Cléac'h", _
    "O'Malley", _
    "80's music", _
    "Ho, mia kor’! Post longa laborado", _
    "John's shoes", _
    "doesn't mean anything ƨƳƭƫƼ'Ɖƃ sorry it's my fault", _
    "Διεθνής εβδομάδα χειμερ’ινών αγώνων", _
    "Тиждень зимо՚вих видів спорту (насправді 11 днів)", _
    " ഇതിൽ അഞ്ചു വളയങ്ങൾ'ആലേഖനം ", _
    "μζΣΨϑ'ʤʞʫʀ" _
]

For $i = 0 To UBound($a) - 1
    $a[$i] = _String_Proper($a[$i])
Next

_ArrayDisplay($a)

Func _String_Proper($s)
    Local Static $sPattern = "(*UCP)\b((?<!['՚‘’“”ʼʾ׳״])[[:lower:]])"
    Return Execute('"' & StringRegExpReplace(StringLower($s), $sPattern, '" & StringUpper("$1") & "') & '"')
EndFunc   ;==>_String_Proper

Depending on the script (language) of the input text, you may have to adapt the set of apostrophe-like Unicode signs. Yet never expect a perfect universal result using such a simple approach. Even in "simple" english, O'Malley gives O'malley, so even there things are not that simple.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

@jchd

 

#include <Array.au3>
#include <String.au3>

Local $a = [ _
    "Greengrocers’ apostrophes", _
    "St James’S Park", _
    "ladies’ hats", _
    "Le Cléac'H", _
    "O'Malley", _
    "80's music", _
    "Ho, mia kor’! Post longa laborado", _
    "John's shoes", _
    "doesn'T mean anything ƨƳƭƫƼ'Ɖƃ sorry it's my fault", _
    "Διεθνής εβδομάδα χειμερ’ινών αγώνων", _
    "Тиждень зимо՚вих видів спорту (насправді 11 днів)", _
    " ഇതിൽ അഞ്ചു വളയങ്ങൾ'ആലേഖനം ", _
    "μζΣΨϑ'ʤʞʫʀ" _
]

Local $hTimer = TimerInit()
For $i = 0 To UBound($a) - 1
    $a[$i] = _StringProper_jpm($a[$i])
Next
Local $iDiff = TimerDiff($hTimer)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $iDiff = ' & $iDiff & @CRLF & '>Error code: ' & @error & '    Extended code: ' & @extended & ' (0x' & Hex(@extended) & ')' & @CRLF) ;### Debug Console

_ArrayDisplay($a)

Func _StringProper_jchd($s)
    Local Static $sPattern = "(*UCP)\b((?<!['՚‘’“”ʼʾ׳״])[[:lower:]])"
    Return Execute('"' & StringRegExpReplace(StringLower($s), $sPattern, '" & StringUpper("$1") & "') & '"')
EndFunc   ;==>_StringProper_jchd

Func _StringProper_jpm($sString)
    Local $bCapNext = True, $sChr = "", $sReturn = ""
    Local Static $sPattern = '[a-zA-ZÀ-ÿšœžŸ]'
    Local $iStrLen = StringLen($sString)
    For $i = 1 To $iStrLen
        $sChr = StringMid($sString, $i, 1)
        Select
            Case $bCapNext = True
                If StringRegExp($sChr, $sPattern) Then
                    If $i <> $iStrLen And StringMid($sString, $i + 1, 1) <> " " Then
                        $sChr = StringUpper($sChr)
                    Else
                        $sChr = StringLower($sChr)
                    EndIf
                    $bCapNext = False
                EndIf
            Case Not StringRegExp($sChr, $sPattern)
                $bCapNext = True
            Case Else
                $sChr = StringLower($sChr)
        EndSelect
        $sReturn &= $sChr
    Next
    Return $sReturn
EndFunc   ;==>_StringProper_jpm

Unless you have a regexp for O'Malley, What do you think about a slight mod of the current implementation of _StringProper()?

Edited by jpm
Link to comment
Share on other sites

Well, depending on use context it kind of works, but still very far from universal.

Local $a = [ _
    "Qu'est-ce qu'il fait chaud !", _
    "Le skipper c'est armel le cléac'h.", _
    "LE CléaC'H", _
    "O'Malley", _
    "autoit.exe", _
    "Διεθνής εβδομάδα χειμερ’Δινών αγώνων", _
    "Тиждень зимо՚Твих видів спорту (насправді 11 днів)", _
    "μζΣΨϑ'Σʤʞʫʀ" _
]

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Names like O'Something can be dealt with using an alternation I guess. Not too much free time right now

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...