This repository has been archived by the owner on Nov 19, 2019. It is now read-only.
forked from JanX2/UniversalDetector
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWrappedUniversalDetector.cpp
130 lines (106 loc) · 2.65 KB
/
WrappedUniversalDetector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "WrappedUniversalDetector.h"
#include "nscore.h"
#include "nsUniversalDetector.h"
#include "nsCharSetProber.h"
// You are welcome to fix this ObjC wrapper to allow initializing nsUniversalDetector with a non-zero value for aLanguageFilter!
class wrappedUniversalDetector:public nsUniversalDetector
{
public:
wrappedUniversalDetector():nsUniversalDetector(NS_FILTER_ALL) {}
void Report(const char* aCharset) {}
const char *charset(float &confidence)
{
if(!mGotData)
{
confidence=0;
return 0;
}
if(mDetectedCharset)
{
confidence=1;
return mDetectedCharset;
}
switch(mInputState)
{
case eHighbyte:
{
float proberConfidence;
float maxProberConfidence = (float)0.0;
PRInt32 maxProber = 0;
for (PRInt32 i = 0; i < NUM_OF_CHARSET_PROBERS; i++)
{
if (mCharSetProbers[i])
{
proberConfidence = mCharSetProbers[i]->GetConfidence();
if (proberConfidence > maxProberConfidence)
{
maxProberConfidence = proberConfidence;
maxProber = i;
}
}
}
if (mCharSetProbers[maxProber]) {
confidence=maxProberConfidence;
return mCharSetProbers[maxProber]->GetCharSetName();
}
}
break;
case ePureAscii:
confidence=1.0;
return "UTF-8";
default:
break;
}
confidence=0;
return 0;
}
bool done()
{
if(mDetectedCharset) return true;
return false;
}
/*
void debug()
{
for (PRInt32 i = 0; i < NUM_OF_CHARSET_PROBERS; i++)
{
// If no data was received the array might stay filled with nulls
// the way it was initialized in the constructor.
if (mCharSetProbers[i])
mCharSetProbers[i]->DumpStatus();
}
}
*/
void reset() { Reset(); }
};
extern "C" {
void *AllocUniversalDetector()
{
return (void *)new wrappedUniversalDetector;
}
void FreeUniversalDetector(void *detectorPtr)
{
delete (wrappedUniversalDetector *)detectorPtr;
}
void UniversalDetectorHandleData(void *detectorPtr,const char *data,int length)
{
wrappedUniversalDetector *detector=(wrappedUniversalDetector *)detectorPtr;
if(detector->done()) return;
detector->HandleData(data,length);
}
void UniversalDetectorReset(void *detectorPtr)
{
wrappedUniversalDetector *detector=(wrappedUniversalDetector *)detectorPtr;
detector->reset();
}
int UniversalDetectorDone(void *detectorPtr)
{
wrappedUniversalDetector *detector=(wrappedUniversalDetector *)detectorPtr;
return detector->done()?1:0;
}
const char *UniversalDetectorCharset(void *detectorPtr, float *confidence)
{
wrappedUniversalDetector *detector=(wrappedUniversalDetector *)detectorPtr;
return detector->charset(*confidence);
}
}