-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecognizer.java
85 lines (82 loc) · 2.68 KB
/
Recognizer.java
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
package com.example.vidhi.reminderr;
/**
* Created by vidhi / nishant on 07-05-2017.
*/
public class Recognizer {
private static final int MAXN=Const.MAXN;
private static final int MAXNUM=Const.MAXNUM;
private static final int[][][][] count=new int[MAXN][MAXN][2][MAXNUM+1];
private static final int[] count2=new int[MAXNUM+1];
private static final double[][][][] po=new double[MAXN][MAXN][2][MAXNUM+1];
private static final double[] po2=new double[MAXNUM+1];
public static void init()
{
for (int i=0;i<MAXN;i++)
for (int j=0;j<MAXN;j++)
for (int k=0;k<=MAXNUM;k++)
count[i][j][0][k]=count[i][j][1][k]=0;
for(int i=0;i<=MAXNUM;i++)
count2[i]=0;
}
public static void add(boolean[][] input,int n)
{
for (int i=0;i<MAXN;i++)
for (int j=0;j<MAXN;j++)
if (input[i][j])
count[i][j][1][n]++;
else
count[i][j][0][n]++;
count2[n]++;
}
public static void learn()
{
boolean flag=false;
for (int i=0;i<=MAXNUM;i++)
if (count2[i]==0)
{
flag=true;
break;
}
if (flag)
{
for (int i=0;i<=MAXNUM;i++)
count2[i]++;
}
int sum=0;
for (int i=0;i<=MAXNUM;i++)
sum+=count2[i];
for (int i=0;i<=MAXNUM;i++)
po2[i]=count2[i]*1.0/sum;
for (int i=0;i<MAXN;i++)
for (int j=0;j<MAXN;j++)
for (int k=0;k<=MAXNUM;k++)
{
if (count[i][j][0][k]!=0&&count[i][j][1][k]!=0)
{
po[i][j][0][k]=count[i][j][0][k]*1.0/count2[k];
po[i][j][1][k]=count[i][j][1][k]*1.0/count2[k];
}
else
{
po[i][j][0][k]=(count[i][j][0][k]+1)*1.0/(count2[k]+2);
po[i][j][1][k]=(count[i][j][1][k]+1)*1.0/(count2[k]+2);
}
}
}
public static double[] recognize(boolean[][] input)
{
double[] result=new double[MAXNUM+1];
for (int i=0;i<=MAXNUM;i++)
{
result[i]=1;
for (int j=0;j<MAXN;j++)
for (int k=0;k<MAXN;k++)
if (input[j][k])
result[i]*=po[j][k][1][i];
else
result[i]*=po[j][k][0][i];
result[i]*=po2[i];
}
return result;
}
}