可能大家都碰到过类似的问题 比如 “我国信息化工程发展迅速。”这句话中,出现了信息化 和 化工嵌套的关键词,那么在加链接的时候如何处理?当然不可能两个都加,只能加一个的情况下,只能通过给文档定义类型,根据类型进行关键词的类型指派。我这里实现了根据关键词的顺序进行替换,谁早先换谁。呵呵
关键词实体
public class mainkey {
private String name = "";
private String url = "";
public mainkey(String temp_name ,String temp_url) {
name = temp_name;
url = temp_url;
}
public void setName(String temp_name)
{
name = temp_name;
}
//
public String getName()
{
return name;
}
public void setUrl(String temp_url)
{
url = temp_url;
}
public String getUrl()
{
return url;
}
}
内容位置匹配
public class station {
private int starti;
private int endi;
private int length;
private mainkey key;
private int typei;
public station(int starti,int endi,int length,mainkey key) {
this.starti = starti;
this.endi = endi;
this.length = length;
this.key = key;
this.typei = 1;
}
public void setStarti(int temp)
{
starti = temp;
}
public int getStarti()
{
return starti;
}
public void setEndi(int temp)
{
endi = temp;
}
public int getEndi()
{
return endi;
}
public void setLength(int temp)
{
length = temp;
}
public int getLength()
{
return length;
}
public void setKey(mainkey key)
{
this.key = key;
}
public mainkey getKey()
{
return key;
}
public void setTypei(int temp)
{
typei = temp;
}
public int getTypei()
{
return typei;
}
}
具体替换
public class change {
public change() {
}
public static void main(String args[])
{
change ch = new change();
System.out.println(ch.changeStr("我国信息化工程发展迅速,电子化程度很高,国家大力提倡信息化。"));
}
public String changeStr(String cs)
{
//key
java.util.ArrayList keylist = new java.util.ArrayList();
keylist.add(new mainkey("电子","dianzi"));
keylist.add(new mainkey("信息化","xinxihua"));
keylist.add(new mainkey("国家","guojia"));
keylist.add(new mainkey("化工","huagong"));
//station
java.util.ArrayList stationlist = new java.util.ArrayList();
int keyi = 0;
for (int i=0;i<keylist.size();i++)
{
mainkey tempkey = (mainkey)keylist.get(i);
int length = tempkey.getName().length();
int starti = cs.indexOf(tempkey.getName());
int endi = starti+length-1;
station tempstation = null;
if (starti >0)
{
tempstation = new station(starti,endi,length,tempkey);
stationlist.add(tempstation);
}
}
//change
this.BubbleSort(stationlist);
//开始比较
for (int i=0;i<stationlist.size()-1;i++)
{
if ((((station)stationlist.get(i)).getStarti())<=(((station)stationlist.get(i+1)).getEndi()))
{
((station)stationlist.get(i)).setTypei(0);
}
}
//
//开始替换
for (int i=0;i<stationlist.size();i++)
{
station ss = ((station)stationlist.get(i));
if (ss.getTypei() == 1)
{
mainkey key = ss.getKey();
cs = cs.replaceAll(key.getName(),"<"+key.getUrl()+">"+key.getName()+"</"+key.getUrl()+">");
}
}
return cs;
}
void BubbleSort(java.util.ArrayList changelist)
{ //R(l..n)是待排序的文件,采用自下向上扫描,对R做冒泡排序
int i,j;
int n = changelist.size()-1;
boolean exchange; //交换标志
for(i=0;i<n;i++){ //最多做n-1趟排序
exchange=false; //本趟排序开始前,交换标志应为假
for(j=n-1;j>=i;j--) //对当前无序区R[i..n]自下向上扫描
if(((station)changelist.get(j+1)).getStarti()>((station)changelist.get(j)).getStarti()){//交换记录
Object tempstation = changelist.get(j+1);
changelist.set(j+1,changelist.get(j));
changelist.set(j,tempstation);
exchange=true; //发生了交换,故将交换标志置为真
}
if(!exchange) //本趟排序未发生交换,提前终止算法
return;
} //endfor(外循环)
}
}