Anzahl von Wörtern zählen

  • Hallo Forum,
    Ich glaube ich stehe gerade ein wenig auf der Leitung.
    Ich habe z.B. so eine Textdatei:

    Text

    wo
    abc
    51
    abc
    abc
    wo
    abc
    51


    und möchte dann wissen wie oft jedes Wort vorgekommen ist.
    Es gibt aber nicht immer nur die gleichen Wörter sondern es kannn auch z.B. "anderes Wort" oder "Forum" dabei sein.

    Bis jetzt habe ich erst das

    $file = FileOpen("datei.txt",0)
    $inhalt_roh = FileRead($file)
    FileClose($file)
    $inhalt_array = StringSplit($inhalt_roh,@LF)

    For $i = 1 To $inhalt_array[0]

    Next

    • Offizieller Beitrag
    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]
  • Hi,
    Danke für die Antwort.
    Aber irgendwie funktioniert das nur mit zahlen.
    Bei Buchstaben kommt das: (Bild)
    Ich hab das jetzt so:

    Spoiler anzeigen
    [autoit]

    #include <Array.au3>
    $file = FileOpen("text.txt",0)
    $inhalt_roh = FileRead($file)
    FileClose($file)
    ;~ $inhalt_array = StringSplit($inhalt_roh,@LF)
    Global $str1 = StringReplace($inhalt_roh,@LF,",")

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

    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]