• R/O
  • SSH
  • HTTPS

particle-filter: 提交


Commit MetaInfo

修订版15 (tree)
时间2010-05-04 18:31:35
作者tullio

Log Message

fixed test codes

更改概述

差异

--- test/test_edit.cc (revision 14)
+++ test/test_edit.cc (revision 15)
@@ -1,8 +1,8 @@
11 #include <cppcutter.h>
22 #include <particle.h>
33 #include <iostream>
4+#include <edit.cc>
45 using namespace std;
5-extern int edit(const string &,const string &);
66 namespace test_edit
77 {
88
@@ -15,9 +15,9 @@
1515
1616 void test_edit(void)
1717 {
18- cut_assert_equal_int(3,edit("kitten","sitting"));
19- cut_assert_equal_int(1,edit("あかまきがみ","あおまきがみ"));
20- cut_assert_equal_int(3,edit("あかMAXがみ","あおMINがみ"));
21- cut_assert_equal_int(4,edit("情報工学","画像情報"));
18+ cut_assert_equal_int(3,LevenshteinDistance::edit("kitten","sitting"));
19+ cut_assert_equal_int(1,LevenshteinDistance::edit("あかまきがみ","あおまきがみ"));
20+ cut_assert_equal_int(3,LevenshteinDistance::edit("あかMAXがみ","あおMINがみ"));
21+ cut_assert_equal_int(4,LevenshteinDistance::edit("情報工学","画像情報"));
2222 }
2323 }
--- test/test_filter.cc (revision 14)
+++ test/test_filter.cc (revision 15)
@@ -69,4 +69,4 @@
6969 {
7070 test_filter::test_construction();
7171 return(0);
72-}
\ No newline at end of file
72+}
--- src/edit.java (revision 14)
+++ src/edit.java (revision 15)
@@ -4,16 +4,17 @@
44
55 class LevenshteinDistance
66 {
7- private int isutf8(String args) throws UnsupportedEncodingException
7+ static int isutf8(String args) throws UnsupportedEncodingException
88 {
99 int ret = -1;
1010 byte[] c = args.getBytes("UTF-8");
1111 if((c[0]&0x80) == 0x00) ret = 1;
1212 else if((c[0]&0xe0) == 0xc0 && (c[1]&0xc0) == 0x80) ret = 2;
13- else if((c[0]&0xf0) == 0xe0 && (c[1]&0xc0) == 0x80 && (c[2]&0xc0) == 0x80 ) ret = 3;
13+ else if((c[0]&0xf0) == 0xe0 && (c[1]&0xc0) == 0x80
14+ && (c[2]&0xc0) == 0x80 ) ret = 3;
1415 return(ret);
1516 }
16- Boolean equal(String s1, String s2) throws UnsupportedEncodingException
17+ static Boolean equal(String s1, String s2) throws UnsupportedEncodingException
1718 {
1819 Boolean ret = false;
1920 if(isutf8(s1)==isutf8(s2))
@@ -29,7 +30,7 @@
2930 }
3031 return(ret);
3132 }
32- int edit(String px, String py) throws UnsupportedEncodingException
33+ static int edit(String px, String py) throws UnsupportedEncodingException
3334 {
3435 Vector<Vector> row = new Vector<Vector>();
3536 int len1=0,len2=0;
@@ -53,7 +54,8 @@
5354 for(j=1;j<=len2;++j)
5455 {
5556 row.get(i).set(j,Math.min(Math.min(
56- (Integer)(row.get(i-1).get(j-1)) + (equal(px.substring(i-1),py.substring(j-1))?0:1) , // replace
57+ (Integer)(row.get(i-1).get(j-1))
58+ + (equal(px.substring(i-1),py.substring(j-1))?0:1) , // replace
5759 (Integer)(row.get(i).get(j-1)) + 1), // delete
5860 (Integer)(row.get(i-1).get(j)) + 1 )); // insert
5961 }
@@ -65,7 +67,6 @@
6567
6668 public static void main(String[] args) throws UnsupportedEncodingException
6769 {
68- LevenshteinDistance ld = new LevenshteinDistance() ;
69- System.out.println(ld.edit("あい","あうう"));
70+ System.out.println(LevenshteinDistance.edit("あい","あいう"));
7071 }
7172 }
\ No newline at end of file
--- src/particle.cc (revision 14)
+++ src/particle.cc (revision 15)
@@ -63,7 +63,7 @@
6363 s<<"(";
6464 // for(vector<C>::iterator i=p.begin();
6565 // i!=p.end();++i)
66- for(int i=0;i<p.size();++i)
66+ for(unsigned int i=0;i<p.size();++i)
6767 {
6868 // if(i!=p.begin()) s<<",";
6969 if(i!=0) s<<",";
--- src/edit.cc (revision 14)
+++ src/edit.cc (revision 15)
@@ -1,75 +1,72 @@
11 #include <iostream>
2-#include <algorithm>
32 #include <vector>
43 using namespace std;
5-int isutf8(const string &c)
4+class LevenshteinDistance
65 {
7- int ret;
8- if((c[0]&0x80) == 0x00) ret = 1;
9- else if((c[0]&0xe0) == 0xc0 && (c[1]&0xc0) == 0x80) ret = 2;
10- else if((c[0]&0xf0) == 0xe0 && (c[1]&0xc0) == 0x80 && (c[2]&0xc0) == 0x80 ) ret = 3;
11- return(ret);
12-}
13-bool equal(const string &s1, const string &s2)
14-{
15- bool ret = false;
16- if(isutf8(s1)==isutf8(s2))
6+public:
7+ static int isutf8(const string &c)
178 {
18- int f=0;
19- for(int i=0;i<isutf8(s1);++i)
9+ int ret;
10+ if((c[0]&0x80) == 0x00) ret = 1;
11+ else if((c[0]&0xe0) == 0xc0 && (c[1]&0xc0) == 0x80) ret = 2;
12+ else if((c[0]&0xf0) == 0xe0 && (c[1]&0xc0) == 0x80
13+ && (c[2]&0xc0) == 0x80 ) ret = 3;
14+ return(ret);
15+ }
16+ static bool equal(const string &s1, const string &s2)
17+ {
18+ bool ret = false;
19+ if(isutf8(s1)==isutf8(s2))
2020 {
21- if(s1[i]==s2[i]) ++f;
21+ int f=0;
22+ for(int i=0;i<isutf8(s1);++i)
23+ {
24+ if(s1[i]==s2[i]) ++f;
25+ }
26+ if(f==isutf8(s1)) ret = true;
2227 }
23- if(f==isutf8(s1)) ret = true;
28+ return(ret);
2429 }
25- return(ret);
26-}
27-int edit(const string &px, const string &py)
28-{
29- vector<vector<int> > row;
30- int len1=0,len2=0;
31- int i,j;
32- int result;
33- // len1=(int)px.size(); len2=(int)py.size();
34- for(i=0;i<px.size();)
30+ static int edit(const string &px, const string &py)
3531 {
36- i += isutf8(&px[i]);
37- ++len1;
38- }
39- for(i=0;i<py.size();)
40- {
41- i += isutf8(&py[i]);
42- ++len2;
43- }
44- row.resize(len1+1);
45- for(vector<vector<int> >::iterator i=row.begin(); i!=row.end(); ++i)
46- {
47- i->resize(len2+1);
48- }
32+ int len1=0,len2=0;
33+ int i,j;
34+ int result;
35+ for(unsigned i=0;i<px.size();)
36+ {
37+ i += isutf8(&px[i]);
38+ ++len1;
39+ }
40+ for(unsigned i=0;i<py.size();)
41+ {
42+ i += isutf8(&py[i]);
43+ ++len2;
44+ }
45+ vector<vector<int> > row(len1+1, vector<int>(len2+1));
4946
50- for(i=0;i<len1+1;i++) row[i][0]=i;
51- for(i=0;i<len2+1;i++) row[0][i]=i;
52- int k=0,l=0;
53- for(i=1;i<=len1;++i)
54- {
55- l = 0;
56- for(j=1;j<=len2;++j)
47+ for(i=0;i<len1+1;i++) row[i][0]=i;
48+ for(i=0;i<len2+1;i++) row[0][i]=i;
49+ int k=0,l=0;
50+ for(i=1;i<=len1;++i)
5751 {
58- row[i][j] = min(min(
59- row[i-1][j-1] + (equal(&px[k],&py[l])?0:1) , // replace
60- row[i][j-1] + 1), // delete
61- row[i-1][j] + 1 ); // insert
62- l += isutf8(&py[l]);
52+ l = 0;
53+ for(j=1;j<=len2;++j)
54+ {
55+ row[i][j] = min(min(
56+ row[i-1][j-1] + (equal(&px[k],&py[l])?0:1) , // replace
57+ row[i][j-1] + 1), // delete
58+ row[i-1][j] + 1 ); // insert
59+ l += isutf8(&py[l]);
60+ }
61+ k += isutf8(&px[k]);
6362 }
64- k += isutf8(&px[k]);
63+ result=row[i-1][j-1];
64+
65+ return result;
6566 }
66- result=row[i-1][j-1];
67-
68- return result;
69-}
67+};
7068 int main()
7169 {
72-// cout<<edit("kitten","sitting")<<endl;
73- cout<<edit("あほb","bあ")<<endl;
70+ cout<<LevenshteinDistance::edit("あい","あいう")<<endl;
7471 return(0);
75-}
\ No newline at end of file
72+}
--- src/filter.cc (revision 14)
+++ src/filter.cc (revision 15)
@@ -78,4 +78,4 @@
7878 {
7979 y = p;
8080 return(true);
81-}
\ No newline at end of file
81+}
--- src/particles.cc (revision 14)
+++ src/particles.cc (revision 15)
@@ -22,7 +22,7 @@
2222 typedef Particle<C> ParticleC;
2323 // for(vector<ParticleC>::iterator i= p.begin();
2424 // i!=p.end();++i)
25- for(int i=0;i<p.size();++i)
25+ for(unsigned int i=0;i<p.size();++i)
2626 {
2727 // i->resize(dimension);
2828 p[i].resize(dimension);
@@ -49,7 +49,7 @@
4949 {
5050 // for(vector<Particle<C> >::iterator i=p.begin();
5151 // i!=p.end();++i)
52- for(int i=0;i<p.size();++i)
52+ for(unsigned int i=0;i<p.size();++i)
5353 {
5454 // i->dump();
5555 p[i].dump();
@@ -58,4 +58,4 @@
5858 }
5959
6060 template class Particles<int>;
61-template class Particles<double>;
\ No newline at end of file
61+template class Particles<double>;
Show on old repository browser