Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- def main():
- samples = ['ZR0-Gravity7-pyBoy', 'zr0gravity7', '6takeshi', '_69_420_', 'str8up', 'theExiled', 'Exiled', 'Life_Is_Strange', 'MyNameIsBifGames', 'MindHeadDumb', 'GTXBEEF', 'noupper', 'ResQRanger_-', 'balista_2234', 'impala_89', 'marzbar_', 'cats7', 'CarBCdMagicLCDoor']
- min_len, max_len = 2, 4
- #-----------------------------------------#
- #write your username in here to try it out!
- try_it_out = ''
- #-----------------------------------------#
- if try_it_out: samples.append(try_it_out)
- longest = len(max(samples, key=len))
- print('\n' + '-'*(longest + 3) + '+' + '-'*(max_len + 3))
- for name in samples:
- print('| {:>{}} | {:{}} |'.format(name, longest, abbreviate(name, min_len=min_len, max_len=max_len), max_len))
- print('-'*(longest + 3) + '+' + '-'*(max_len + 3))
- def abbreviate(name, min_len=2, max_len=4):
- assert((min_len<=max_len) and min_len<=len(name))
- brief, sections, types = '', [], ['lower', 'title', 'digit', 'special']
- distr = {key:0 for key in types}
- pattern = re.compile(r'(?P<{}>[a-z]+)|(?P<{}>[A-Z][a-z]+|[A-Z])|(?P<{}>\d+)|(?P<{}>[-_]+)'.format(*types))
- for match in pattern.finditer(name):
- for key, value in match.groupdict().items():
- if value:
- sections.append({value:key})
- distr[key]+=1
- break
- #print('sections: {}\ndistr: {}'.format(sections, distr))
- if ((max_len//2) <= len(sections) <= max_len) and (sum([len(list(sections[i].keys())[0]) for i in range(max_len//2)]) == max_len): #the first few sections' length are equal to the max_len
- brief = ''.join([list(sections[i].keys())[0] for i in range(max_len//2)])
- elif (list(distr.values()).count(0) == len(types)-1) and not distr['title']: #homogenous name
- brief = name[:max_len] #first few chars of name
- elif len(name) <= max_len: #short name
- brief = name
- elif len(sections) <= min_len: #not enough sections
- carry, order, proportion, briefs = 0, 1, max_len//sum(distr.values()), []
- if len(list(sections[-1].keys())[0]) < proportion: #if the last section is disproportionally short
- order = -1 #reverse the order
- for i, section in enumerate(sections[::order]):
- key, value = list(section.items())[0]
- briefs.append(key[:proportion + carry])
- carry = max(0, proportion - len(key))
- brief = ''.join(briefs[::order])
- elif min_len < len(sections) <= max_len: #right amount of sections
- for i, section in enumerate(sections):
- key, value = list(section.items())[0]
- if i == len(sections)-1: #last section
- if value in ('digit', 'special'):
- brief += key[-1]
- break
- brief += key[0]
- elif max_len <= len(sections): #too many sections
- if min_len < (distr['title'] + distr['lower']): #enough alpha
- i = 0
- while (i < len(sections)) and (len(brief) < max_len):
- key, value = list(sections[i].items())[0]
- if value in ('lower', 'title'): brief += key[0]
- i+=1
- else:
- for section in sections[:max_len]:
- brief += list(section.keys())[0][0]
- return brief
- if __name__=='__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement