Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/chardet/latin1prober.py: 98%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x 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 Universal charset detector 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) 2001
7# the Initial Developer. All Rights Reserved.
8#
9# Contributor(s):
10# Mark Pilgrim - port to Python
11# Shy Shalom - original C code
12#
13# This library is free software; you can redistribute it and/or
14# modify it under the terms of the GNU Lesser General Public
15# License as published by the Free Software Foundation; either
16# version 2.1 of the License, or (at your option) any later version.
17#
18# This library is distributed in the hope that it will be useful,
19# but WITHOUT ANY WARRANTY; without even the implied warranty of
20# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21# Lesser General Public License for more details.
22#
23# You should have received a copy of the GNU Lesser General Public
24# License along with this library; if not, see
25# <https://www.gnu.org/licenses/>.
26######################### END LICENSE BLOCK #########################
28from typing import List, Union
30from .charsetprober import CharSetProber
31from .enums import ProbingState
33FREQ_CAT_NUM = 4
35UDF = 0 # undefined
36OTH = 1 # other
37ASC = 2 # ascii capital letter
38ASS = 3 # ascii small letter
39ACV = 4 # accent capital vowel
40ACO = 5 # accent capital other
41ASV = 6 # accent small vowel
42ASO = 7 # accent small other
43CLASS_NUM = 8 # total classes
45# fmt: off
46Latin1_CharToClass = (
47 OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # 00 - 07
48 OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # 08 - 0F
49 OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # 10 - 17
50 OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # 18 - 1F
51 OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # 20 - 27
52 OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # 28 - 2F
53 OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # 30 - 37
54 OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # 38 - 3F
55 OTH, ASC, ASC, ASC, ASC, ASC, ASC, ASC, # 40 - 47
56 ASC, ASC, ASC, ASC, ASC, ASC, ASC, ASC, # 48 - 4F
57 ASC, ASC, ASC, ASC, ASC, ASC, ASC, ASC, # 50 - 57
58 ASC, ASC, ASC, OTH, OTH, OTH, OTH, OTH, # 58 - 5F
59 OTH, ASS, ASS, ASS, ASS, ASS, ASS, ASS, # 60 - 67
60 ASS, ASS, ASS, ASS, ASS, ASS, ASS, ASS, # 68 - 6F
61 ASS, ASS, ASS, ASS, ASS, ASS, ASS, ASS, # 70 - 77
62 ASS, ASS, ASS, OTH, OTH, OTH, OTH, OTH, # 78 - 7F
63 OTH, UDF, OTH, ASO, OTH, OTH, OTH, OTH, # 80 - 87
64 OTH, OTH, ACO, OTH, ACO, UDF, ACO, UDF, # 88 - 8F
65 UDF, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # 90 - 97
66 OTH, OTH, ASO, OTH, ASO, UDF, ASO, ACO, # 98 - 9F
67 OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # A0 - A7
68 OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # A8 - AF
69 OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # B0 - B7
70 OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # B8 - BF
71 ACV, ACV, ACV, ACV, ACV, ACV, ACO, ACO, # C0 - C7
72 ACV, ACV, ACV, ACV, ACV, ACV, ACV, ACV, # C8 - CF
73 ACO, ACO, ACV, ACV, ACV, ACV, ACV, OTH, # D0 - D7
74 ACV, ACV, ACV, ACV, ACV, ACO, ACO, ACO, # D8 - DF
75 ASV, ASV, ASV, ASV, ASV, ASV, ASO, ASO, # E0 - E7
76 ASV, ASV, ASV, ASV, ASV, ASV, ASV, ASV, # E8 - EF
77 ASO, ASO, ASV, ASV, ASV, ASV, ASV, OTH, # F0 - F7
78 ASV, ASV, ASV, ASV, ASV, ASO, ASO, ASO, # F8 - FF
79)
81# 0 : illegal
82# 1 : very unlikely
83# 2 : normal
84# 3 : very likely
85Latin1ClassModel = (
86# UDF OTH ASC ASS ACV ACO ASV ASO
87 0, 0, 0, 0, 0, 0, 0, 0, # UDF
88 0, 3, 3, 3, 3, 3, 3, 3, # OTH
89 0, 3, 3, 3, 3, 3, 3, 3, # ASC
90 0, 3, 3, 3, 1, 1, 3, 3, # ASS
91 0, 3, 3, 3, 1, 2, 1, 2, # ACV
92 0, 3, 3, 3, 3, 3, 3, 3, # ACO
93 0, 3, 1, 3, 1, 1, 1, 3, # ASV
94 0, 3, 1, 3, 1, 1, 3, 3, # ASO
95)
96# fmt: on
99class Latin1Prober(CharSetProber):
100 def __init__(self) -> None:
101 super().__init__()
102 self._last_char_class = OTH
103 self._freq_counter: List[int] = []
104 self.reset()
106 def reset(self) -> None:
107 self._last_char_class = OTH
108 self._freq_counter = [0] * FREQ_CAT_NUM
109 super().reset()
111 @property
112 def charset_name(self) -> str:
113 return "ISO-8859-1"
115 @property
116 def language(self) -> str:
117 return ""
119 def feed(self, byte_str: Union[bytes, bytearray]) -> ProbingState:
120 byte_str = self.remove_xml_tags(byte_str)
121 for c in byte_str:
122 char_class = Latin1_CharToClass[c]
123 freq = Latin1ClassModel[(self._last_char_class * CLASS_NUM) + char_class]
124 if freq == 0:
125 self._state = ProbingState.NOT_ME
126 break
127 self._freq_counter[freq] += 1
128 self._last_char_class = char_class
130 return self.state
132 def get_confidence(self) -> float:
133 if self.state == ProbingState.NOT_ME:
134 return 0.01
136 total = sum(self._freq_counter)
137 confidence = (
138 0.0
139 if total < 0.01
140 else (self._freq_counter[3] - self._freq_counter[1] * 20.0) / total
141 )
142 confidence = max(confidence, 0.0)
143 # lower the confidence of latin1 so that other more accurate
144 # detector can take priority.
145 confidence *= 0.73
146 return confidence