"Character Count" by ZedWonk

Rating: (0 votes)

Report this Script

Name: Character Count
Uploaded By: ZedWonk
Date Created: April 1, 2010 9:10 a.m.
Date Modified: April 1, 2010 9:11 a.m.
Description: Gets a count by character for the current selection (or document if nothing is selected)
Num Views: 522
Num Downloads: 184

Contents

Download this script

###############################################################################
## Character Count v0.1
##   Counts characters in the selected text
##   If there is no selection, the whole document is selected
##   Based on HexDump by NickDMax
##   Counting code from http://www.daniweb.com/code/snippet216639.html
## By: Scott (wischeese/ZedWonk)

import pn
import scintilla
from pypn.decorators import script

def CreateCount(text):
    output = ""
    if text is not None:
      charCount = {}
      for char in text:
        charCount[char] = charCount.get(char, 0) + 1
      sortedCharTuples = sorted(charCount.items())
      for charTuple in sortedCharTuples:
        charshow = charTuple[0]
        charint = ord(charshow)
        if charint <= 32:
          charshow = " "    
        output += "%02X (%s) - %04d \n" % (charint, charshow,  charTuple[1])          
    return output

@script("Character Count", "Analysis")
def CharacterCount():
    """ Character Count wil create a distribution graph of the selected text"""
    doc = pn.CurrentDoc()
    if doc is not None:    
        editor = scintilla.Scintilla(pn.CurrentDoc())
        start = editor.SelectionStart
        end = editor.SelectionEnd
        if (start == end):  
            start = 0
            end = editor.Length 
        text = editor.GetTextRange(start, end)
        newDoc = pn.NewDocument(None)
        newEditor = scintilla.Scintilla(newDoc)
        newEditor.BeginUndoAction()
        count = CreateCount(text)
        newEditor.AppendText(len(count), count)        
        newEditor.EndUndoAction()

Comments: