• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.10.2 API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • kdecore
  • localization
  • probers
nsSBCharSetProber.cpp
Go to the documentation of this file.
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* -*- C++ -*-
3 * Copyright (C) 1998 <developer@mozilla.org>
4 *
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25 
26 #include "nsSBCharSetProber.h"
27 
28 #include <stdio.h>
29 
30 namespace kencodingprober {
31 nsProbingState nsSingleByteCharSetProber::HandleData(const char* aBuf, unsigned int aLen)
32 {
33  unsigned char order;
34 
35  for (unsigned int i = 0; i < aLen; i++)
36  {
37  order = mModel->charToOrderMap[(unsigned char)aBuf[i]];
38 
39  if (order < SYMBOL_CAT_ORDER)
40  mTotalChar++;
41  if (order < SAMPLE_SIZE)
42  {
43  mFreqChar++;
44 
45  if (mLastOrder < SAMPLE_SIZE)
46  {
47  mTotalSeqs++;
48  if (!mReversed)
49  ++(mSeqCounters[(int)mModel->precedenceMatrix[mLastOrder*SAMPLE_SIZE+order]]);
50  else // reverse the order of the letters in the lookup
51  ++(mSeqCounters[(int)mModel->precedenceMatrix[order*SAMPLE_SIZE+mLastOrder]]);
52  }
53  }
54  mLastOrder = order;
55  }
56 
57  if (mState == eDetecting)
58  if (mTotalSeqs > SB_ENOUGH_REL_THRESHOLD)
59  {
60  float cf = GetConfidence();
61  if (cf > POSITIVE_SHORTCUT_THRESHOLD)
62  mState = eFoundIt;
63  else if (cf < NEGATIVE_SHORTCUT_THRESHOLD)
64  mState = eNotMe;
65  }
66 
67  return mState;
68 }
69 
70 void nsSingleByteCharSetProber::Reset(void)
71 {
72  mState = eDetecting;
73  mLastOrder = 255;
74  for (unsigned int i = 0; i < NUMBER_OF_SEQ_CAT; i++)
75  mSeqCounters[i] = 0;
76  mTotalSeqs = 0;
77  mTotalChar = 0;
78  mFreqChar = 0;
79 }
80 
81 //#define NEGATIVE_APPROACH 1
82 
83 float nsSingleByteCharSetProber::GetConfidence(void)
84 {
85 #ifdef NEGATIVE_APPROACH
86  if (mTotalSeqs > 0)
87  if (mTotalSeqs > mSeqCounters[NEGATIVE_CAT]*10 )
88  return ((float)(mTotalSeqs - mSeqCounters[NEGATIVE_CAT]*10))/mTotalSeqs * mFreqChar / mTotalChar;
89  return (float)0.01;
90 #else //POSITIVE_APPROACH
91  float r;
92 
93  if (mTotalSeqs > 0) {
94  r = ((float)1.0) * mSeqCounters[POSITIVE_CAT] / mTotalSeqs / mModel->mTypicalPositiveRatio;
95  r = r*mFreqChar/mTotalChar;
96  if (r >= (float)1.00)
97  r = (float)0.99;
98  return r;
99  }
100  return (float)0.01;
101 #endif
102 }
103 
104 const char* nsSingleByteCharSetProber::GetCharSetName()
105 {
106  if (!mNameProber)
107  return mModel->charsetName;
108  return mNameProber->GetCharSetName();
109 }
110 
111 #ifdef DEBUG_PROBE
112 void nsSingleByteCharSetProber::DumpStatus()
113 {
114  printf(" SBCS: %1.3f [%s]\r\n", GetConfidence(), GetCharSetName());
115 }
116 #endif
117 }
118 
119 
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Tue Apr 16 2013 20:55:47 by doxygen 1.8.3.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

Skip menu "KDECore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.10.2 API Reference

Skip menu "kdelibs-4.10.2 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal