Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1######################## BEGIN LICENSE BLOCK ######################## 

2# The Original Code is mozilla.org code. 

3# 

4# The Initial Developer of the Original Code is 

5# Netscape Communications Corporation. 

6# Portions created by the Initial Developer are Copyright (C) 1998 

7# the Initial Developer. All Rights Reserved. 

8# 

9# Contributor(s): 

10# Mark Pilgrim - port to Python 

11# 

12# This library is free software; you can redistribute it and/or 

13# modify it under the terms of the GNU Lesser General Public 

14# License as published by the Free Software Foundation; either 

15# version 2.1 of the License, or (at your option) any later version. 

16# 

17# This library is distributed in the hope that it will be useful, 

18# but WITHOUT ANY WARRANTY; without even the implied warranty of 

19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 

20# Lesser General Public License for more details. 

21# 

22# You should have received a copy of the GNU Lesser General Public 

23# License along with this library; if not, write to the Free Software 

24# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 

25# 02110-1301 USA 

26######################### END LICENSE BLOCK ######################### 

27 

28from .charsetprober import CharSetProber 

29from .codingstatemachine import CodingStateMachine 

30from .enums import LanguageFilter, ProbingState, MachineState 

31from .escsm import (HZ_SM_MODEL, ISO2022CN_SM_MODEL, ISO2022JP_SM_MODEL, 

32 ISO2022KR_SM_MODEL) 

33 

34 

35class EscCharSetProber(CharSetProber): 

36 """ 

37 This CharSetProber uses a "code scheme" approach for detecting encodings, 

38 whereby easily recognizable escape or shift sequences are relied on to 

39 identify these encodings. 

40 """ 

41 

42 def __init__(self, lang_filter=None): 

43 super(EscCharSetProber, self).__init__(lang_filter=lang_filter) 

44 self.coding_sm = [] 

45 if self.lang_filter & LanguageFilter.CHINESE_SIMPLIFIED: 

46 self.coding_sm.append(CodingStateMachine(HZ_SM_MODEL)) 

47 self.coding_sm.append(CodingStateMachine(ISO2022CN_SM_MODEL)) 

48 if self.lang_filter & LanguageFilter.JAPANESE: 

49 self.coding_sm.append(CodingStateMachine(ISO2022JP_SM_MODEL)) 

50 if self.lang_filter & LanguageFilter.KOREAN: 

51 self.coding_sm.append(CodingStateMachine(ISO2022KR_SM_MODEL)) 

52 self.active_sm_count = None 

53 self._detected_charset = None 

54 self._detected_language = None 

55 self._state = None 

56 self.reset() 

57 

58 def reset(self): 

59 super(EscCharSetProber, self).reset() 

60 for coding_sm in self.coding_sm: 

61 if not coding_sm: 

62 continue 

63 coding_sm.active = True 

64 coding_sm.reset() 

65 self.active_sm_count = len(self.coding_sm) 

66 self._detected_charset = None 

67 self._detected_language = None 

68 

69 @property 

70 def charset_name(self): 

71 return self._detected_charset 

72 

73 @property 

74 def language(self): 

75 return self._detected_language 

76 

77 def get_confidence(self): 

78 if self._detected_charset: 

79 return 0.99 

80 else: 

81 return 0.00 

82 

83 def feed(self, byte_str): 

84 for c in byte_str: 

85 for coding_sm in self.coding_sm: 

86 if not coding_sm or not coding_sm.active: 

87 continue 

88 coding_state = coding_sm.next_state(c) 

89 if coding_state == MachineState.ERROR: 

90 coding_sm.active = False 

91 self.active_sm_count -= 1 

92 if self.active_sm_count <= 0: 

93 self._state = ProbingState.NOT_ME 

94 return self.state 

95 elif coding_state == MachineState.ITS_ME: 

96 self._state = ProbingState.FOUND_IT 

97 self._detected_charset = coding_sm.get_coding_state_machine() 

98 self._detected_language = coding_sm.language 

99 return self.state 

100 

101 return self.state