/ictclas4j/src/org/ictclas4j/util/DebugUtil.java

http://ictclas4j.googlecode.com/ · Java · 131 lines · 91 code · 16 blank · 24 comment · 19 complexity · c1097cfb04d0157befbf391bb023dd19 MD5 · raw file

  1. package org.ictclas4j.util;
  2. import java.io.File;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.io.PrintWriter;
  6. import java.util.ArrayList;
  7. import org.ictclas4j.bean.AdjoiningPos;
  8. import org.ictclas4j.bean.DebugResult;
  9. import org.ictclas4j.bean.SegNode;
  10. import com.gftech.util.GFFinal;
  11. /**
  12. * ???????
  13. *
  14. * @author sinboy
  15. *
  16. */
  17. public class DebugUtil {
  18. /**
  19. * ????????????????HMTL???
  20. *
  21. * @param snList
  22. */
  23. public static void output2html(DebugResult dr) {
  24. if (dr != null) {
  25. try {
  26. String html = "<html><head><title>Eve????</title></head>";
  27. html += "<body bgcolor=\"#CCFF99\">";
  28. html += dr.toHTML();
  29. html += "</body></html>";
  30. writeTxtFile("output"+GFFinal.FILE_SEP+"sr.html", html, false);
  31. } catch (IOException e) {
  32. }
  33. }
  34. }
  35. public static void outputPostag(ArrayList<SegNode> sns) {
  36. if (sns != null) {
  37. try {
  38. StringBuffer html = new StringBuffer();
  39. html.append("<html><head><title>Eve????</title></head>");
  40. html.append("<body bgcolor=\"#CCFF99\">");
  41. html.append("<p>???????????");
  42. html.append("<table border=\"1\" width=\"100%\">");
  43. for (SegNode sn : sns) {
  44. html.append("<tr>");
  45. html.append("<td width=\"10%\" bgcolor=\"#99CCFF\" rowspan=\"" + sn.getPosSize() + "\">"
  46. + sn.getWord() + "</td>");
  47. ArrayList<AdjoiningPos> allPos = sn.getAllPos();
  48. boolean flag = false;
  49. for (AdjoiningPos pos : allPos) {
  50. if (flag)
  51. html.append("<tr>");
  52. html.append("<td width=\"20%\" >" + pos.getPos() + "</td>");
  53. html.append("<td width=\"20%\" >" + pos.getValue() + "</td>");
  54. html.append("<td width=\"20%\" >" + pos.getPrev() + "</td>");
  55. String sBest=pos.isBest()?"true":"&nbsp";
  56. html.append("<td width=\"20%\" >" + sBest + "</td>");
  57. html.append("</tr>");
  58. if (!flag)
  59. flag = true;
  60. }
  61. }
  62. html.append("</table>");
  63. html.append("</body></html>");
  64. writeTxtFile("output"+GFFinal.FILE_SEP+"postag.html", html.toString(), false);
  65. } catch (IOException e) {
  66. }
  67. }
  68. }
  69. /**
  70. * ?????.???????????"\n"??,??????????
  71. *
  72. * @param fileName
  73. * ????
  74. * @param txt
  75. * ????????
  76. * @param isAppend
  77. * ??????????
  78. * @return
  79. * @throws IOException
  80. */
  81. public static boolean writeTxtFile(String fileName, String txt, boolean isAppend) throws IOException {
  82. FileWriter fw = null;
  83. PrintWriter out = null;
  84. if (fileName != null && txt != null)
  85. try {
  86. String parent;
  87. File fp;
  88. File file = new File(fileName);
  89. // ????????????????????????????
  90. if (!file.exists()) {
  91. parent = file.getParent();
  92. if (parent != null) {
  93. fp = new File(parent);
  94. if (!fp.isDirectory())
  95. fp.mkdirs();
  96. }
  97. }
  98. String[] msgs = txt.split("\n");
  99. fw = new FileWriter(file, isAppend);
  100. out = new PrintWriter(fw);
  101. for (int i = 0; i < msgs.length; i++) {
  102. out.println(msgs[i]);
  103. }
  104. out.flush();
  105. out.close();
  106. return true;
  107. } catch (IOException e) {
  108. throw new IOException();
  109. } finally {
  110. if (out != null)
  111. out.close();
  112. }
  113. return false;
  114. }
  115. }