java词频统计——web版支持

需求概要:

1.把程序迁移到web平台,通过用户上传TXT的方式接收文件。

2.用户直接输入要统计的文本,服务器返回结果

3.在页面上给出链接 (如果有封皮、作者、字数、页数等信息更佳)或表格,展示经典英文小说词频统计结果;

4.支持用户自定义单词分隔符;

5.词汇范围对比(额外项)。

分析和设计:

1.创建web工程,利用servlet上传文件的技术实现用户向服务器上传文件。页面设置表单类型为enctype="multipart/form-data",创建文件上传文本框<input type="file" id="upfilename" name="upfilename" value="" />,服务器端使用Part p = request.getPart("upfilename");获取上传的文件,然后写入到指定地址即可。

2.直接分析用户post到服务器的内容,为了使用原有的api,可以将输入内容写到文件中,再进行分析。

3.页面展示统计结果

4.用户可以输入自定义的分隔符和设置显示统计结果前10行(可修改)。需要修改原词频统计的有效字符函数。

5.暂时不考虑

部分代码实现:

表单实现

     <div align="center" id="txtform">
<form action="upload" method="post" enctype="multipart/form-data">
<input type="file" id="upfilename" name="upfilename" value="" /> 自定义分隔符<input
type="text" id="splitter" name="splitter"> <input
type="submit" id="submit" value="上传" />
</form>
</div>
<div align="center" id="txtform">
<form action="wordcount" method="post">
<div align="center">待统计内容</div>
<textarea name="content" id="content"
style="width: 700px; height: 200px;"></textarea>
<br> 统计前<select id="num" name="num">
<option value="10">10</option>
<option value="20">20</option>
<option value="0">所有</option>
</select>项 <br>自定义分隔符<input type="text" id="splitter" name="splitter"> <input
type="submit" value="提交" /> <input type="button"
onclick="if(confirm('确认重置?')){reset()}" value="重置">
</form>
</div>

文件上传:

         request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
byte b[] = new byte[2048];
@SuppressWarnings("unused")
int len = 0;
Part p = request.getPart("upfilename");
if(p==null){
System.out.println("p == null");
}
String splitter = request.getParameter("splitter");
InputStream in = p.getInputStream();
String name = ""+System.currentTimeMillis();
FileWriter fr = new FileWriter("D:\\upload\\" + name+".txt");
while ((len = in.read(b)) > 0) {
fr.write(new String(b));
}
fr.close();
out.println("uploaded");
response.sendRedirect("wordcount?id="+name+"&splitter"+splitter);
out.flush();
out.close();

servlet处理:

     protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String id = request.getParameter("id");
int num = 10;
String filename = "D:\\upload\\" + id + ".txt";
WordUtil wu = WordUtilFactory.getWordUtil();
long start = System.currentTimeMillis();
String splitter = request.getParameter("splitter");
wu.setSplitter(splitter);
List<String[]> result = wu.getSortedWordGroupCountBuffered(filename, splitter);
int size = result.size();
for (int i = 0; i < (size > num ? num == 0 ? size : num : size); i++) {
String[] strs = result.get(i);
out.println(strs[1] + " : " + strs[0] + "<br>");
}
long end = System.currentTimeMillis();
out.println("execution time :" + (end - start) + "ms");
out.flush();
out.close();
} protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String content = request.getParameter("content");
String numStr = request.getParameter("num");
int num = 10;
if (numStr != null) {
num = Integer.parseInt(numStr);
}
WordUtil wu = WordUtilFactory.getWordUtil(); long start = System.currentTimeMillis();
String filename = "D://tmp.txt"; FileWriter fr = new FileWriter(filename);
fr.write(content);
fr.close();
String splitter = request.getParameter("splitter");
wu.setSplitter(splitter);
List<String[]> result = wu.getSortedWordGroupCountBuffered(filename, splitter);
int size = result.size();
for (int i = 0; i < (size > num ? num == 0 ? size : num : size); i++) {
String[] strs = result.get(i);
out.println(strs[1] + " : " + strs[0] + "<br>");
}
long end = System.currentTimeMillis();
out.println("execution time :" + (end - start) + "ms");
out.flush();
out.close();
}

有效字符判定(即自定义分隔符)

     public void setSplitter(String splitter) {
char[] tmp = splitter.toCharArray();
ArrayList<Character> deleted = new ArrayList<>();
for(int i=0;i<tmp.length-1;i++){
if(tmp[i]=='\\'){
char c = tmp[i+1];
if(c=='n'){
deleted.add('\n');
}
if(c=='r'){
deleted.add('\n');
}
if(c=='t'){
deleted.add('\n');
}
char[] copy = new char[tmp.length-2];
for(int j = 0;j <i;j++){
copy[j]=tmp[j];
}
for(int j=i;j<tmp.length-2;j++){
copy[j]=tmp[j+2];
}
i++;
}
}
split = new char[tmp.length+deleted.size()];
for(int i = 0;i<tmp.length;i++){
split[i]=tmp[i];
}
for(int i=tmp.length;i<split.length;i++){
split[i]=deleted.get(split.length-tmp.length-1);
}
} private int isCharacter(char ch, String splitter) {
if (split == null) {
if ((ch >= 'a' && ch <= 'z'))
return 1;
if ((ch >= 'A' && ch <= 'Z'))
return 1;
if (ch >= '0' && ch <= '9')
return 2;
return 0;
}
if (split.equals("")) {
if ((ch >= 'a' && ch <= 'z'))
return 1;
if ((ch >= 'A' && ch <= 'Z'))
return 1;
if (ch >= '0' && ch <= '9')
return 2;
return 0;
}
for (int i = 0; i < split.length; i++) {
if (ch == split[i]) {
return 0;
}
}
if ((ch >= 'a' && ch <= 'z'))
return 1;
if ((ch >= 'A' && ch <= 'Z'))
return 1;
if (ch >= '0' && ch <= '9')
return 2;
return 1;
}

 运行截图:

java词频统计——web版支持

java词频统计——web版支持

java词频统计——web版支持

java词频统计——web版支持

web版工程地址:https://git.coding.net/jx8zjs/wordcount-web.git

ssh:  git@git.coding.net:jx8zjs/wordcount-web.git

console版工程地址https://coding.net/u/jx8zjs/p/wordCount/git

ssh:  git@git.coding.net:jx8zjs/wordCount.git

上一篇:Java实现的词频统计——功能改进


下一篇:图像特效——摩尔纹 moir