package boader;
import java.util.HashMap;
public class pro {
String str;
String[] subword=new String[500];
int subnumber,s;
String[] word=new String[5000];
int wordnumber;
HashMap<String,Integer> map = new HashMap<String,Integer>();
int h,tr;
int[] count=new int[5000];
String[][] policy=new String[6][500];
int[] number=new int[6];
double[] p=new double[5000];
public static void main(String[] args) {
pro test=new pro();
}
pro(){
str="私は猫である";
part(str);
int sx;
sx=0;
for(s=1;s<subnumber+1;s++){
h=0;
if(map.containsKey(subword[s]))h=100;
if(h<50)sx=sx+1;
if(h<50)map.put(subword[s],sx);
if(h<60)word[sx]=subword[s];
}
wordnumber=sx;
for(s=1;s<subnumber+1;s++){
sx=map.get(subword[s]);
count[sx]=count[sx]+1;
}
System.out.println(wordnumber);
int z1;
z1=0;
for(s=1;s<wordnumber+1;s++){
z1=z1+count[s];
}
for(s=1;s<wordnumber+1;s++){
p[s]=(double)count[s]/z1;
}
p[0]=(double)1/z1;
str="お財布は女物です";
for(tr=1;tr<6;tr++){
part(str);
for(s=1;s<subnumber+1;s++){
policy[tr][s]=subword[s];
}
number[tr]=subnumber;
}
int trs;
double maxp;
trs=0;
maxp=-9999;
for(tr=1;tr<6;tr++){
subnumber=number[tr];
double lp;
lp=1;
for(s=1;s<subnumber+1;s++){
sx=0;
if(map.containsKey(policy[tr][s]))sx=map.get(policy[tr][s]);
lp=lp*Math.log(p[sx]);
}
if(lp>maxp)trs=tr;
if(lp>maxp)maxp=lp;
}
subnumber=number[trs];
for(s=1;s<subnumber+1;s++){
subword[s]=policy[trs][s];
}
sx=wordnumber;
for(s=1;s<subnumber+1;s++){
h=0;
if(map.containsKey(subword[s]))h=100;
if(h<50)sx=sx+1;
if(h<50)map.put(subword[s],sx);
if(h<60)word[sx]=subword[s];
}
wordnumber=sx;
for(s=1;s<subnumber+1;s++){
sx=map.get(subword[s]);
count[sx]=count[sx]+1;
}
System.out.println(wordnumber);
}
void part(String str){
int number;
int s,sx;
int[] x=new int[1000];
number=str.length();
x[0]=0;
for(s=1;s<number;s++){
x[s]=0;
double p1=Math.random();
if(p1<0.15)x[s]=1;
}
for(s=0;s<number+1;s++){
subword[s]="";
}
sx=1;
for(s=0;s<number;s++){
if(x[s]==1)sx=sx+1;
subword[sx]=subword[sx]+str.substring(s,s+1);
}
subnumber=sx;
}
}
最終更新:2014年04月13日 04:39