Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. chosungs = [u'ㄱ', u'ㄲ', u'ㄴ', u'ㄷ', u'ㄸ', u'ㄹ', u'ㅁ', u'ㅂ', u'ㅃ',
  5. u'ㅅ', u'ㅆ', u'ㅇ', u'ㅈ', u'ㅉ', u'ㅊ', u'ㅋ', u'ㅌ', u'ㅍ',
  6. u'ㅎ']
  7. jungsungs = [u'ㅏ', u'ㅐ', u'ㅑ', u'ㅒ', u'ㅓ', u'ㅔ', u'ㅕ', u'ㅖ', u'ㅗ',
  8. u'ㅘ', u'ㅙ', u'ㅚ', u'ㅛ', u'ㅜ', u'ㅝ', u'ㅞ', u'ㅟ', u'ㅠ',
  9. u'ㅡ', u'ㅢ', u'ㅣ']
  10. jongsungs = [u'', u'ㄱ', u'ㄲ', u'ㄳ', u'ㄴ', u'ㄵ', u'ㄶ', u'ㄷ', u'ㄹ',
  11. u'ㄺ', u'ㄻ', u'ㄼ', u'ㄽ', u'ㄾ', u'ㄿ', u'ㅀ', u'ㅁ', u'ㅂ',
  12. u'ㅄ', u'ㅅ', u'ㅆ', u'ㅇ', u'ㅈ', u'ㅊ', u'ㅋ', u'ㅌ', u'ㅍ',
  13. u'ㅎ']
  14.  
  15. CHO_LEN, JUNG_LEN, JONG_LEN = len(chosungs), len(jungsungs), len(jongsungs)
  16. HAN_FST, HAN_LST = ord(u'가'), ord(u'힣')
  17. JAUM_FST, JAUM_LST = ord(u'ㄱ'), ord(u'ㅎ')
  18.  
  19.  
  20. def cho_jung_jong(han_char):
  21. code_point = ord(han_char)
  22. if HAN_FST <= code_point <= HAN_LST:
  23. code_diff = code_point - HAN_FST
  24. cho_idx = (code_diff / (JONG_LEN * JUNG_LEN)) % CHO_LEN
  25. jung_idx = (code_diff / JONG_LEN) % JUNG_LEN
  26. jong_idx = code_diff % JONG_LEN
  27. return (chosungs[cho_idx], jungsungs[jung_idx], jongsungs[jong_idx])
  28. elif JAUM_FST <= code_point <= JAUM_LST:
  29. cho_idx = code_point - JAUM_FST
  30. return (chosungs[cho_idx], '', '')
  31. else:
  32. return ('', '', '')
  33.  
  34.  
  35. def parse_chosung(han_str):
  36. return ''.join([cho_jung_jong(i)[0] for i in han_str])
  37.  
  38.  
  39. def compare_chosung(i, target):
  40. i, target = i.replace(' ', ''), target.replace(' ', '')
  41. return (i in target) or (i in parse_chosung(target))
  42.  
  43.  
  44. def main():
  45. contacts = [i.decode('utf-8') for i in open('contacts.txt').readlines()]
  46. print ''.join([i for i in contacts])
  47. my_input = unicode(raw_input('search: '), 'utf-8')
  48. matches = [nm for nm in contacts if compare_chosung(my_input, nm)]
  49. print ', '.join(matches)
  50.  
  51. if __name__ == '__main__':
  52. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement