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. Swift 中类的初始化器与继承

    首先,Swift 为类定义了两种初始化器来确保类中所有的储存属性都能得到一个初始化值.这两种初始化器就是「指定初始化器」(Designated Initializer)与「便利初始化器」(Conven ...

  2. [翻译]Go语言调度器

    Go语言调度器 译序 本文翻译 Daniel Morsing 的博文 The Go scheduler.个人认为这篇文章把Go Routine和调度器的知识讲的浅显易懂.作为一篇介绍性的文章.非常不错 ...

  3. HDU3415:Max Sum of Max-K-sub-sequence(单调队列)

    Problem Description Given a circle sequence A[1],A[2],A[3]......A[n]. Circle sequence means the left ...

  4. ORA-600 [kcratr_scan_lastbwr] 逻辑坏块

    数据库版本: 11.2.0.3 问题现象: 今天在启动一台测试数据库的时候,发现db不能open,报错如下: ERROR at line 1:ORA-00600: internal error cod ...

  5. DevExpress ASP.NET 使用经验谈(1)-XPO模型的创建

    这个系列通过一些简单例子循序渐进,介绍DevExpress ASP.NET控件的使用.先来介绍一下XPO的使用,安装的DevExpress版本为DXperienceUniversal-12.2.4,使 ...

  6. 对Devexpress ASP.NET组件的一些看法

    使用.net开发的应该都熟悉DevExpress这套组件,强大的功能,显著提高开发效率和提升用户体验. 不过好像大都用winform, 说起用asp.net组件来开发webform,很多人开口就说慢, ...

  7. android http同步请求

    1.界面 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:too ...

  8. 【iOS】Mapkit的使用:地图显示、定位、大头针、气泡等

    转自:http://blog.csdn.net/dolacmeng/article/details/46594839 以前做项目用高德地图SDK,需要注册账号和AppID,然后下载SDK集成到项目中, ...

  9. ExtJS003单击按钮弹出window

    html部分 <input type="button" id="btn" name="name" value="点击&quo ...

  10. IWebBrowser隐藏滚动条

    刚才在项目里看到一个IWebBrowser2,竟然需要通过MoveWindow的方式把滚动条遮挡,如果要缩小IWebBrowser2控件的显示大小呢?这种方法至少我用不习惯,起码也得从源头解决这样的问 ...