Created
April 14, 2011 09:11
-
-
Save gpiancastelli/919161 to your computer and use it in GitHub Desktop.
A simplified keyword counter for Python scripts, written in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileReader; | |
import java.io.FilenameFilter; | |
import java.io.IOException; | |
import java.util.HashMap; | |
import java.util.HashSet; | |
import java.util.Map; | |
import java.util.Set; | |
public class KeywordCounter { | |
static final String PATH = "/path/to/python/scripts"; | |
static final Set<String> KEYWORDS = new HashSet<String>(); | |
static { | |
KEYWORDS.add("and"); | |
KEYWORDS.add("as"); | |
KEYWORDS.add("assert"); | |
KEYWORDS.add("break"); | |
KEYWORDS.add("class"); | |
KEYWORDS.add("continue"); | |
KEYWORDS.add("def"); | |
KEYWORDS.add("del"); | |
KEYWORDS.add("elif"); | |
KEYWORDS.add("else"); | |
KEYWORDS.add("except"); | |
KEYWORDS.add("exec"); | |
KEYWORDS.add("finally"); | |
KEYWORDS.add("for"); | |
KEYWORDS.add("from"); | |
KEYWORDS.add("global"); | |
KEYWORDS.add("if"); | |
KEYWORDS.add("import"); | |
KEYWORDS.add("in"); | |
KEYWORDS.add("is"); | |
KEYWORDS.add("lambda"); | |
KEYWORDS.add("not"); | |
KEYWORDS.add("or"); | |
KEYWORDS.add("pass"); | |
KEYWORDS.add("print"); | |
KEYWORDS.add("raise"); | |
KEYWORDS.add("return"); | |
KEYWORDS.add("try"); | |
KEYWORDS.add("while"); | |
KEYWORDS.add("with"); | |
KEYWORDS.add("yield"); | |
} | |
public static void main(String[] args) throws Exception { | |
File scriptDirectory = new File(PATH); | |
String[] scriptNames = scriptDirectory.list(new FilenameFilter() { | |
public boolean accept(File dir, String name) { | |
return name.endsWith(".py"); | |
} | |
}); | |
// collect results | |
Map<Script, Integer> result = new HashMap<Script, Integer>(); | |
for (String name : scriptNames) { | |
Script script = new Script(PATH + File.separator + name); | |
result.put(script, script.countKeywords()); | |
} | |
// print results | |
for (Map.Entry<Script, Integer> script : result.entrySet()) | |
System.out.printf("%s uses %d reserved words\n", | |
script.getKey().getName(), | |
script.getValue()); | |
} | |
} | |
class Script { | |
private File script; | |
public Script(String name) { | |
script = new File(name); | |
} | |
public int countKeywords() throws IOException { | |
int nk = 0; | |
BufferedReader reader = null; | |
try { | |
reader = new BufferedReader(new FileReader(script)); | |
String line = ""; | |
while ((line = reader.readLine()) != null) | |
for (String token : line.split("\\s+")) | |
if (KeywordCounter.KEYWORDS.contains(token.split(":")[0])) | |
nk++; | |
} finally { | |
if (reader != null) | |
reader.close(); | |
} | |
return nk; | |
} | |
public String getName() { | |
return script.getName(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment