javaweb基于内容的搜索引擎(3)_lire后台使用

javaweb基于内容的搜索引擎(3)_lire后台使用

经过了爬图,我得到了图片,以及图片的原网址,这个时候就该处理了。

图片处理本来是一件很复杂的事,而且各种特征也很多,而我仅仅是用的lire,别人第三方开源的jar包。

这里介绍下lire:Lucene Image REtrieval

lire是一个开源的基于图片内容的java类库,它提供了一种简单地方式来检索图像和照片,这是

基于图像的颜色和纹理特征来实现的。lire基于图像的内容和特点产生了一个lucene索引。

lire有多种不同的图像纹理的生成器,比如MPEG-7 ScalableColor, ColorLayout, and EdgeHistogram,

Auto Color Correlogram, PHOG, CEDD, JCD, FCTH等等。lire的搜图也是基于索引的,先把

一些图片读取,生成索引,然后在把待检索的图片来通过比对,从而得出相似的,并且有相似度

体现。lire源码是基于 Gnu GPL license.

而我呢,后台主要也是用到了lire的生成索引的技术。通过图片的object url,来获取图片

输入流,通过输入流一个个来添加索引。

下面给出加索引的主要代码:


public String craw() throws Exception {keyvalue = URLDecoder.decode(keyvalue, "utf-8");String url = new String();url = crawUrl.getNextUrl(keyvalue);System.out.println("2222");List<String> urls = new ArrayList<String>();List<String> infos = new ArrayList<String>();List<String> homeUrls = new ArrayList<String>();Connection connJsoup = Jsoup.connect(url.toString());Document dochtml = connJsoup.timeout(20000).get();String xml = dochtml.toString();// 把xml拆开,然后提取目标urlurls = crawUrl.getObjInfo(""pic_url":"", "",", xml);infos = crawUrl.getObjInfo(""title":"", "",", xml);homeUrls = crawUrl.getObjInfo(""page_url":"", "",", xml);// 得到索引存放地址String location = ildi.getRootLocation().getValue();// 创建一个合适的文件生成器,Lire针对图像的多种属性有不同的生成器DocumentBuilder db = DocumentBuilderFactory.getJCDDocumentBuilder();IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_33,new SimpleAnalyzer(Version.LUCENE_33));IndexWriter iw = new IndexWriter(FSDirectory.open(new File(location)),iwc);int num = 0;// 建立索引,只建立前二十个for (int i = 1; i < urls.size(); i++, num++) {// 以下为建立索引的过程java.net.URL myurl = new java.net.URL(urls.get(i));java.net.HttpURLConnection conn = (java.net.HttpURLConnection) myurl.openConnection();conn.setRequestMethod("GET");conn.setConnectTimeout(5 * 1000);InputStream inStream;try {inStream = conn.getInputStream();// 通过输入流获取图片数据} catch (Exception e) {continue;}// 以下为插入图片过程Picture picture = new Picture();List<Integer> listKey = new ArrayList<Integer>();int j = 0;for (int k = 0; k < pdi.getAllPicture().size(); k++) {listKey.add(pdi.getAllPicture().get(k).getId());}for (int k = 0; k < 1000000; k++) {if (!listKey.contains(k)) {j = k;break;}}picture.setId(j);picture.setKeyWord(kwdi.getByValue(keyvalue));picture.setName(urls.get(i));picture.setDescription(infos.get(i));picture.setObjurl(homeUrls.get(i));if (num > 20) {break;}pdi.addPicture(picture);// 将图片存为docorg.apache.lucene.document.Document doc = db.createDocument(inStream, urls.get(i));iw.addDocument(doc);inStream.close();}// 将标记设为已建立索引kwdi.updateIsAdd(keyvalue);iw.optimize();iw.close();HttpServletResponse response = ServletActionContext.getResponse();// 设置字符集response.setCharacterEncoding("UTF-8");PrintWriter out = response.getWriter();out.print("1");return null;}
 
<span ></span>索引建立后的文件:

免责声明:本网信息来自于互联网,目的在于传递更多信息,并不代表本网赞同其观点。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,并请自行核实相关内容。本站不承担此类作品侵权行为的直接责任及连带责任。如若本网有任何内容侵犯您的权益,请及时联系我们,本站将会在24小时内处理完毕。
相关文章
返回顶部