Advertisement
Guest User

abbreviate.py

a guest
Aug 24th, 2019
1,355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.38 KB | None | 0 0
  1. import re
  2.  
  3. def main():
  4.     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']
  5.     min_len, max_len = 2, 4
  6.  
  7.     #-----------------------------------------#
  8.     #write your username in here to try it out!
  9.     try_it_out = ''
  10.     #-----------------------------------------#
  11.  
  12.     if try_it_out: samples.append(try_it_out)
  13.  
  14.     longest = len(max(samples, key=len))
  15.     print('\n' + '-'*(longest + 3) + '+' + '-'*(max_len + 3))
  16.     for name in samples:
  17.         print('| {:>{}} | {:{}} |'.format(name, longest, abbreviate(name, min_len=min_len, max_len=max_len), max_len))
  18.         print('-'*(longest + 3) + '+' + '-'*(max_len + 3))
  19.  
  20. def abbreviate(name, min_len=2, max_len=4):
  21.     assert((min_len<=max_len) and min_len<=len(name))
  22.     brief, sections, types = '', [], ['lower', 'title', 'digit', 'special']
  23.     distr = {key:0 for key in types}
  24.     pattern = re.compile(r'(?P<{}>[a-z]+)|(?P<{}>[A-Z][a-z]+|[A-Z])|(?P<{}>\d+)|(?P<{}>[-_]+)'.format(*types))
  25.     for match in pattern.finditer(name):
  26.         for key, value in match.groupdict().items():
  27.             if value:
  28.                 sections.append({value:key})
  29.                 distr[key]+=1
  30.                 break
  31.     #print('sections: {}\ndistr: {}'.format(sections, distr))
  32.  
  33.     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
  34.             brief = ''.join([list(sections[i].keys())[0] for i in range(max_len//2)])
  35.  
  36.     elif (list(distr.values()).count(0) == len(types)-1) and not distr['title']: #homogenous name
  37.         brief = name[:max_len] #first few chars of name
  38.  
  39.     elif len(name) <= max_len: #short name
  40.         brief = name
  41.  
  42.     elif len(sections) <= min_len: #not enough sections
  43.         carry, order, proportion, briefs = 0, 1, max_len//sum(distr.values()), []
  44.  
  45.         if len(list(sections[-1].keys())[0]) < proportion: #if the last section is disproportionally short
  46.             order = -1 #reverse the order
  47.  
  48.         for i, section in enumerate(sections[::order]):
  49.             key, value = list(section.items())[0]
  50.             briefs.append(key[:proportion + carry])
  51.             carry = max(0, proportion - len(key))
  52.  
  53.         brief = ''.join(briefs[::order])
  54.  
  55.     elif min_len < len(sections) <= max_len: #right amount of sections
  56.         for i, section in enumerate(sections):
  57.                 key, value = list(section.items())[0]
  58.                 if i == len(sections)-1: #last section
  59.                     if value in ('digit', 'special'):
  60.                         brief += key[-1]
  61.                         break
  62.                 brief += key[0]
  63.  
  64.     elif max_len <= len(sections): #too many sections
  65.         if min_len < (distr['title'] + distr['lower']): #enough alpha
  66.             i = 0
  67.             while (i < len(sections)) and (len(brief) < max_len):
  68.                 key, value = list(sections[i].items())[0]
  69.                 if value in ('lower', 'title'): brief += key[0]
  70.                 i+=1
  71.         else:
  72.             for section in sections[:max_len]:
  73.                 brief += list(section.keys())[0][0]
  74.  
  75.     return brief
  76.  
  77. if __name__=='__main__':
  78.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement