forked from alinamatyukhina/Attribution_Real_World
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhalstead.py
91 lines (63 loc) · 1.73 KB
/
halstead.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import itertools
import re
import math
from collections import Counter
import glob, os
numbers = re.compile(r'(\d+)')
def numericalSort(value):
parts = numbers.split(value)
parts[1::2] = map(int, parts[1::2])
return parts
for inputFilename in sorted(glob.glob("*.java"),key=numericalSort) :
from collections import Counter
def ngrams(s, n=2, i=0):
while len(s[i:i+n]) == n:
yield s[i:i+n]
i += 1
a=[]
with open(inputFilename,'r', encoding='latin-1') as f2:
for line in f2:
unigram= ngrams(line.split(), n=1)
b=list(unigram)
#print(b)
a.append(b)
#print(a)
flattened = [val for sublist in a for val in sublist]
#print(flattened)
flattened2 = [val for sublist in flattened for val in sublist]
print(flattened2)
print(len(flattened2)) #N1+N2
a = dict(Counter(flattened2))
print(a)
print(len(a)) #n1+n2
# for C
##for inputFilename in sorted(glob.glob("*.c"),key=numericalSort) :
##
##
##
## from collections import Counter
##
## def ngrams(s, n=2, i=0):
## while len(s[i:i+n]) == n:
## yield s[i:i+n]
## i += 1
##
## a=[]
## with open(inputFilename,'r', encoding='latin-1') as f2:
## for line in f2:
## unigram= ngrams(line.split(), n=1)
## b=list(unigram)
## #print(b)
## a.append(b)
## #print(a)
##
## flattened = [val for sublist in a for val in sublist]
## #print(flattened)
##
## flattened2 = [val for sublist in flattened for val in sublist]
## print(flattened2)
## print(len(flattened2)) #N1+N2
##
## a = dict(Counter(flattened2))
## print(a)
## print(len(a)) #n1+n2