wiki上关于KShingling Algorithm(w-shingling)的说明:

http://en.wikipedia.org/wiki/W-shingling

摘要:

In natural language processing a w-shingling is a set of unique "shingles"—contiguous subsequences of tokens in a document—that can be used to gauge the similarity of two documents. The w denotes the number of tokens in each shingle in the set.

The document, "a rose is a rose is a rose" can be tokenized as follows:

(a,rose,is,a,rose,is,a,rose)

The set of all contiguous sequences of 4 tokens (N-grams, here: 4-grams) is

{ (a,rose,is,a), (rose,is,a,rose), (is,a,rose,is), (a,rose,is,a), (rose,is,a,rose) } = { (a,rose,is,a), (rose,is,a,rose), (is,a,rose,is) }

我理解的此算法,是把每段文本都像上述分解后,统计两段文本的合集b,再统计交集a,用a/b得到相似度。

写得有些复杂:

 package bigproject2;

 import javax.swing.JOptionPane;

 public class union {
//求子集
public String[] ziji(String str)
{
char[] ch=str.toCharArray();
int c=0;
for(int i=0;i<ch.length;i++)
{
if(ch[i]==' ')
c++;
}
//建立单词数组
String[] strt=new String[c+1];
for(int i=0;i<c+1;i++)
strt[i]="";
int h=0;
for(int i=0;i<c+1;i++)
{
for(int j=h;j<ch.length;j++)
{
if(ch[j]==' ')
{
h=j+1;
break;
}
else strt[i]+=ch[j];
}
}
return strt;
}
//按k分,并去掉重复子集。
public String[] cut(String[] str,int k) throws MyException{
if(str.length<k)
throw new MyException("单词数少于"+k+",无法进行计算!");
String[] t=new String[str.length-k+1];
for(int i=0;i<str.length-k+1;i++)
t[i]="";
int h=0,m=0;
for(;h<str.length-k+1;h++)
{
for(int i=m;i<m+k;i++)
t[h]+=str[i];
m++;
}
//去掉重复部分
int merge=0;
for(int i=0;i<t.length-1;i++)
{
if(t[i].equals("")) break;
for(int j=i+1;j<t.length;j++)
{
if(t[i].equals(t[j]))
{
merge++;
int y=j;
for(;y<t.length-1;y++)
{
t[y]=t[y+1];
}
t[y]="";
}
}
}
String[] fin=new String[t.length-merge];
for(int i=0;i<t.length-merge;i++)
fin[i]=t[i];
return fin;
}
public class MyException extends Exception{
public MyException(String str){
JOptionPane.showMessageDialog(null, str,"警告", JOptionPane.INFORMATION_MESSAGE);
}
}
//求两字符串数组合集个数。
public int heji(String[] a,String[] b){
int count=a.length+b.length;
for(int i=0;i<a.length;i++)
{
for(int j=0;j<b.length;j++)
{
if(a[i].equals(b[j]))
count--;
}
}
return count;
}
//求两字符串数组交集个数。
public int jiaoji(String[] a,String[] b){
int count=0;
for(int i=0;i<a.length;i++)
{
for(int j=0;j<b.length;j++)
{
if(a[i].equals(b[j]))
count++;
}
}
return count;
} }
 package bigproject2;

 public class KShinglingAlgorithm extends union{
private String text1,text2;
public String getText1()
{
return text1;
}
public String getText2()
{
return text2;
}
public void setText1(String text1)
{
this.text1=text1;
}
public void setText2(String text2)
{
this.text2=text2;
} public float getSimilarity(int k)
{
union a=new union();
String[] t1=a.ziji(this.text1);
String[] t2=a.ziji(this.text2);
String[] t1t,t2t;
try{
t1t=a.cut(t1, k);
t2t=a.cut(t2, k); }catch(MyException e){
return -1;
}
int he=a.heji(t1t, t2t);
int jiao=a.jiaoji(t1t, t2t);
return (float)jiao/he;
} }

面板设计部分:

 package bigproject2;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader; import javax.swing.*;
import javax.swing.event.*;
import javax.swing.filechooser.FileNameExtensionFilter; public class Outlook extends JFrame{
JFrame frm=new JFrame("相似度计算器");
JPanel areabottom=new JPanel();
JPanel areatop=new JPanel();
JPanel areamiddle=new JPanel();
static JTextArea tl=new JTextArea();
static JTextArea tr=new JTextArea();
JScrollPane left=new JScrollPane(tl,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JScrollPane right=new JScrollPane(tr,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JSplitPane sp=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,left,right);
static JButton toBig=new JButton("全部大写");
static JButton delbd=new JButton("去掉标点");
static JButton count=new JButton("计算相似度");
JLabel space=new JLabel(" ");
JLabel t1=new JLabel("Text1");
JLabel t2=new JLabel("Text2"); JMenuBar mb=new JMenuBar();
JMenu open=new JMenu("打开");
JMenuItem opent1=new JMenuItem("打开到Text1");
JMenuItem opent2=new JMenuItem("打开到Text2"); private String str="";
public Outlook()
{
judge(); frm.setVisible(true);
frm.setBounds(50, 50, 500, 400);
frm.setLayout(new BorderLayout(5,5)); frm.add("North",areatop);
frm.add("Center",areamiddle);
frm.add("South",areabottom); areatop.add(mb);
mb.add(open);
open.add(opent1);
open.add(opent2);
open.setPreferredSize(new Dimension(40,18));
mb.setBackground(frm.getBackground());
areatop.setLayout(new FlowLayout(FlowLayout.LEFT));
areamiddle.setLayout(new FlowLayout(FlowLayout.LEFT)); areamiddle.add(t1);
t1.setPreferredSize(new Dimension(frm.getWidth()/2-20,10));
areamiddle.add(t2);
t2.setPreferredSize(new Dimension(50,10));
areamiddle.add(left);
left.setPreferredSize(new Dimension(frm.getWidth()/2-20,frm.getHeight()/2));
areamiddle.add(right);
right.setPreferredSize(new Dimension(frm.getWidth()/2-20,frm.getHeight()/2));
tl.setLineWrap(true);
tr.setLineWrap(true); areabottom.add(toBig);
areabottom.add(delbd);
areabottom.add(space);
areabottom.add(count); opent1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try {
openfile();
tl.setText(str);
} catch (IOException e1) {
e1.printStackTrace();
}
judge();
}
});
opent2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try {
openfile();
tr.setText(str);
} catch (IOException e1) {
e1.printStackTrace();
}
judge();
}
});
toBig.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tl.setText(tobig(tl.getText()));
tr.setText(tobig(tr.getText()));
}
}); delbd.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tl.setText(del(tl.getText()));
tr.setText(del(tr.getText()));
judge();
} });
count.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
KShinglingAlgorithm a=new KShinglingAlgorithm();
a.setText1(tl.getText());
a.setText2(tr.getText());
float b=a.getSimilarity(4);
if(b!=-1)
JOptionPane.showMessageDialog(null, Float.toString(b),"相似度", JOptionPane.INFORMATION_MESSAGE);
}
});
tr.addKeyListener(new KeyAdapter(){
public void keyTyped(KeyEvent e){
judge();
}
});
tl.addKeyListener(new KeyAdapter(){
public void keyTyped(KeyEvent e){
judge();
}
});
}
public void judge(){
if(tl.getText().length()!=0||tr.getText().length()!=0) {
toBig.setEnabled(true);
delbd.setEnabled(true);
count.setEnabled(true);
}
else{
toBig.setEnabled(false);
delbd.setEnabled(false);
count.setEnabled(false);
}
}
public void openfile() throws IOException{
str="";
JFileChooser choose=new JFileChooser();
int result = choose.showOpenDialog(this);
File file = null; //注意初始化
//加过滤器
if (result == JFileChooser.APPROVE_OPTION) {
file = choose.getSelectedFile();
}
else{
return; //使点取消后不会抛出异常
}
FileReader fr=new FileReader(file);
BufferedReader br=new BufferedReader(fr);
char c[]=new char[512];
String strline="";
while(br.ready()){
strline=br.readLine();
str+=strline;
};
br.close();
fr.close();
}
public String tobig(String str){
String temp="";
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)>='a'&&str.charAt(i)<='z')
{
char t=str.charAt(i);
t=(char)(str.charAt(i)-32);
temp+=t;
}
else temp+=str.charAt(i);
}
return temp;
} public String del(String str){
String temp="";
for(int i=0;i<str.length();i++)
{
char t=str.charAt(i);
if(t>='!'&&t<='/'||t>=58&&t<=64||t>=91&&t<=96||t>=123&&t<=126);
else temp+=t;
}
return temp;
}
public static void main(String[] args){
new Outlook(); }
}

Outlook

java大作业 KShinglingAlgorithm的更多相关文章

  1. JAVA大作业汇总1

    JAVA大作业 代码 ``` package thegreatwork; import javafx.application.; import javafx.scene.control.; impor ...

  2. JAVA大作业汇总2

    JAVA大作业2 代码 package thegreatwork; //Enum一般用来表示一组相同类型的常量,这里用于表示运动方向的枚举型常量,每个方向对象包括方向向量. public enum D ...

  3. JAVA大作业汇总3

    JAVA大作业3 代码 ``` package thegreatwork; import java.util.; import java.io.; /Board.java 目的:里面有一些关于如何移动 ...

  4. < JAVA - 大作业(2)仿qq即时通讯软件 >

    < JAVA - 大作业(2)仿qq即时通讯软件 > 背景 JAVA上机大作业:设计一个仿qq即时通讯软件 任务简要叙述:设计一款仿QQ的个人用户即时通讯软件,能够实现注册,登陆,与好友聊 ...

  5. java大作业博客--购物车

    Java 大作业----使用MySQL的购物车 一.团队介绍 姓名 任务 李天明.康友煌 GUI设计及代码编写 谢晓淞 业务代码编写.MySQL服务器平台部署.git代码库 严威 类和包的结构关系设计 ...

  6. <JAVA - 大作业(1)文本编辑器 >

    <JAVA - 大作业(1)文本编辑器 > 背景 JAVA上机大作业:qq / 代码评价系统 第一次上机主题是练习JAVA自带的GUI图形化编程 目的:实现一个跟window10记事本界面 ...

  7. 期末Java Web大作业----简易的学生管理系统

    学生信息管理系统(大作业) 2018-12-21:此文章已在我的网站更新,添加视图介绍等信息,源码请移步下载https://www.jeson.xin/javaweb-sims.html PS:首先不 ...

  8. Java Web大作业——编程导航系统

    title: Java Web大作业--编程导航系统 categories: - - 计算机科学 - Java abbrlink: 40bc48a1 date: 2021-12-29 00:37:35 ...

  9. 最课程阶段大作业之01:使用SVN实现版本控制

    版本控制在友军那里都是放在整个培训的最后阶段才开始讲的,但我们打算放到SE阶段.与其匆匆在项目实战阶段弄个半生不熟,然后进入实际工作中接受他人对你的怀疑,不如……早死早超生~~~. 可是,我们毕竟现在 ...

随机推荐

  1. Windows下Oracle服务介绍

    如图,截取的是11gR2下RAC其中一个节点的Oracle服务列表. oracle在处理一般事务时并不需要全部启动其后台的所有服务由于oracle服务所占用系统资源比较大,一般情况下,对于单实例的OR ...

  2. BZOJ 1927: [Sdoi2010]星际竞速(最小费用最大流)

    拆点,费用流... ----------------------------------------------------------------------------- #include< ...

  3. comparable与comparator比较

    两种比较接口分析 前者应该比较固定,和一个具体类相绑定,而后者比较灵活,它可以被用于各个需要比较功能的类使用. 一个类实现了 Camparable 接口表明这个类的对象之间是可以相互比较的.如果用数学 ...

  4. AWT和Swing

    布局分类 一.流式布局 二.边界布局 三.网格布局 四.卡片布局 五.坐标式布局 随意布置控件位置. 六.混合布局

  5. HDU1257-最少拦截系统

    描述: 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高度.某天,雷达捕捉到敌国的导弹来 ...

  6. 自定义jquery表格插件

    以前一直都是再用easyui插件来实现各种功能,但是easyui太过于庞大,使用越多对服务器负载影响越大. 基于此,在模仿easyui的dataGrid表格插件的同时,自己去封装了一个.实现了基本的j ...

  7. 高质量程序设计指南C/C++语言——内存管理

    • free()和delete只是把指针所指的内容给释放掉,并没有把指针本身删掉.指针被free()或delete以后其地址仍然不变(不等于NULL),只是该地址对应的内存是垃圾——p成了野指针.如果 ...

  8. (IOS)关于Xcode的架构(Architectures)设置

    首先来了解一下Architectures中几个参数的含义 ARMv6:ARM11内核用于iPhone2G和iPhone3G中的架构 ARMv7:modern ARM内核用于iPhone3GS和iPho ...

  9. css 定义hr的几种样式

    <style type="text/css"> <!-- .hr0{ height:1px;border:none;border-top:1px dashed # ...

  10. 解读ECMAScript 6箭头函数

    箭头函数是ECMAScript 6最受关注的更新内容之一.它引入了一种用「箭头」(=>)来定义函数的新语法,它…它碉堡了~.箭头函数与传统的JavaScript函数主要区别在于以下几点: 对 t ...