relationship.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. offset = 4
  2. relation_first_grade = {
  3. 'f': ['Ur-Ur-Großmutter', 'Ur-Großmutter', 'Großmutter', 'Mutter', 'ich', 'Tochter', 'Enkelin', 'Ur-Enkelin', 'Ur-Ur-Enkelin'],
  4. 'm': ['Ur-Ur-Großvater', 'Ur-Großvater', 'Großvater', 'Vater', 'ich', 'Sohn', 'Enkel', 'Ur-Enkel', 'Ur-Ur-Enkel']
  5. }
  6. relation_higher_grade = {
  7. 'f': ['Ur-Ur-Großtante', 'Ur-Großtante', 'Großtante', 'Tante', 'Cousine', 'Nichte', 'Großnichte', 'Ur-Großnichte', 'Ur-Ur-Großnichte'],
  8. 'm': ['Ur-Ur-Großonkel', 'Ur-Großonkel', 'Großonkel', 'Onkel', 'Cousin', 'Neffe', 'Großneffe', 'Ur-Großneffe', 'Ur-Ur-Großneffe']
  9. }
  10. def relationship_name(subject, relative, gender):
  11. if gender != 'm':
  12. gender = 'f'
  13. s = subject.split('.')
  14. r = relative.split('.')
  15. delta = len(r) - len(s)
  16. if subject.startswith(relative) or relative.startswith(subject):
  17. return relation_first_grade[gender][delta + offset]
  18. common = relationship_grade(s, r)
  19. min_grade = min(len(s), len(r))
  20. grade = min_grade - common
  21. if delta == 0:
  22. grade = grade - 1
  23. if grade == 0:
  24. return 'Bruder' if (gender == 'm') else 'Schwester'
  25. if grade == 1:
  26. return relation_higher_grade[gender][delta + offset]
  27. return relation_higher_grade[gender][delta + offset] + f' {grade}. Grades'
  28. def relationship_grade(s, r):
  29. min_grade = min(len(s), len(r))
  30. for i in range(min_grade):
  31. if s[i] != r[i]:
  32. return i
  33. return min_grade
  34. def relationship_distance(subject, relative):
  35. s = subject.split('.')
  36. r = relative.split('.')
  37. common = relationship_grade(s, r)
  38. grade = len(s) + len(r) - (2 * common)
  39. return grade
  40. def relationship_list(family_list, rel_index):
  41. subject = family_list[rel_index]
  42. result = []
  43. for person in family_list:
  44. rel_name = relationship_name(subject[1], person[1], person[2])
  45. rel_name2 = relationship_name(person[1], subject[1], subject[2])
  46. dist = relationship_distance(subject[1], person[1])
  47. result.append((*subject, rel_name2, *person, rel_name, dist))
  48. return result
  49. def relationship_list_all(family_list):
  50. result = []
  51. for i in range(len(family_list)):
  52. result.extend(relationship_list(family_list, i))
  53. return result