Path: blob/master/thirdparty/chardet/hebrewprober.py
2992 views
######################## BEGIN LICENSE BLOCK ########################1# The Original Code is Mozilla Universal charset detector code.2#3# The Initial Developer of the Original Code is4# Shy Shalom5# Portions created by the Initial Developer are Copyright (C) 20056# the Initial Developer. All Rights Reserved.7#8# Contributor(s):9# Mark Pilgrim - port to Python10#11# This library is free software; you can redistribute it and/or12# modify it under the terms of the GNU Lesser General Public13# License as published by the Free Software Foundation; either14# version 2.1 of the License, or (at your option) any later version.15#16# This library is distributed in the hope that it will be useful,17# but WITHOUT ANY WARRANTY; without even the implied warranty of18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU19# Lesser General Public License for more details.20#21# You should have received a copy of the GNU Lesser General Public22# License along with this library; if not, write to the Free Software23# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA24# 02110-1301 USA25######################### END LICENSE BLOCK #########################2627from .charsetprober import CharSetProber28from .enums import ProbingState2930# This prober doesn't actually recognize a language or a charset.31# It is a helper prober for the use of the Hebrew model probers3233### General ideas of the Hebrew charset recognition ###34#35# Four main charsets exist in Hebrew:36# "ISO-8859-8" - Visual Hebrew37# "windows-1255" - Logical Hebrew38# "ISO-8859-8-I" - Logical Hebrew39# "x-mac-hebrew" - ?? Logical Hebrew ??40#41# Both "ISO" charsets use a completely identical set of code points, whereas42# "windows-1255" and "x-mac-hebrew" are two different proper supersets of43# these code points. windows-1255 defines additional characters in the range44# 0x80-0x9F as some misc punctuation marks as well as some Hebrew-specific45# diacritics and additional 'Yiddish' ligature letters in the range 0xc0-0xd6.46# x-mac-hebrew defines similar additional code points but with a different47# mapping.48#49# As far as an average Hebrew text with no diacritics is concerned, all four50# charsets are identical with respect to code points. Meaning that for the51# main Hebrew alphabet, all four map the same values to all 27 Hebrew letters52# (including final letters).53#54# The dominant difference between these charsets is their directionality.55# "Visual" directionality means that the text is ordered as if the renderer is56# not aware of a BIDI rendering algorithm. The renderer sees the text and57# draws it from left to right. The text itself when ordered naturally is read58# backwards. A buffer of Visual Hebrew generally looks like so:59# "[last word of first line spelled backwards] [whole line ordered backwards60# and spelled backwards] [first word of first line spelled backwards]61# [end of line] [last word of second line] ... etc' "62# adding punctuation marks, numbers and English text to visual text is63# naturally also "visual" and from left to right.64#65# "Logical" directionality means the text is ordered "naturally" according to66# the order it is read. It is the responsibility of the renderer to display67# the text from right to left. A BIDI algorithm is used to place general68# punctuation marks, numbers and English text in the text.69#70# Texts in x-mac-hebrew are almost impossible to find on the Internet. From71# what little evidence I could find, it seems that its general directionality72# is Logical.73#74# To sum up all of the above, the Hebrew probing mechanism knows about two75# charsets:76# Visual Hebrew - "ISO-8859-8" - backwards text - Words and sentences are77# backwards while line order is natural. For charset recognition purposes78# the line order is unimportant (In fact, for this implementation, even79# word order is unimportant).80# Logical Hebrew - "windows-1255" - normal, naturally ordered text.81#82# "ISO-8859-8-I" is a subset of windows-1255 and doesn't need to be83# specifically identified.84# "x-mac-hebrew" is also identified as windows-1255. A text in x-mac-hebrew85# that contain special punctuation marks or diacritics is displayed with86# some unconverted characters showing as question marks. This problem might87# be corrected using another model prober for x-mac-hebrew. Due to the fact88# that x-mac-hebrew texts are so rare, writing another model prober isn't89# worth the effort and performance hit.90#91#### The Prober ####92#93# The prober is divided between two SBCharSetProbers and a HebrewProber,94# all of which are managed, created, fed data, inquired and deleted by the95# SBCSGroupProber. The two SBCharSetProbers identify that the text is in96# fact some kind of Hebrew, Logical or Visual. The final decision about which97# one is it is made by the HebrewProber by combining final-letter scores98# with the scores of the two SBCharSetProbers to produce a final answer.99#100# The SBCSGroupProber is responsible for stripping the original text of HTML101# tags, English characters, numbers, low-ASCII punctuation characters, spaces102# and new lines. It reduces any sequence of such characters to a single space.103# The buffer fed to each prober in the SBCS group prober is pure text in104# high-ASCII.105# The two SBCharSetProbers (model probers) share the same language model:106# Win1255Model.107# The first SBCharSetProber uses the model normally as any other108# SBCharSetProber does, to recognize windows-1255, upon which this model was109# built. The second SBCharSetProber is told to make the pair-of-letter110# lookup in the language model backwards. This in practice exactly simulates111# a visual Hebrew model using the windows-1255 logical Hebrew model.112#113# The HebrewProber is not using any language model. All it does is look for114# final-letter evidence suggesting the text is either logical Hebrew or visual115# Hebrew. Disjointed from the model probers, the results of the HebrewProber116# alone are meaningless. HebrewProber always returns 0.00 as confidence117# since it never identifies a charset by itself. Instead, the pointer to the118# HebrewProber is passed to the model probers as a helper "Name Prober".119# When the Group prober receives a positive identification from any prober,120# it asks for the name of the charset identified. If the prober queried is a121# Hebrew model prober, the model prober forwards the call to the122# HebrewProber to make the final decision. In the HebrewProber, the123# decision is made according to the final-letters scores maintained and Both124# model probers scores. The answer is returned in the form of the name of the125# charset identified, either "windows-1255" or "ISO-8859-8".126127class HebrewProber(CharSetProber):128# windows-1255 / ISO-8859-8 code points of interest129FINAL_KAF = 0xea130NORMAL_KAF = 0xeb131FINAL_MEM = 0xed132NORMAL_MEM = 0xee133FINAL_NUN = 0xef134NORMAL_NUN = 0xf0135FINAL_PE = 0xf3136NORMAL_PE = 0xf4137FINAL_TSADI = 0xf5138NORMAL_TSADI = 0xf6139140# Minimum Visual vs Logical final letter score difference.141# If the difference is below this, don't rely solely on the final letter score142# distance.143MIN_FINAL_CHAR_DISTANCE = 5144145# Minimum Visual vs Logical model score difference.146# If the difference is below this, don't rely at all on the model score147# distance.148MIN_MODEL_DISTANCE = 0.01149150VISUAL_HEBREW_NAME = "ISO-8859-8"151LOGICAL_HEBREW_NAME = "windows-1255"152153def __init__(self):154super(HebrewProber, self).__init__()155self._final_char_logical_score = None156self._final_char_visual_score = None157self._prev = None158self._before_prev = None159self._logical_prober = None160self._visual_prober = None161self.reset()162163def reset(self):164self._final_char_logical_score = 0165self._final_char_visual_score = 0166# The two last characters seen in the previous buffer,167# mPrev and mBeforePrev are initialized to space in order to simulate168# a word delimiter at the beginning of the data169self._prev = ' '170self._before_prev = ' '171# These probers are owned by the group prober.172173def set_model_probers(self, logicalProber, visualProber):174self._logical_prober = logicalProber175self._visual_prober = visualProber176177def is_final(self, c):178return c in [self.FINAL_KAF, self.FINAL_MEM, self.FINAL_NUN,179self.FINAL_PE, self.FINAL_TSADI]180181def is_non_final(self, c):182# The normal Tsadi is not a good Non-Final letter due to words like183# 'lechotet' (to chat) containing an apostrophe after the tsadi. This184# apostrophe is converted to a space in FilterWithoutEnglishLetters185# causing the Non-Final tsadi to appear at an end of a word even186# though this is not the case in the original text.187# The letters Pe and Kaf rarely display a related behavior of not being188# a good Non-Final letter. Words like 'Pop', 'Winamp' and 'Mubarak'189# for example legally end with a Non-Final Pe or Kaf. However, the190# benefit of these letters as Non-Final letters outweighs the damage191# since these words are quite rare.192return c in [self.NORMAL_KAF, self.NORMAL_MEM,193self.NORMAL_NUN, self.NORMAL_PE]194195def feed(self, byte_str):196# Final letter analysis for logical-visual decision.197# Look for evidence that the received buffer is either logical Hebrew198# or visual Hebrew.199# The following cases are checked:200# 1) A word longer than 1 letter, ending with a final letter. This is201# an indication that the text is laid out "naturally" since the202# final letter really appears at the end. +1 for logical score.203# 2) A word longer than 1 letter, ending with a Non-Final letter. In204# normal Hebrew, words ending with Kaf, Mem, Nun, Pe or Tsadi,205# should not end with the Non-Final form of that letter. Exceptions206# to this rule are mentioned above in isNonFinal(). This is an207# indication that the text is laid out backwards. +1 for visual208# score209# 3) A word longer than 1 letter, starting with a final letter. Final210# letters should not appear at the beginning of a word. This is an211# indication that the text is laid out backwards. +1 for visual212# score.213#214# The visual score and logical score are accumulated throughout the215# text and are finally checked against each other in GetCharSetName().216# No checking for final letters in the middle of words is done since217# that case is not an indication for either Logical or Visual text.218#219# We automatically filter out all 7-bit characters (replace them with220# spaces) so the word boundary detection works properly. [MAP]221222if self.state == ProbingState.NOT_ME:223# Both model probers say it's not them. No reason to continue.224return ProbingState.NOT_ME225226byte_str = self.filter_high_byte_only(byte_str)227228for cur in byte_str:229if cur == ' ':230# We stand on a space - a word just ended231if self._before_prev != ' ':232# next-to-last char was not a space so self._prev is not a233# 1 letter word234if self.is_final(self._prev):235# case (1) [-2:not space][-1:final letter][cur:space]236self._final_char_logical_score += 1237elif self.is_non_final(self._prev):238# case (2) [-2:not space][-1:Non-Final letter][239# cur:space]240self._final_char_visual_score += 1241else:242# Not standing on a space243if ((self._before_prev == ' ') and244(self.is_final(self._prev)) and (cur != ' ')):245# case (3) [-2:space][-1:final letter][cur:not space]246self._final_char_visual_score += 1247self._before_prev = self._prev248self._prev = cur249250# Forever detecting, till the end or until both model probers return251# ProbingState.NOT_ME (handled above)252return ProbingState.DETECTING253254@property255def charset_name(self):256# Make the decision: is it Logical or Visual?257# If the final letter score distance is dominant enough, rely on it.258finalsub = self._final_char_logical_score - self._final_char_visual_score259if finalsub >= self.MIN_FINAL_CHAR_DISTANCE:260return self.LOGICAL_HEBREW_NAME261if finalsub <= -self.MIN_FINAL_CHAR_DISTANCE:262return self.VISUAL_HEBREW_NAME263264# It's not dominant enough, try to rely on the model scores instead.265modelsub = (self._logical_prober.get_confidence()266- self._visual_prober.get_confidence())267if modelsub > self.MIN_MODEL_DISTANCE:268return self.LOGICAL_HEBREW_NAME269if modelsub < -self.MIN_MODEL_DISTANCE:270return self.VISUAL_HEBREW_NAME271272# Still no good, back to final letter distance, maybe it'll save the273# day.274if finalsub < 0.0:275return self.VISUAL_HEBREW_NAME276277# (finalsub > 0 - Logical) or (don't know what to do) default to278# Logical.279return self.LOGICAL_HEBREW_NAME280281@property282def language(self):283return 'Hebrew'284285@property286def state(self):287# Remain active as long as any of the model probers are active.288if (self._logical_prober.state == ProbingState.NOT_ME) and \289(self._visual_prober.state == ProbingState.NOT_ME):290return ProbingState.NOT_ME291return ProbingState.DETECTING292293294