Wörter zählen

  • Hi
    ich wollte mal wissen ob man die wörter in einem String Zählen kann.
    Ich habe weder in der Hilfe noch in den Foren eine Lösung gefunden.
    Das muss aber doch gehen oder?

    Jemand ne Idee?

  • Wollte ich erst machen abervorm ersten und nach dem letzten wort ist möglicherweise kein leerzeichen

    -Gedankenblitz
    Aber ich könnte ja mir Stringright und left gucken obe da ein Leerzeichen ist und dann 1 wort dazu rechnen.

    Aber was wenn mehrere Leerzeichen oder Tabs zwischen den Wörtern sind?

  • So danke für die Hilfe ich habs geschafft

    Spoiler anzeigen
    [autoit]


    $string = "Ergebnis: drei Worte"

    [/autoit] [autoit][/autoit] [autoit]

    $asplit = StringSplit($string, " ")
    $icount = $asplit[0]

    [/autoit] [autoit][/autoit] [autoit]

    If StringRight($string, 1) = " " Then
    $icount -= 1
    EndIf
    If StringLeft($string, 1) = " " Then
    $icount -= 1
    EndIf

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    If StringInStr($string, " ") Then
    $strimstring = $string
    While StringInStr($strimstring, " ") <> 0
    If StringInStr($strimstring, " ") Then
    $icount -= 1
    $strimstring = StringTrimLeft($strimstring, StringInStr($strimstring, " "))
    EndIf
    WEnd
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    MsgBox(0,"",$icount)

    [/autoit] [autoit][/autoit] [autoit][/autoit]

    Das sollte so funktoniern wenn Fehler auftreten bitte melden bei Gelegenheit mache ich ne UDF draus.

    • Offizieller Beitrag

    Muß nicht gleich 'ne UDF werden ;)
    Mit RegEx ist es eine Codezeile:

    [autoit]

    $str = 'Das ist ein Wort und auch das ist eins.'
    MsgBox(0, 'Wortzahl', 'Der String enthält: ' & UBound(StringRegExp($str, '[^\s]+', 3)) & ' Worte.')

    [/autoit]


    Man könnte hier noch verfeinern und definieren, was genau ein Wort ist (2 oder 3 Buchstaben min).
    In der momentanen Abfrage werden auch alleinstehende Ziffern, Zeichen als Wort erfaßt.

  • Und jetzt die PREISFRAGE: "Wie oft kommt das Wort ein" in dem Satz vor?" und das mit mit einem Script nachgewiesen.

    ;) Morgens halb sieben in Deutschland. Okay 6:44 Uhr.

    LG,
    Crazy-A.

    Lieben Gruß,
    Alina

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Geheime Information: ;)
    k3mrwmIBHejryPvylQSFieDF5f3VOnk6iLAVBGVhKQegrFuWr3iraNIblLweSW4WgqI0SrRbS7U5jI3sn50R4a15Cthu1bEr

    • Offizieller Beitrag
    [autoit]

    Global $s = "Hello World, I will count letters or Numbers like 1,2,3. And so on. World 111!"

    [/autoit][autoit][/autoit][autoit]

    MsgBox(0, "", _stringCount($s, "World"))
    MsgBox(0, "", _stringCount($s, "1"))
    MsgBox(0, "", _stringCount($s, "num", 1)) ; Case-insensitivity flag.

    [/autoit][autoit][/autoit][autoit]

    Func _stringCount($string, $toFind, $case = "(?-i)", $startPosition = 0)
    If $case = 1 Then $case = "(?i)"
    Local $count = StringRegExp($string, $case & $toFind, 3, $startPosition)
    Switch @error
    Case 0
    If IsArray($count) Then Return UBound($count)
    Case 1
    If Not IsArray($count) Then Return 0
    Case 2
    Return -1
    EndSwitch
    EndFunc ;==>_stringCount

    [/autoit]

    mit Häufigkeit

    Spoiler anzeigen
    [autoit]

    #include<Array.au3>
    Global $str1 = '23, 129,45,67,23,768,45,878,34,67,34 / 34,45,23,57,34,789,46,78,345,768,89,34 / 12'
    MsgBox(0, "Am Häufgisten oder bei Gleichheit am Größten ist :", _occurrence($str1))

    [/autoit] [autoit][/autoit] [autoit]

    Func _occurrence($sString)
    Local $splitted = StringRegExp(StringStripWS($sString, 8), '\d+', 3)
    Local $result[UBound($splitted)][2]
    For $i = 0 To UBound($splitted) - 1
    StringReplace($str1, $splitted[$i], '')
    $result[$i][1] = @extended
    $result[$i][0] = Number($splitted[$i])
    Next
    If $result[0][1] = 1 Then Return _ArrayMax($splitted, 1, 0)
    _ArraySort($result, 1, 0, 0, 1); 1, 0, 0, 2, 1)
    _ArrayDblDel($result)
    _ArrayDisplay($result)
    Return $result[0][0]
    EndFunc ;==>_occurrence

    [/autoit] [autoit][/autoit] [autoit]

    ;----------------------------------------------------------------------------------------------------------------------
    ; Function _ArrayDblDel(ByRef $ARRAY [, $CASESENS=0])
    ;
    ; Description - From an 1D/2D Array will delete double entries (2D -> combination by '[n][0]' and '[n][1]').
    ; - Autodetection 1D/2D Array
    ; - By using string, you can choose case sensitivity.
    ;
    ; Parameter $ARRAY: Array to sort
    ; optional $CASESENS: Case sensitivity off[0] or on[1] (default 0)
    ;
    ; Return Success -1 ByRef Array without doubles
    ; Failure 0 @error = 1, given Array is not Array
    ;
    ; Requirements Only 2 occurences in the second dimension
    ; #include <array.au3>
    ;
    ; Author BugFix ([email='bugfix@autoit.de'][/email])
    ;----------------------------------------------------------------------------------------------------------------------

    [/autoit] [autoit][/autoit] [autoit]

    Func _ArrayDblDel(ByRef $ARRAY, $CASESENS = 0)
    If (Not IsArray($ARRAY)) Then
    SetError(1)
    Return 0
    EndIf
    Local $iDIM, $arTmp1D[1], $arTmp2D[1][2], $dbl = 0
    UBound($ARRAY, 2)
    If @error = 2 Then
    $iDIM = 1
    Else
    $iDIM = 2
    EndIf
    $arTmp1D[0] = ""
    $arTmp2D[0][0] = ""
    Switch $iDIM
    Case 1 ; 1D
    For $i = 0 To UBound($ARRAY) - 1
    $dbl = 0
    For $k = 0 To UBound($arTmp1D) - 1
    Switch $CASESENS
    Case 0
    If $arTmp1D[$k] = $ARRAY[$i] Then $dbl = 1
    Case 1
    If $arTmp1D[$k] == $ARRAY[$i] Then $dbl = 1
    EndSwitch
    Next
    If $dbl = 0 Then
    If $arTmp1D[0] = "" Then
    $arTmp1D[0] = $ARRAY[$i]
    Else
    _ArrayAdd($arTmp1D, $ARRAY[$i])
    EndIf
    Else
    $dbl = 0
    EndIf
    Next
    $ARRAY = $arTmp1D
    Case 2 ; 2D
    For $i = 0 To UBound($ARRAY) - 1
    $dbl = 0
    For $k = 0 To UBound($arTmp2D) - 1
    Switch $CASESENS
    Case 0
    If ($arTmp2D[$k][0] = $ARRAY[$i][0]) And _
    ($arTmp2D[$k][1] = $ARRAY[$i][1]) Then $dbl = 1
    Case 1
    If ($arTmp2D[$k][0] == $ARRAY[$i][0]) And _
    ($arTmp2D[$k][1] == $ARRAY[$i][1]) Then $dbl = 1
    EndSwitch
    Next
    If $dbl = 0 Then
    If $arTmp2D[0][0] = "" Then
    $arTmp2D[0][0] = $ARRAY[$i][0]
    $arTmp2D[0][1] = $ARRAY[$i][1]
    Else
    ReDim $arTmp2D[UBound($arTmp2D) + 1][2]
    $arTmp2D[UBound($arTmp2D) - 1][0] = $ARRAY[$i][0]
    $arTmp2D[UBound($arTmp2D) - 1][1] = $ARRAY[$i][1]
    EndIf
    Else
    $dbl = 0
    EndIf
    Next
    $ARRAY = $arTmp2D
    EndSwitch
    Return -1
    EndFunc ;==>_ArrayDblDel

    [/autoit]

    Mega