Repository navigation
Expand file tree
/
Copy pathindex_metadata.py
More file actions
93 lines (79 loc) · 3.02 KB
/
Copy pathindex_metadata.py
File metadata and controls
93 lines (79 loc) · 3.02 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
import copy
from cmn_name_helper import tokenize_fun_name
class IndexMetadata:
_name_abbr_dict = {}
#
# [
# {
# name: FUNCTION_NAME
# package: 'common.poi.Address'
# tests: '//@:'
# body: '{}'
# type: 'function' #Could be omitted if it's function
# class: 2 #Could be omitted if has no class,
# number refer to the class index in _funMetaList
# memberFunctions: [4, 5, 6] #Could be omitted if it's not class,
# array refers to indices of member functions belong to the class
# },
# { ... }
# ]
#
_fun_meta_list = []
#
# {
# token1: [0, 4, 98] #Array of indics refered to in _fun_meta_list
# token2: [1, 53, 34, 98] #Array of indics refered to in _fun_meta_list
# ...
# }
#
_fun_invert_index = {}
_IF_FUN_NAME_AS_TOKEN = True
def set_name_abbr(self, name_abbr_dict):
self._name_abbr_dict = name_abbr_dict
def set_pkg_codes(self, pkg_dict):
_fun_meta_list = []
_fun_invert_index = {}
self._translate_pkg_codes(copy.deepcopy(pkg_dict))
def get_fun_meta_by_tokens(self, tokens, is_union = False):
meta_indices_lists = []
for token in tokens:
token = token.lower().strip()
if token in self._fun_invert_index:
meta_indices_lists.append(self._fun_invert_index[token])
if len(meta_indices_lists) is 0:
return []
result_metadata = meta_indices_lists[0]
for meta_index_set in meta_indices_lists[1:]:
if is_union:
result_metadata = result_metadata.union(meta_index_set)
else:
result_metadata = result_metadata.intersection(meta_index_set)
return map(lambda index: copy.deepcopy(self._fun_meta_list[index]), sorted(result_metadata))
#
# Private
#
def _translate_pkg_codes(self, pkg_dict):
for pkg_name, fun_dicts in pkg_dict.iteritems():
for fun_name, fun_dict in fun_dicts.iteritems():
self._set_function(fun_name, pkg_name, fun_dict)
def _set_function(self, name, pkg, opt = {}):
name = name.strip()
pkg = pkg.strip()
meta_dict = opt
meta_dict['name'] = name
meta_dict['package'] = pkg
name_tokens = tokenize_fun_name(name)
if self._IF_FUN_NAME_AS_TOKEN:
name_tokens.add(name.lower())
self._fun_meta_list.append(meta_dict)
self._set_fun_invert_index(name_tokens, len(self._fun_meta_list) - 1)
def _set_fun_invert_index(self, name_tokens, meta_list_index):
for token in name_tokens:
for name in self._get_name_and_abbr_set(token):
if name not in self._fun_invert_index:
self._fun_invert_index[name] = set()
self._fun_invert_index[name].add(meta_list_index)
def _get_name_and_abbr_set(self, name):
result_set = {name}
result_set.update(self._name_abbr_dict.get(name, {}))
return result_set