Twitter 如何使用lucene从推文中提取日期?
我有一些tweet,我已经为“TweetIndexer”编制了索引,lucene知道每条tweet都包含一个ID、用户、文本和日期 我只想用另一个类“TweetSearcher”检索日期如何继续 tweet示例:Twitter 如何使用lucene从推文中提取日期?,twitter,lucene,Twitter,Lucene,我有一些tweet,我已经为“TweetIndexer”编制了索引,lucene知道每条tweet都包含一个ID、用户、文本和日期 我只想用另一个类“TweetSearcher”检索日期如何继续 tweet示例: "0","1467811372","Mon Apr 06 22:20:00 PDT 2009","NO_QUERY","joy_wolf","@Kwesidei not the whole crew ". 这是我的类TweetIndexer: import org.apach
"0","1467811372","Mon Apr 06 22:20:00 PDT 2009","NO_QUERY","joy_wolf","@Kwesidei not the whole crew ".
这是我的类TweetIndexer
:
import org.apache.lucene.analysis.core.KeywordAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class TweetIndexer {
protected static final String COMMA = "\",\"";
protected static final String POLARITY = "polarity";
protected static final String ID = "id";
protected static final String DATE = "date";
protected static final String QUERY = "query";
protected static final String USER = "user";
protected static final String TEXT = "text";
public static void main(String[] args) throws Exception {
try {
String indexDir = "D:\\tweet\\index";
String dataFile = "D:\\tweet\\collection\\tweets.csv";
TweetIndexer tweetIndexer = new TweetIndexer();
long start = System.currentTimeMillis();
int count = tweetIndexer.index(new File(indexDir), new File(dataFile));
System.out.print(String.format("Indexed %d documents in %d seconds", count, (System.currentTimeMillis() - start) / 1000));
}
catch (Exception e) {
System.out.println("Usage: java TweetIndexer <index directory> <csv data file>");
}
}
private int index(File indexDir, File dataFile) throws Exception {
IndexWriter indexWriter = new IndexWriter(
FSDirectory.open(indexDir),
new IndexWriterConfig(Version.LUCENE_44, new KeywordAnalyzer()));
int count = indexFile(indexWriter, dataFile);
indexWriter.close();
return count;
}
private int indexFile(IndexWriter indexWriter, File dataFile) throws IOException {
FieldType fieldType = new FieldType();
fieldType.setStored(true);
fieldType.setIndexed(true);
BufferedReader bufferedReader = new BufferedReader(new FileReader(dataFile));
String line = "";
int count = 0;
while ((line = bufferedReader.readLine()) != null) {
// Hack to ignore commas within elements in csv (so we can split on "," rather than just ,)
line = line.substring(1, line.length() - 1);
String[] tweetInfo = line.split(COMMA);
Document document = new Document();
document.add(new Field(POLARITY, tweetInfo[0], fieldType));
document.add(new Field(ID, tweetInfo[1], fieldType));
document.add(new Field(DATE, tweetInfo[2], fieldType));
document.add(new Field(QUERY, tweetInfo[3], fieldType));
document.add(new StringField(USER, tweetInfo[4], Field.Store.YES));
document.add(new StringField(TEXT, tweetInfo[5], Field.Store.YES));
indexWriter.addDocument(document);
count++;
}
return count;
}
嗨,西莉亚,如果我们能看到你的代码,这对我们真的很有帮助——否则我们就没有任何工作要做了。如果你编辑你的文章,包括你已经写了,这将是一个很大的帮助。你好,我添加了代码,你能帮我吗!好的,这看起来是个好的开始。你得到了什么错误?当我检查我的索引文件时没有错误,我找到了创建的索引,但我想创建另一个类TweetSearcher,我只使用它检索tweet的创建日期。谢谢。如果你能分享你的尝试,我们可以帮助你。我们不能为你写一个新类。嗨,西莉亚,如果我们能看到你的代码,这对我们真的很有帮助,否则我们就没有任何工作要做了。如果你编辑你的文章,包括你已经写了,这将是一个很大的帮助。你好,我添加了代码,你能帮我吗!好的,这看起来是个好的开始。你得到了什么错误?当我检查我的索引文件时没有错误,我找到了创建的索引,但我想创建另一个类TweetSearcher,我只使用它检索tweet的创建日期。谢谢。如果你能分享你的尝试,我们可以帮助你。我们不能为你写新的课程。
public class TweetSearcher {
public static void main(String[] args) throws Exception {
try {
String indexDir = "D:\\tweet\\index";
int numHits = Integer.parseInt("3");
TweetSearcher tweetSearcher = new TweetSearcher();
tweetSearcher.dateSearch(new File(indexDir), numHits);
private void dateSearch(File indexDir, int numHits) throws Exception {
System.out.println("Find dates:");
Directory directory = FSDirectory.open(indexDir);
DirectoryReader directoryReader = DirectoryReader.open(directory);
IndexSearcher indexSearcher = new IndexSearcher(directoryReader);