Sign in to follow this  
Followers 0
laroald

"ent" Is A Msdosprogram For Counting Characters, But..

8 posts in this topic

Hello!

At a danish forum someone asked for help to find a program to count the frequence of all the characters in a text. To get a statistics over how many a,b,c,d... Perhaps sorted. What about presented in a simple graph? Here are the forum; http://www.eksperten.dk/spm/703397

http://www.fourmilab.ch/random/ is the link to the MSDOS program Ent.exe

A nice and simple program - but it's MSDOS

If you're in MSDOS at the respective folder and use the command >ent -c Tell.txt (and have placed some text in a file called Tell.text in the same folder) you'll get at list like this:

1 a 20

2 b 32

3 c 8

4 d 14

..

Still in dos-mode.

Does any of you have any proposal for script that can link this process to the clipboard.

a) Open a textfile

:think: Save the content from clipboard in that textfile

c) Start Ent.exe

d) Send the command >ent -c [textfile]

e) Grab the output from the MSDOS-program

f) Present the result in a sorted graph

g) Delete the content in the textfile

h) Shut down ent.exe

This could be a useful process/program for all who is interested in translations ++

Or will it be more easy to make the whole by AutoIt directly?


Lars RoanBergen, Norway

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

You could always use StringSplit()

$count = 0
For $i = Asc('a') To Asc('z')
    $CharCount = _CountChars(@DeskTopDir & '\Au3Test.txt', Chr($i), 1)
    If $CharCount Then
        $count = $count + 1
        ConsoleWrite($count & '. [' & Chr($i) & '] ' & $CharCount &  @LF)
    EndIf
Next

For $x = Asc('A') To Asc('Z')
    $CharCount = _CountChars(@DeskTopDir & '\Au3Test.txt', Chr($x), 1)
    If $CharCount Then 
        $count = $count + 1
        ConsoleWrite($count & '. [' & Chr($x) & '] ' & $CharCount &  @LF)
    EndIf
Next    

Func _CountChars($h_File, $v_Char, $i_CaseSensitive = 0); 1 = Case Sensitive
    If Not FileExists($h_File) Then
        SetError(1)
        Return 0
    EndIf
    
    If $i_CaseSensitive = 0 Then
        $a_Split = StringSplit(StringLower(FileRead($h_File, FileGetSize($h_File))), $v_Char)       
    ElseIf $i_CaseSensitive = 1 Then
        $a_Split = StringSplit(FileRead($h_File, FileGetSize($h_File)), $v_Char)
    Else
        SetError(2)
        Return 0
    EndIf
    Return UBound($a_Split) - 2
EndFunc

Edit:

Made it more like your example.

Edited by SmOke_N

[center]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.[/center]

Share this post


Link to post
Share on other sites

Thanks SmOke_N

By StringSplit() - it funks. In this way it's not neccessary to go by an external program.

Now I've got two further challenges. First to grab the content from the clipboard instead of a textfile.

And then present the result by a simple (and sorted graph) - instead of using ConsoleWrite.

The plan is not to grab it by a texteditor, but rather make a simple graph to popup.

I didn't succeed to use Array and accumulate the result instead of ConsoleWrite.

;I putted this at the top:

#include <Array.au3>

Dim $avArray

$avArray = _ArrayCreate("Count", "Char", "Number")

;and then

_ArrayAdd($avArray, $count,'. [' & Chr($i) & '] ' )

;instead of your ConsoleWrite-lines

;but didn't succeed - no multidimensional arrays.

What's the alternative? One array for each new count?

And then how to present it finally in a simple graph?


Lars RoanBergen, Norway

Share this post


Link to post
Share on other sites

Thanks SmOke_N

By StringSplit() - it funks. In this way it's not neccessary to go by an external program.

Now I've got two further challenges. First to grab the content from the clipboard instead of a textfile.

And then present the result by a simple (and sorted graph) - instead of using ConsoleWrite.

The plan is not to grab it by a texteditor, but rather make a simple graph to popup.

And then how to present it finally in a simple graph?

This will read it from your clipboard and write it out to a .csv file that you could open up in a spreadsheet program like Excel and get a graph.. I think that's the best you're going to do short of looking at the excelcom stuff which is still going to use/require excel. The Excelcom Thread

$count = 0
For $i = Asc('a') To Asc('z')
    $CharCount = _CountChars(Chr($i), 1)
    If $CharCount Then
        $count = $count + 1
        FileWriteline ( "LetterCount.csv", Chr($i) & ',' & $CharCount )
    EndIf
Next

For $x = Asc('A') To Asc('Z')
    $CharCount = _CountChars(Chr($x), 1)
    If $CharCount Then
        $count = $count + 1
        FileWriteline ( "LetterCount.csv", Chr($x) & ',' & $CharCount )
    EndIf
Next    

Func _CountChars($v_Char, $i_CaseSensitive = 0); 1 = Case Sensitive
    If ClipGet () = "" Then
        SetError(1)
        Return 0
    EndIf
    
    If $i_CaseSensitive = 0 Then
        $a_Split = StringSplit(StringLower(ClipGet()), $v_Char)     
    ElseIf $i_CaseSensitive = 1 Then
        $a_Split = StringSplit(ClipGet(), $v_Char)
    Else
        SetError(2)
        Return 0
    EndIf
    Return UBound($a_Split) - 2
EndFunc

Share this post


Link to post
Share on other sites

Thanks exodius!

It funk's. Now you have succeeded to make a script that grab the content from the clipboard and count the number of characters. But what's the best way to present the result? Just imagine that you're sitting in front of a texteditor and want to know how many of a, b, c....

You copy, activate the script, and then..?

Now the output in Excel looks like this:

a, 12

b, 9

c, 1

d, 4

e, 14

..

The graph is not "a must". Will it be better with a messagebox? Or another alternative?

What about two coloums. The first alphabetical and the second sorted?

a, 12 ...... e -14

b, 9 ....... a -12

c, 1 ........ b - 9

d 4 ........ d - 4

e 14 ........ c - 1

...

If we go by Excel is it then possible to open Excel and present the data by a graph directly?

Don't you think the most rapid way is to make an output by autoit?


Lars RoanBergen, Norway

Share this post


Link to post
Share on other sites

What I'm locking for is to present the result in this way;

#include <GUIConstants.au3>

GUICreate("Frequenze of Labels", 200, 600)

GUICtrlCreateLabel("To list up the characters and bars:", 20, 10)

GuiCtrlCreateLabel("a - 39", 15, 35, 40, 15)

GuiCtrlSetBkColor(-1, 0x00FF00)

GuiCtrlCreateLabel("b - 62", 15, 55, 55, 15)

GuiCtrlSetBkColor(-1, 0x00FF00)

GUICtrlCreateLabel("Sorted would be optimal", 20, 130)

GUISetState (@SW_SHOW)

While 1

$msg = GUIGetMsg()

If $msg = $GUI_EVENT_CLOSE Then ExitLoop

Wend

How to bring over and present the result of the counting to this GUI?


Lars RoanBergen, Norway

Share this post


Link to post
Share on other sites

This will do what you're wanting.. I don't know that I like it because it's pretty cumbersome and since the capital and lower case letters are differents sizes it throws off the look of the bar... but since you want it, here it is.

Dim $array
_FileReadToArray ( "LetterCount.csv", $array )

$WindowHeight = $array[0]-1
$WindowHeight = $WindowHeight * 20 + 40

#include <GUIConstants.au3>
GUICreate("Frequency of Labels", 200, $WindowHeight)
GUICtrlCreateLabel("To list up the characters and bars:", 10, 5)

For $x = 1 To $array[0]-1
    $split = StringSplit ( $array[$x], "," )
    $split2 = StringStripCR ( $split[2] )
    $split2 = StringStripWS ( $split2, 8 )
    GuiCtrlCreateLabel($split[1] & " - " & $split2, 15, 20 * $x, $split[2] + 50, 15)
    GuiCtrlSetBkColor(-1, 0x00FF00)
Next

GUISetState (@SW_SHOW)
While 1
$msg = GUIGetMsg()
If $msg = $GUI_EVENT_CLOSE Then ExitLoop
Wend

Otherwise you should look at the Excelcom UDF and the XLChart11.au3 Example that is included in the Excelcom UDF.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

An alterntive which fix the differents size for capital and lower case letters. By this script you grab the content of the clipboard and present a frequency of letters by a simple graph. Useful for many translators and others..

Two furhter challenges;

-Shouldn't it be rather easy to drop the csv-file? To accumulate the content of the clipboard stright into an array instead of an external file?

-And the graph would be much more informative if we presented the frequence of (a) and (A) at the same line. (A) in direction to the left, and (a) in direction to the right. In this way we could have defined 26 lines for all the characters, and drop the $WindowHeight.

-And how to define the width of the GUI? When I grab just a few words into the clipboard then everything funks well. But with ~1000 char's and further on then there has to be >1 char pr dots.

-What other info can be presented in the same GUI? Which letters has highes freq.?, Could it be smart to sort the freq.?

#include <file.au3>
#include <GUIConstants.au3>

$count = 0
For $i = Asc('a') To Asc('z')
    $CharCount = _CountChars(Chr($i), 1)
    If $CharCount Then
        $count = $count + 1
        FileWriteline ( "LetterCount.csv", Chr($i) & ',' & $CharCount )
    EndIf
Next

For $x = Asc('A') To Asc('Z')
    $CharCount = _CountChars(Chr($x), 1)
    If $CharCount Then
        $count = $count + 1
        FileWriteline ( "LetterCount.csv", Chr($x) & ',' & $CharCount )
    EndIf
Next    

Func _CountChars($v_Char, $i_CaseSensitive = 0); 1 = Case Sensitive
    If ClipGet () = "" Then
        SetError(1)
        Return 0
    EndIf
    
    If $i_CaseSensitive = 0 Then
        $a_Split = StringSplit(StringLower(ClipGet()), $v_Char)     
    ElseIf $i_CaseSensitive = 1 Then
        $a_Split = StringSplit(ClipGet(), $v_Char)
    Else
        SetError(2)
        Return 0
    EndIf
    Return UBound($a_Split) - 2
EndFunc

Dim $array
_FileReadToArray( "LetterCount.csv", $array)

$WindowHeight = $array[0] - 1
$WindowHeight = $WindowHeight * 20 + 40
;Opt("GUICoordMode",0)

GUICreate("Frequency of Letters", 300, $WindowHeight)
;GUICtrlCreateLabel("Characters:", 10, 5)

For $x = 1 To $array[0] - 1
   $split = StringSplit($array[$x], ",")
   $split2 = StringStripCR($split[2])
   $split2 = StringStripWS($split2, 8)
   GUICtrlCreateLabel($split[1], 5, 20 * $x,20, 15)
GUICtrlSetBkColor(-1, 0x00FF00)   
  GUICtrlCreateLabel($split2,   28, 20 * $x,20, 15)
GUICtrlSetBkColor(-1, 0x00FF00)
   GUICtrlCreateLabel("   ", 50, -1, $split[2] *2, 15)
   GUICtrlSetBkColor(-1, 0x8080ff)
Next 

GUISetState(@SW_SHOW)
While 1
   $msg = GUIGetMsg()
   If $msg = $GUI_EVENT_CLOSE Then 
       FileDelete("LetterCount.csv")
           ExitLoop
           EndIf
WEnd
Edited by laroald

Lars RoanBergen, Norway

Share this post


Link to post
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
Sign in to follow this  
Followers 0