PageRenderTime 18ms CodeModel.GetById 5ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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