Jump to content

Letter frequency in words loop


Hasher
 Share

Recommended Posts

Hi

Trying to understand arrays better and I have set myself a little task to write an app that studies letter frequency based on position in a word and then write the results into a text file. Below is my effort , It has an error and wont run , but if I put a msg box in front of the error it runs ?? weird to me ??

I am using this word list http://www.mieliestronk.com/corncob_lowercase.zip

Thanks for any help in advance :-)

#include <file.au3>
#include <Array.au3>
local $avArray[41][26] ; bad defining of an array ?
Local $count
$string = "abcdefghijklmnopqrstuvwxyz"
$file = FileOpen("corncob_lowercase.txt",0)
For $i = 1 to 5 step 1 ;58112  <--- all the words in the file
$phrase = FileReadLine($file,$i)    
for $j = 1 to StringLen($phrase)
        $var = StringMid($phrase, $j, 1)
        $result = StringInStr($string, $var)
        Msgbox(0,"Test",@ScriptDir & "\test.txt") ;wont run without msgbox ...wierd??
        $count = $avArray[$j][$result]
        $count =+ 1
        $avArray[$j][$result] = $count
        
    Next 
Next
_FileWriteFromArray(@ScriptDir & "\test.txt",$avArray)
Edited by Hasher

Firefox's secret is the same as Jessica Simpson's: its effortless, glamorous style is the result of — shhh — extensions!

Link to comment
Share on other sites

oki that was basic ;-)

other error is with the array reading to the file

C:\Program Files\AutoIt3\Include\file.au3 (229) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

FileWrite($hFile, $a_Array[$i_Base])

FileWrite($hFile, ^ ERROR

Firefox's secret is the same as Jessica Simpson's: its effortless, glamorous style is the result of — shhh — extensions!

Link to comment
Share on other sites

  • Moderators

1. Right away I noticed you using For 1 and you declared the 2nd delimiter in the array as [26]. You should have an issue there, you need to declare it as 27 as there are 26 letters in the alphabet, I can only assume that you are comparing that to the alphabet though.

2. You have the first delimiter declared at 41, I don't know what that is, but you are using it in the loop as the character count of the string, are you saying that the max character count is 40 of any given string?

3. Can you show with 1 word, what you want the output to look like?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

1) yes I am using the alphabet $string = "abcdefghijklmnopqrstuvwxyz"

2) I set it at 40 under assumption that no word would be longer than that

3)originally I was going to use a ini file to dump info into and format it like this:

[1] ;<----First letter

A= 10

B= 32

C=54 <----the count of each letter

But really I didn't work on this as I could get past the errors further up:-(

Firefox's secret is the same as Jessica Simpson's: its effortless, glamorous style is the result of — shhh — extensions!

Link to comment
Share on other sites

  • Moderators

I'm serioiusly confused on what you are trying to attempt. I understand your new to arrays, so I'll ignore the fact that you are trying to use a single dimension array function (_FileWriteFromArray) with a 2 dimensional array.

Let me ask you this... are you trying to find out how many times (Example only) "a" was used in all the words combined?

Edit:

If I am right on the above... for this big of a text file, it's going to take quite a while to get the result...

#include <array.au3>
$Array = _Count("corncob_lowercase.txt")
_ArrayDisplay($Array, 'A-Z|1-26')

Func _Count($hFile)
    Local $aAlph = StringSplit('abcdefghijklmnopqrstuvwxyz', '')
    Local $aArray[UBound($aAlph)], $sString = FileRead($hFile)
    For $iCC = 1 To UBound($aAlph) - 1
        StringReplace($sString, $aAlph[$iCC], '')
        $aArray[$iCC] = @extended
    Next
    Return $aArray
EndFunc

Result:

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

I am trying to discover the frequency that a letter is used in general language in each position....... 1st, 2nd, 3rd.....etc etc

I now understand that my problem is that _FileWriteFromArray cant handle my array and will attempt to fix it. Now that my loop is fixed I can work on it!!

Firefox's secret is the same as Jessica Simpson's: its effortless, glamorous style is the result of — shhh — extensions!

Link to comment
Share on other sites

  • Moderators

I am trying to discover the frequency that a letter is used in general language in each position....... 1st, 2nd, 3rd.....etc etc

I now understand that my problem is that _FileWriteFromArray cant handle my array and will attempt to fix it. Now that my loop is fixed I can work on it!!

I think I understand... Is this kind of what you are looking for?

$Array = _CharPos("corncob_lowercase.txt")
_FileWriteChar($Array, 'TestOutFile.ini')

Func _CharPos($hFile)
    Local $sString = FileRead($hFile), $sWord, $sSMidChar, $nChar
    Local $aWords = StringSplit(StringStripCR($sString), @LF)
    Local $aPosChar[UBound($aWords)][27], $sChars = 'abcdefghijklmnopqrstuvwxyz'
    For $iCC = 1 To UBound($aWords) - 1;Loop for word
        $sWord = StringStripWS($aWords[$iCC], 8)
        For $xCC = 1 To StringLen($sWord);Loop for characters
            $sSMidChar = StringMid($sWord, $xCC, 1);Individual char
            $nChar = StringInStr($sChars, $sSMidChar);number
            $aPosChar[$xCC][$nChar] += 1;Add to that positions count
        Next
    Next
    Return $aPosChar
EndFunc

Func _FileWriteChar($avArray, $hOutFile)
    If IsArray($avArray) = 0 Then Return SetError(1, 0, 0)
    FileClose(FileOpen($hOutFile, 2));Create out file and or erase previous contents
    Local $sHold, $aAlpha = StringSplit('abcdefghijklmnopqrstuvwxyz', '')
    For $iCC = 1 To UBound($avArray, 1) - 1
        $sHold &= '[Char_Position' & $iCC & ']' & @CRLF
        For $xCC = 1 To 26;Alpha
            If $avArray[$iCC][$xCC] = '' Then $avArray[$iCC][$xCC] = 0
            $sHold &= $aAlpha[$xCC] & ' = ' & $avArray[$iCC][$xCC] & @CRLF
        Next
    Next
    If Not $sHold Then Return SetError(2, 0, 0)
    Return FileWrite($hOutFile, StringTrimRight($sHold, 2))
EndFunc
Creates an Ini as it's out file (Well in the example I'm giving it does).

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

There was an error my logic, in my _CharPos() function... instead of editing and confusing anyone... try the one below:

$Array = _CharPos("corncob_lowercase.txt")
_FileWriteChar($Array, 'TestOutFile.ini')

Func _CharPos($hFile)
    Local $sString = FileRead($hFile), $sWord, $sSMidChar, $nChar
    Local $aWords = StringSplit(StringStripCR($sString), @LF), $nSLen = 0
    Local $aPosChar[1][27], $sChars = 'abcdefghijklmnopqrstuvwxyz'
    For $iCC = 1 To UBound($aWords) - 1;Loop for word
        $sWord = StringStripWS($aWords[$iCC], 8)
        If StringLen($sWord) > $nSLen Then
            $nSLen = StringLen($sWord)
            ReDim $aPosChar[$nSLen + 1][27]
        EndIf
        For $xCC = 1 To $nSLen - ($nSLen - StringLen($sWord))
            $sSMidChar = StringMid($sWord, $xCC, 1);Individual char
            $nChar = StringInStr($sChars, $sSMidChar);number
            $aPosChar[$xCC][$nChar] += 1;Add to that positions count
        Next
    Next
    Return $aPosChar
EndFunc

Func _FileWriteChar($avArray, $hOutFile)
    If IsArray($avArray) = 0 Then Return SetError(1, 0, 0)
    FileClose(FileOpen($hOutFile, 2));Create out file and or erase previous contents
    Local $sHold, $aAlpha = StringSplit('abcdefghijklmnopqrstuvwxyz', '')
    For $iCC = 1 To UBound($avArray, 1) - 1
        $sHold &= '[Char_Position' & $iCC & ']' & @CRLF
        For $xCC = 1 To 26;Alpha
            If $avArray[$iCC][$xCC] = '' Then $avArray[$iCC][$xCC] = 0
            $sHold &= $aAlpha[$xCC] & ' = ' & $avArray[$iCC][$xCC] & @CRLF
        Next
    Next
    If Not $sHold Then Return SetError(2, 0, 0)
    Return FileWrite($hOutFile, StringTrimRight($sHold, 2))
EndFunc

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

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...