Coverage for presidio_analyzer / predefined_recognizers / country_specific / korea / kr_frn_recognizer.py: 100%

12 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-03-29 09:03 +0000

1from typing import List, Optional, Tuple 

2 

3from presidio_analyzer import Pattern 

4from presidio_analyzer.predefined_recognizers.country_specific.korea.kr_rrn_recognizer import ( # noqa: E501 

5 KrRrnRecognizer, 

6) 

7 

8 

9class KrFrnRecognizer(KrRrnRecognizer): 

10 """ 

11 Recognize Korean Foreigner Registration Number (FRN). 

12 

13 The Korean FRN is a 13-digit number issued to registered foreigners in Korea. 

14 The format is YYMMDD-GHIJKLX where: 

15 - YYMMDD represents the birth date. 

16 - G determines gender and century (5-8). 

17 - 5: Male, 1900-1999 birth 

18 - 6: Female, 1900-1999 birth 

19 - 7: Male, 2000-2099 birth 

20 - 8: Female, 2000-2099 birth 

21 - (Note: 9 and 0 are sometimes used for 1800s birth, but rare) 

22 

23 For FRNs issued before October 2020: 

24 - HIJKL is a serial number assigned by district 

25 - X is a check digit calculated using the preceding 12 digits 

26 

27 For FRNs issued after October 2020: 

28 - HIJKLX is a random number 

29 

30 Reference: https://en.wikipedia.org/wiki/Resident_registration_number 

31 

32 :param patterns: List of patterns to be used by this recognizer 

33 :param context: List of context words to increase confidence in detection 

34 :param supported_language: Language this recognizer supports 

35 :param supported_entity: The entity this recognizer can detect 

36 :param replacement_pairs: List of tuples with potential replacement values 

37 for different strings to be used during pattern matching. 

38 This can allow a greater variety in input, for example by removing dashes. 

39 """ 

40 

41 PATTERNS = [ 

42 Pattern( 

43 "FRN (Medium)", 

44 r"(?<!\d)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])(-?)[5-8]\d{6}(?!\d)", 

45 0.5, 

46 ) 

47 ] 

48 

49 CONTEXT = [ 

50 "외국인등록번호", 

51 "Korean FRN", 

52 "FRN", 

53 "Foreigner Registration Number", 

54 "Korean Foreigner Registration Number", 

55 "외국인번호", 

56 ] 

57 

58 def __init__( 

59 self, 

60 patterns: Optional[List[Pattern]] = None, 

61 context: Optional[List[str]] = None, 

62 supported_language: str = "ko", 

63 supported_entity: str = "KR_FRN", 

64 replacement_pairs: Optional[List[Tuple[str, str]]] = None, 

65 name: Optional[str] = None, 

66 ): 

67 super().__init__( 

68 patterns=patterns if patterns else self.PATTERNS, 

69 context=context if context else self.CONTEXT, 

70 supported_language=supported_language, 

71 supported_entity=supported_entity, 

72 replacement_pairs=replacement_pairs, 

73 name=name, 

74 ) 

75 

76 def _validate_checksum(self, frn: str) -> bool: 

77 """ 

78 Validate the checksum of Korean FRN. 

79 

80 The checksum is calculated using the preceding 12 digits. 

81 X = (13 - (2A+3B+4C+5D+6E+7F+8G+9H+2I+3J+4K+5L) mod 11) mod 10 

82 

83 :param frn: The FRN to validate 

84 :return: True if checksum is valid, False otherwise 

85 """ 

86 digit_sum = super()._compute_checksum(frn) 

87 checksum = (13 - (digit_sum % 11)) % 10 

88 return checksum == int(frn[12])