Created
October 16, 2017 07:48
-
-
Save zhenghaoz/615d7c24875d05b7db01855f91f9f2a9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import sys, re | |
from pathlib import Path | |
def preprocessor(text): | |
# Remove comments | |
text = re.sub(r'\/\/.+', '', text) | |
text = re.sub(r'\/\*(\*(?!\/)|[^*])*\*\/', '', text) | |
# Remove the package name | |
text = re.sub(r'package\s\w+(\.\w+)+;', '', text) | |
# Remove imports | |
text = re.sub(r'import\s\w+(\.\w+)+;', '', text) | |
# Replace numbers | |
text = re.sub(r'-?\d+(\.\d+)?(e-?\d+)?', '#', text) | |
return text | |
def split(text): | |
text = preprocessor(text) | |
words = re.split(r'[^\w\{\}]', text) | |
return list(filter(None, words)) | |
def count(dir): | |
pathlist = Path(dir).glob('**/*.java') | |
result = [] | |
for path in pathlist: | |
path_in_str = str(path) | |
file = open(path_in_str, 'r') | |
text = file.read() | |
words = split(text) | |
weight = 0 | |
for word in words: | |
if word == '{': | |
weight += 1 | |
elif word == '}': | |
weight -= 1 | |
else: | |
if len(result) <= weight: | |
result.append(0) | |
result[weight] += 1 | |
return result | |
if __name__ == '__main__': | |
if len(sys.argv) == 1: | |
print("Usage: %s [directory]" % (sys.argv[0])) | |
exit(0) | |
print(count(sys.argv[1])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment