Java汉字排序(3)按笔划排序

 

对于包含汉字的字符串来说,排序的方式主要有两种:一种是拼音,一种是笔画。

本文就讲述如何实现按笔划排序的比较器(Comparator)。


作者:Jeff 发表于:2007年12月21日 11:27 最后更新于: 2007年12月21日 12:38 
版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明
http://www.blogjava.net/jeff-lau/archive/2007/12/21/169257.html

笔画排序

要按笔画排序,就要实现笔画比较器。

class StokeComparator implements Comparator<String>

如果有个方法可以求得汉字的笔画数,上面的功能就很容易实现。如何求一个汉字的笔画数?最容易想到的就是查表法。建一个汉字笔画数表,如:

汉字Unicode编码笔画数
U4E001
U4E8C2
U9F8D16
.........

表二

如果是连续的、按unicode编码排好顺序的表,实际存储在笔画数表中的只需最后一列就够了。

那如何建这个表呢?这个表存储在哪里?

建汉字笔画数表

现在大多数系统还只能支持Unicode中的基本汉字那部分汉字,编码从U9FA6-U9FBF。所以我们只建这部分汉字的笔画表。汉字笔画数表,我们可以按照下面的方法生成:

  1. 用java程序生成一个文本文件(Chinese.csv)。包括所有的从U9FA6-U9FBF的字符的编码和文字。利用excel的按笔画排序功能,对Chinese.csv文件中的内容排序。
  2. 编写Java程序分析Chinese.csv文件,求得笔画数, 生成ChineseStroke.csv。矫正笔画数,重新按汉字的Unicode编码对ChineseStroke.csv文件排序。
  3. 只保留ChineseStroke.csv文件的最后一列,生成Stroke.csv。

在这里 下载上面3个步骤生成的3个文件

生成Chinese.csv的Java程序

 1 /**
 2  * @author Jeff 
 3  * 
 4  * Copyright (c) 复制或转载本文,请保留该注释。
 5  */
 6 package chinese.utility.preface;
 7 
 8 import java.io.IOException;
 9 import java.io.PrintWriter;
10 
11 public class ChineseCoder {
12 
13     public static void main(String[] args) throws IOException {
14         PrintWriter out = new PrintWriter("Chinese.csv");
15         // 基本汉字
16         for (char c = 0x4E00; c <= 0x9FA5; c++) {
17             out.println((int) c + "," + c);
18         }
19         out.flush();
20         out.close();
21 
22     }
23 
24 }

 

初始化笔画数

从Excel排序过后的Chinese.csv文件来看,排好序的文件还是有一定规律的。在文件的第9行-12行可以看出:逐行扫描的时候,当unicode会变小了,笔画数也就加1。

20059,乛
20101,亅
19969,丁
19970,丂

用下面的Java程序分析吧。

 1 /**
 2  * @author Jeff 
 3  * 
 4  * Copyright (c) 复制或转载本文,请保留该注释。
 5  */
 6 package chinese.utility.preface;
 7 
 8 import java.io.File;
 9 import java.io.IOException;
10 import java.io.PrintWriter;
11 import java.util.Scanner;
12 
13 public class Stroke {
14 
15     /**
16      * @param args
17      * @throws IOException
18      */
19     public static void main(String[] args) throws IOException {
20         Scanner in = new Scanner(new File("Chinese.csv"));
21         PrintWriter out = new PrintWriter("ChineseStroke.csv");
22         String oldLine = "999999";
23         int stroke = 0;
24         while (in.hasNextLine()) {
25             String line = in.nextLine();
26             if (line.compareTo(oldLine) < 0) {
27                 stroke++;
28             }
29             oldLine = line;
30             out.println(line + "," + stroke);
31         }
32         out.flush();
33         out.close();
34         in.close();
35     }
36 
37 }

 

上面用的这个规律有问题吗?有问题,从ChineseStroke.csv文件抽取最后几个汉字就发现,笔画数不对。为什么呢?

  • 笔画数可能不是连续的。
  • n+1笔画数的最小Unicode码可能比n笔画数的最大Unicode码要大

我们要人工核对ChineseStroke文件,但只要核对在笔画变化的那几个汉字的笔画数。最后,我发现,只有笔画数多于30的少数几个汉字的笔画数不对。核对并矫正笔画数后,用Excel按Unicode重新排序,去掉汉字和Unicode两列,只保留笔画数那列,得到Stroke.csv文件。

求得笔画数的方法和笔画比较器方法

求得笔画数的方法测试代码:

 1 /**
 2  * @author Jeff 
 3  * 
 4  * Copyright (c) 复制或转载本文,请保留该注释。
 5  */
 6 package chinese.utility.test;
 7 
 8 import static org.junit.Assert.assertEquals;
 9 
10 import org.junit.Before;
11 import org.junit.Test;
12 import chinese.utility.Chinese;
13 
14 public class StrokeTest {
15 
16     Chinese chinese;
17 
18     @Before
19     public void setUp() {
20         chinese = new Chinese();
21     }
22 
23     @Test
24     public void testStroke() {
25         assertEquals(1, chinese.stroke(''));
26     }
27 
28     @Test
29     public void testStroke2() {
30         assertEquals(2, chinese.stroke(''));
31     }
32 
33     @Test
34     public void testStroke16() {
35         assertEquals(16, chinese.stroke(''));
36     }
37 
38     @Test
39     public void testStrokeABC() {
40         assertEquals(-1, chinese.stroke('a'));
41     }
42 
43 }

 

求得笔画数的方法代码

 1 /**
 2  * @author Jeff 
 3  * 
 4  * Copyright (c) 复制或转载本文,请保留该注释。
 5  */
 6 package chinese.utility;
 7 
 8 import java.util.Comparator;
 9 
10 public class StrokeComparator implements Comparator<String> {
11 
12     public int compare(String o1, String o2) {
13 
14         Chinese chinese = new Chinese();
15 
16         for (int i = 0; i < o1.length() && i < o2.length(); i++) {
17             int codePoint1 = o1.codePointAt(i);
18             int codePoint2 = o2.codePointAt(i);
19             if (codePoint1 == codePoint2)
20                 continue;
21 
22             int stroke1 = chinese.stroke(codePoint1);
23             int stroke2 = chinese.stroke(codePoint2);
24 
25             if (stroke1 < 0 || stroke2 < 0) {
26                 return codePoint1 - codePoint2;
27             }
28 
29             if (stroke1 != stroke2) {
30                 return stroke1 - stroke2;
31             }
32         }
33 
34         return o1.length() - o2.length();
35     }
36 }

 

笔画比较器测试

 1 /**
 2  * @author Jeff 
 3  * 
 4  * Copyright (c) 复制或转载本文,请保留该注释。
 5  */
 6 package chinese.utility.test;
 7 
 8 import java.util.Comparator;
 9 
10 import org.junit.Assert;
11 import org.junit.Before;
12 import org.junit.Test;
13 
14 import chinese.utility.StrokeComparator;
15 
16 public class StrokeComparatorTest {
17 
18     private Comparator<String> comparator;
19 
20     @Before
21     public void setUp() {
22         comparator = new StrokeComparator();
23     }
24 
25     /**
26      * 相同笔画数
27      */
28     @Test
29     public void testCompareEquals() {
30         Assert.assertTrue(comparator.compare("", "") == 0);
31     }
32 
33     /**
34      * 不同笔画数
35      */
36     @Test
37     public void testCompare() {
38         Assert.assertTrue(comparator.compare("", "") < 0);
39         Assert.assertTrue(comparator.compare("", "") > 0);
40     }
41 
42     /**
43      * 长度不同
44      */
45     @Test
46     public void testCompareDefficultLength() {
47         Assert.assertTrue(comparator.compare("", "二一") < 0);
48     }
49 
50     /**
51      * 非汉字的比较
52      */
53     @Test
54     public void testABC() {
55         Assert.assertTrue(comparator.compare("", "a") > 0);
56         Assert.assertTrue(comparator.compare("a", "b") < 0);
57     }
58 }

笔画比较器

 1 /**
 2  * @author Jeff 
 3  * 
 4  * Copyright (c) 复制或转载本文,请保留该注释。
 5  */
 6 package chinese.utility.test;
 7 
 8 import java.util.Comparator;
 9 
10 import org.junit.Assert;
11 import org.junit.Before;
12 import org.junit.Test;
13 
14 import chinese.utility.StrokeComparator;
15 
16 public class StrokeComparatorTest {
17 
18     private Comparator<String> comparator;
19 
20     @Before
21     public void setUp() {
22         comparator = new StrokeComparator();
23     }
24 
25     /**
26      * 相同笔画数
27      */
28     @Test
29     public void testCompareEquals() {
30         Assert.assertTrue(comparator.compare("", "") == 0);
31     }
32 
33     /**
34      * 不同笔画数
35      */
36     @Test
37     public void testCompare() {
38         Assert.assertTrue(comparator.compare("", "") < 0);
39         Assert.assertTrue(comparator.compare("", "") > 0);
40     }
41 
42     /**
43      * 长度不同
44      */
45     @Test
46     public void testCompareDefficultLength() {
47         Assert.assertTrue(comparator.compare("", "二一") < 0);
48     }
49 
50     /**
51      * 非汉字的比较
52      */
53     @Test
54     public void testABC() {
55         Assert.assertTrue(comparator.compare("", "a") > 0);
56         Assert.assertTrue(comparator.compare("a", "b") < 0);
57     }
58 }

其他程序的汉字排序

  Microsoft在这方面做得比较好。如Sql server 2000,Word和Excel都能按拼音和笔画排序。而Oracle只能是采取宽松拼音排序法。

 

转载于:https://www.cnblogs.com/sjjg/p/4930025.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值