Skip to content

Commit

Permalink
when find the suggested name, use the names in sketch names first. (P…
Browse files Browse the repository at this point in the history
…addlePaddle#4023)

* when find the suggtested name, use the names in sketch names first.

* typo
  • Loading branch information
wadefelix authored Oct 27, 2021
1 parent a3eb368 commit ac1220a
Showing 1 changed file with 65 additions and 43 deletions.
108 changes: 65 additions & 43 deletions docs/api/gen_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,44 +533,6 @@ def collect_referenced_from_infos(docdirs):
docdirs)


def get_shortest_api(api_list):
"""
find the shortest api name (suggested name) in list.
Problems:
1. fuild - if there is any apis don't contain 'fluid' in name, use them.
2. core vs core_avx - using the 'core'.
"""
if len(api_list) == 1:
return api_list[0]
# try to find shortest path of api as the real api
api_info = [
] # {'name': name, 'fluid_in_name': True/False, 'core_avx_in_name': True/Flase', 'len': len}
for api in api_list:
fields = api.split('.')
api_info.append({
'name': api,
'fluid_in_name': 'fluid' in fields,
'core_avx_in_name': 'core_avx' in fields,
'len': len(fields),
})

def shortest(api_info):
if not api_info:
return None
elif len(api_info) == 1:
return api_info[0].get('name')
api_info.sort(key=lambda ele: ele.get('len'))
return api_info[0].get('name')

if not all([api.get('fuild_in_name') for api in api_info]):
api_info = [api for api in api_info if not api.get('fluid_in_name')]
sn = shortest([api for api in api_info if not api.get('core_avx_in_name')])
if sn is None:
sn = shortest(api_info)
return sn


def remove_all_en_files(path="./paddle"):
"""
remove all the existed en doc files
Expand Down Expand Up @@ -822,11 +784,65 @@ def __call__(self):
return self.api_name, self.api_ref_name


def get_shortest_api(api_list):
"""
find the shortest api name (suggested name) in list.
Problems:
1. fuild - if there is any apis don't contain 'fluid' in name, use them.
2. core vs core_avx - using the 'core'.
"""
if len(api_list) == 1:
return api_list[0]
# try to find shortest path of api as the real api
api_info = [
] # {'name': name, 'fluid_in_name': True/False, 'core_avx_in_name': True/Flase', 'len': len}
for api in api_list:
fields = api.split('.')
api_info.append({
'name': api,
'fluid_in_name': 'fluid' in fields,
'core_avx_in_name': 'core_avx' in fields,
'len': len(fields),
})

def shortest(api_info):
if not api_info:
return None
elif len(api_info) == 1:
return api_info[0].get('name')
api_info.sort(key=lambda ele: ele.get('len'))
return api_info[0].get('name')

if not all([api.get('fuild_in_name') for api in api_info]):
api_info = [api for api in api_info if not api.get('fluid_in_name')]
sn = shortest([api for api in api_info if not api.get('core_avx_in_name')])
if sn is None:
sn = shortest(api_info)
return sn


def insert_suggested_names():
"""
add suggested_name field, updte the doc_filename, and sort the all_names.
add suggested_name field, updte the doc_filename, and sort the all_names and api_sketch_names.
"""
pat = re.compile(r'paddle\.fluid\.core_[\w\d]+\.(.*)$')

def sort_name_list(api_names):
"""
sort and move paddle.Tensor.* to the end
"""
names_sorted = sorted(list(api_names))
cnt = 0 # count of paddle.Tensor.*
for n in names_sorted:
if n.startswith('paddle.Tensor.'):
cnt += 1
else:
break
if cnt:
names_sorted = names_sorted[cnt:] + names_sorted[:cnt]
return names_sorted

for id_api in api_info_dict:
if "all_names" not in api_info_dict[id_api]:
api_info_dict[id_api]["all_names"] = set()
Expand All @@ -840,9 +856,15 @@ def insert_suggested_names():
if mo:
api_info_dict[id_api]["all_names"].add('paddle.fluid.core.' +
mo.group(1))
api_info_dict[id_api]["all_names"] = sorted(
list(api_info_dict[id_api]["all_names"]))
sn = get_shortest_api(api_info_dict[id_api]["all_names"])
api_info_dict[id_api]["all_names"] = sort_name_list(
api_info_dict[id_api]["all_names"])
sn = None
if 'api_sketch_names' in api_info_dict[id_api]:
api_info_dict[id_api]['api_sketch_names'] = sort_name_list(
api_info_dict[id_api]['api_sketch_names'])
sn = get_shortest_api(api_info_dict[id_api]['api_sketch_names'])
if not sn:
sn = get_shortest_api(api_info_dict[id_api]["all_names"])
if sn:
# Delete alias_name, api_info_dict[id_api]["alias_name"] = sn
api_info_dict[id_api]["suggested_name"] = sn
Expand Down Expand Up @@ -1239,8 +1261,8 @@ def parse_args():
set_display_attr_of_apis()
set_source_code_attrs()
set_referenced_from_attr()
insert_suggested_names()
set_api_sketch()
insert_suggested_names()
if ('__all__' not in realattrs) or ('__all__' in realattrs and
realattr == '__all__'):
if args.gen_rst:
Expand Down

0 comments on commit ac1220a

Please sign in to comment.