lucene 3.0.2 + 多文件夹微博数据(时间,微博)构建索引
package lia.meetlucene; import java.io.File;
import java.io.IOException;
import java.util.LinkedList; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
//import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList; public class Unicode1 { static boolean numTime = false;
static boolean numText = false;
static String timeTmp = null;
static String textTmp = null; static void indexer(IndexWriter writer) throws CorruptIndexException, IOException
{
org.apache.lucene.document.Document doc = new org.apache.lucene.document.Document();
Field field = new Field("context",textTmp,Field.Store.YES,
Field.Index.ANALYZED);
doc.add(field); field = new Field("time",timeTmp,Field.Store.YES,
Field.Index.NOT_ANALYZED);
doc.add(field); writer.addDocument(doc);
//System.out.println("微博: " + textTmp+ " "+timeTmp);
} static void Dfs(NodeList nodecur,IndexWriter writer) {
for (int j = 0; j < nodecur.getLength(); j++) { if ("timestamp".equals(nodecur.item(j).getNodeName())) // 输出pass
{
//System.out.println("时间: " + nodecur.item(j).getTextContent());
timeTmp = nodecur.item(j).getTextContent();
numTime = true;
}
/*
* else if ("origtext".equals(nodecur.item(j).getNodeName()))
* System.out.println("原微博: " + nodecur.item(j).getTextContent());
*/
else if ("text".equals(nodecur.item(j).getNodeName())) // 输出code
{
//System.out.println("微博: " + nodecur.item(j).getTextContent());
textTmp = nodecur.item(j).getTextContent();
numText = true;
}
if(numText&&numTime)
try {
indexer(writer);
numText = false;
numTime = false;
} catch (CorruptIndexException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} NodeList childNodes = nodecur.item(j).getChildNodes();
Dfs(childNodes,writer);
}
} public static void main(String[] args) throws IOException { long a = System.currentTimeMillis(); // String dataDir ="C:/Users/Administrator/Desktop/xdj/tengxun/A__Vae";
//File dataDir = new File("C:/Users/Administrator/Desktop/xdj/tengxun");
//String indexDir = "C:/Users/Administrator/Desktop/xdj/suoyin";
File dataDir = new File("E:/xdj/tengxun");
String indexDir = "E:/xdj/tengxunsuoying";
Directory dir = FSDirectory.open(new File(indexDir)); IndexWriter writer = new IndexWriter(dir,
new SmartChineseAnalyzer(Version.LUCENE_20),
//new StandardAnalyzer(Version.LUCENE_30),
true,
IndexWriter.MaxFieldLength.UNLIMITED); LinkedList list = new LinkedList();
File file[] = dataDir.listFiles();
for (int i = 0; i < file.length; i++) {
if (file[i].isDirectory())
list.add(file[i]);
}
File tmp;
int num = 0;
while (!list.isEmpty()) { tmp = (File) list.removeFirst();
file = tmp.listFiles();
for (int i = 0; i < file.length; i++) {
System.out.println(file[i].getAbsolutePath()); Element element = null;
// documentBuilder为抽象不能直接实例化(将XML文件转换为DOM文件)
DocumentBuilder db = null;
DocumentBuilderFactory dbf = null;
try {
// 返回documentBuilderFactory对象
dbf = DocumentBuilderFactory.newInstance();
// 返回db对象用documentBuilderFatory对象获得返回documentBuildr对象
db = dbf.newDocumentBuilder();
// 得到一个DOM并返回给document对象
Document dt = db.parse(file[i]);
// 得到一个elment根元素
element = dt.getDocumentElement();
// 获得根节点
System.out.println("根元素:" + element.getNodeName());
// 获得根元素下的子节点 Dfs(element.getChildNodes(),writer); num++;
} catch (Exception e) {
e.printStackTrace();
}
} System.out.println(System.currentTimeMillis() - a + " " + num);
}
writer.close(); }
}
lucene 3.0.2 + 多文件夹微博数据(时间,微博)构建索引的更多相关文章
- Cocos2d-x 3.0新引擎文件夹结构
Cocos2d-x 3.0新引擎文件夹结构 2014年4月29日 Cocos2d-x 3.0学习 作为一个Cocos2d-x的菜鸟,我倒是挺愿意关注不同版本号之间的差别,Cocos2d-x 3.0自 ...
- matrix-gui-2.0 将javascript文件夹改成js文件夹
/******************************************************************************** * matrix-gui-2.0 将 ...
- Tomcat7.0设置虚拟文件夹
(1)眼下,我们的网站网站都是放在默认的文件夹下:tomcat/webapps/下的.可是,在某种情况下.我们须要把网站放到其它的文件夹,比方:tomcat所在磁盘的空间不足: 或者为了项目的统一管理 ...
- Mysql5.7和8.0版本的文件夹版安装教程(整合版,超详细)
安装Mysql(5.7版本) 下载地址在这里可以自选版本,找到合适的版本进行下载 解压安装包 配置环境变量 win+r 输入 sysdm.cpl 点击高级 点击环境变量 新建一个系统变量 变量名为MY ...
- c#创建目录和文件夹,数据写入并生成txt文件
c#创建目录: // 获取程序的基目录.System.AppDomain.CurrentDomain.BaseDirectory // 获取模块的完整路径.System.Diagnostics.Pro ...
- bat如何实现自动创建文件夹(以当前时间命名)
先比较直接的查看当前的日期和时间:(或者cmd中直接输入date,time查看) @echo off color 0a set dt=%date%%time% echo %dt% pause 1.使用 ...
- python 检测文件夹的数据变动
from watchdog.observers import Observerfrom watchdog.events import *import time class FileEventHandl ...
- Hadoop MultipleOutputs 结果输出到多个文件夹 出现数据不全,部分文件为空
如题:出现下图中的情况(设置reduceNum=5) 感觉很奇怪,排除了很久,终于发现是一个第二次犯的错误:丢了这句 this.mOutputs.close(); 加上这句,一切恢复正常!
- python os 命令,及判断文件夹是否存在
使用前 import os导入模块 os模块: os.sep 可以取代操作系统特定的路径分割符 os.linesep 字符串给出当前平台使用的行终止符.例如,Windows使用'\r\n ...
随机推荐
- Android 中4种屏幕尺寸
具体信息,请参考 Android 官方文档 Supporting Multiple Screens small(屏幕尺寸小于3英寸左右的布局), normal(屏幕尺寸小于4.5英寸左右), lar ...
- php的socket通信(二)
案例一:代码详解 // 设置一些基本的变量$host = "192.168.1.99";$port = 1234;// 设置超时时间set_time_limit(0);// 创建一 ...
- Servlet过滤器和监听器
1,Servlet过滤器 <filter> <filter-name>charset</filter-name> <filter-class>org.g ...
- 队列——解密QQ号
队列——解密QQ号 --转自啊哈磊[坐在马桶上看算法]算法4:队列——解密QQ号 新学期开始了,小哈是小哼的新同桌(小哈是个小美女哦~),小哼向小哈询问QQ号,小哈当然不会直接告诉小哼啦,原因嘛你懂的 ...
- ANT 环境搭建
一.ant的下载 ant是Apache的一个项目(http://ant.apache.org/),目前的最新版是1.8.4(http://ant.apache.org/bindownload.cg ...
- 局域网通过ip查mac地址、通过mac地址查ip方法
sh-4.1# which arp #linux主机A /sbin/arp sh-4.1# arp -a 192.168.1.10 #主机B的IP bogon (:8t:8p::: [ether] o ...
- undefined reference to libiconv_open'
ext/iconv/.libs/iconv.o: In function `php_iconv_stream_filter_ctor': /home/king/php-5.2.13/ext/iconv ...
- js 带省略号的分页源码及应用实例
一.js:pagination.js /*--说明分页div id为:changpage*/var eachPageDataNum = 10;//每页显示记录数var nowPage = 1;//当前 ...
- asp.net mvc int[] 和 string[] 自定义数组绑定
新建类,int[]数组模型绑定 using System; using System.Collections.Generic; using System.Linq; using System.Web; ...
- JUC系列回顾之-CountDownLatch底层原理和示例
CountDownLatch 是一个同步工具类,允许一个线程或者多个线程等待其他线程完成操作,再执行. CountDownLatch(int count) 构造一个用给定计数初始化的 CountDow ...