/ictclas4j/src/org/ictclas4j/bean/WordFingerprint.java

http://ictclas4j.googlecode.com/ · Java · 142 lines · 102 code · 25 blank · 15 comment · 17 complexity · dd128d4ccf60b4e9a42736695bb6eb8b MD5 · raw file

  1. package org.ictclas4j.bean;
  2. import java.io.DataInputStream;
  3. import java.io.DataOutputStream;
  4. import java.io.IOException;
  5. /**
  6. * ??????
  7. *
  8. * @author sinboy
  9. * @since 2008.7.5
  10. *
  11. */
  12. public class WordFingerprint {
  13. private String word;
  14. // ????
  15. private String[] pinyin;
  16. // ??????????????????->4425134
  17. private String fingerprint;
  18. public String getWord() {
  19. return word;
  20. }
  21. public void setWord(String word) {
  22. this.word = word;
  23. }
  24. public String[] getPinyin() {
  25. return pinyin;
  26. }
  27. public void setPinyin(String[] pinyin) {
  28. this.pinyin = pinyin;
  29. }
  30. public String getFingerprint() {
  31. return fingerprint;
  32. }
  33. public void setFingerprint(String fingerprint) {
  34. this.fingerprint = fingerprint;
  35. }
  36. public String toString() {
  37. StringBuffer sb = new StringBuffer();
  38. sb.append(word).append(",");
  39. sb.append(fingerprint).append(",");
  40. for (int i = 0; i < pinyin.length; i++) {
  41. sb.append(pinyin[i]);
  42. if (i != pinyin.length - 1)
  43. sb.append("/");
  44. }
  45. return sb.toString();
  46. }
  47. public int read(DataInputStream in, long offset) throws IOException {
  48. int size = 0;
  49. if (in != null) {
  50. in.skip(offset);
  51. // read word
  52. int wordLen = in.readByte();
  53. byte[] bword = new byte[wordLen];
  54. in.read(bword);
  55. size += 1 + wordLen;
  56. word = new String(bword);
  57. // read fingerprint
  58. int fpLen = in.readByte();
  59. byte[] bfp = new byte[fpLen];
  60. in.read(bfp);
  61. size += 1 + fpLen;
  62. fingerprint = new String(bfp);
  63. // read pinyin
  64. int pinyinCount = in.readByte();
  65. size++;
  66. pinyin = new String[pinyinCount];
  67. for (int i = 0; i < pinyinCount; i++) {
  68. int pinyinLen = in.readByte();
  69. byte[] bpinyin = new byte[pinyinLen];
  70. in.read(bpinyin);
  71. pinyin[i] = new String(bpinyin);
  72. size += 1 + pinyinLen;
  73. }
  74. }
  75. return size;
  76. }
  77. public int write(DataOutputStream out) throws IOException {
  78. int size = 0;
  79. if (out != null) {
  80. // write word
  81. int wordLen = word != null ? word.getBytes().length : 0;
  82. out.writeByte((byte) wordLen);
  83. size++;
  84. if (wordLen > 0) {
  85. byte[] b = word.getBytes();
  86. out.write(b);
  87. size += wordLen;
  88. }
  89. // write fingerprint
  90. int fpLen = fingerprint != null ? fingerprint.getBytes().length : 0;
  91. out.writeByte((byte) fpLen);
  92. size++;
  93. if (fpLen > 0) {
  94. byte[] b = fingerprint.getBytes();
  95. out.write(b);
  96. size += fpLen;
  97. }
  98. // write pinyin
  99. int pinyinCount = pinyin != null ? pinyin.length : 0;
  100. out.writeByte((byte) pinyinCount);
  101. size++;
  102. if (pinyinCount > 0) {
  103. for (int i = 0; i < pinyinCount; i++) {
  104. int pinyinLen = pinyin[i] != null ? pinyin[i].getBytes().length : 0;
  105. out.writeByte(pinyinLen);
  106. size++;
  107. if (pinyinLen > 0) {
  108. byte[] b = pinyin[i].getBytes();
  109. out.write(b);
  110. size += pinyinLen;
  111. }
  112. }
  113. }
  114. }
  115. return size;
  116. }
  117. }