学习LuceneSail(论文+代码举例)

一. 论文

1,论文出处 

         The Sesame LuceneSail: RDF queries with full-text search  (2008年发表)

2,论文笔记

      简单说,LuceneSail就是集成lucene到sesame。它是结构化RDF查询和全文搜索的一个结合,sesame (RDF store)和lucene(文本搜索库)。

        Motivation :  SPARQL只能字符串全匹配或过滤,操作慢。

luceneSail 文档:此处

下面分别简单介绍sesame和lucene.

sesame

sesame是一个RDF store,可以存储RDF文件并且对RDF进行查询等操作。它可以使用不同的后端,比如关系数据库、或者原生RDF文件。像JAVA的JDBC连接一样,sesame也需要连接,sesame连接使用SALL Connection(SALL对象)。通过连接,可以进行添加(adding)、移除(removing)、查询(query)、事务管理等操作。

连接集中:事务的清除处理和对RDF store的并发访问。

Lucene

lucene是一个全文搜索引擎库,每个字段包含字符串名称和字符串的值。每一个被检索的文档提供一个评分(根据TFIDF测量的相关性)。查询时,必须指定要查询的字段,故不可能一次只查询所有字段。因此,最好把所有文本再次存储在一个索引的字段中,就可以在所有字段上快速查询。

         图1  Lucene实现全文检索的流程

  1、绿色表示索引过程,对要搜索的原始内容进行索引构建一个索引库,索引过程包括:

    确定原始内容即要搜索的内容→采集文档→创建文档→分析文档→索引文档

  2、红色表示搜索过程,从索引库中搜索内容,搜索过程包括:

    用户通过搜索界面→创建查询→执行搜索,从索引库搜索→渲染搜索结果

 

 

二.代码

详细代码已上传至github:   https://github.com/kathy775/LuceneSail-Demo

实验环境:

java1.7

需要sesame的jar包

代码:

基于nativeStore生成索引

import java.io.File;
import java.util.Scanner;
import org.openrdf.query.Binding;
import org.openrdf.query.BindingSet;
import org.openrdf.query.QueryLanguage;
import org.openrdf.query.TupleQuery;
import org.openrdf.query.TupleQueryResult;
import org.openrdf.repository.sail.SailRepository;
import org.openrdf.repository.sail.SailRepositoryConnection;
import org.openrdf.rio.RDFFormat;
import org.openrdf.sail.lucene.LuceneSail;
import org.openrdf.sail.lucene.LuceneSailSchema;
import org.openrdf.sail.memory.MemoryStore;
import org.openrdf.sail.nativerdf.NativeStore;
/**
 * Example code showing how to use the LuceneSail
 */
public class DBPLuceneSailIndex {
	public static void main(String[] args) throws Exception {		 
		 String index_path = "/home/LuceneSailMemo/dbpedia";	 
		 createSimple(index_path);	
	}

	/**
	 * Create a LuceneSail and add some triples to it, ask a query.
	 */
	public static void createSimple(String index_path ) throws Exception {
		// create a sesame memory sail
		NativeStore myStore = new NativeStore();
		File dataDir = new File(index_path);
		myStore.setDataDir(dataDir);
		// create a lucenesail to wrap the memorystore
		LuceneSail lucenesail = new LuceneSail();
		// set this parameter to let the lucene index store its data in ram
		lucenesail.setParameter(LuceneSail.LUCENE_DIR_KEY, "true");
		// set this parameter to store the lucene index on disk
		// lucenesail.setParameter(LuceneSail.LUCENE_DIR_KEY, "./data/mydirectory");

		// wrap memorystore in a lucenesail
		lucenesail.setBaseSail(myStore);
		// create a Repository to access the sails
		SailRepository repository = new SailRepository(lucenesail);
		repository.initialize();
		SailRepositoryConnection connection = repository.getConnection();
		// connection.begin();
		try {
			// connection.setAutoCommit(false);
			// System.out.println(System.getProperty("user.dir"));  

			String file_path = "file path";
                        File file = new File(file_path);
			System.out.println(file.exists());
                        connection.add(file, "", RDFFormat.NTRIPLES);	

			connection.commit();
			System.out.println("------ 指数文件已生成 -----");
		} finally {
			connection.close();
			repository.shutDown();
		}
	}
}

 

再回首,在更新……

 

下次:luceneSail查询部分代码。memoryStore代码。细节补充

 

2018年12月14日 更新 ……

代码已上传至github:   https://github.com/kathy775/LuceneSail-Demo

包括:

基于nativeStore生成索引

基于memoryStore生成索引

使用 LuceneSail 查询 关键字

 

下次:查询的细节补充

 

      

      

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是一个基于机器学习的信号去噪的 Matlab 代码示例: ```matlab % 加载数据 load('noisy_signal.mat'); % 划分训练集和测试集 train_ratio = 0.8; train_size = floor(train_ratio * length(noisy_signal)); train_set = noisy_signal(1:train_size); test_set = noisy_signal(train_size+1:end); % 创建训练集和测试集的数据矩阵 window_size = 10; train_data = zeros(train_size - window_size + 1, window_size); for i = 1:train_size - window_size + 1 train_data(i,:) = train_set(i:i+window_size-1); end test_data = zeros(length(test_set) - window_size + 1, window_size); for i = 1:length(test_set) - window_size + 1 test_data(i,:) = test_set(i:i+window_size-1); end % 构建自编码器模型 hidden_size = 5; autoencoder = trainAutoencoder(train_data', hidden_size); % 对测试集进行去噪 denoised_test_data = predict(autoencoder, test_data'); % 绘制去噪前后的信号对比图 figure; subplot(2,1,1); plot(test_set); title('Noisy Signal'); subplot(2,1,2); plot(denoised_test_data'); title('Denoised Signal'); ``` 这是一个基于自编码器的信号去噪方法,代码中加载了一个名为 `noisy_signal.mat` 的信号文件,并将其划分为训练集和测试集。然后将训练集和测试集的数据分别放入一个数据矩阵中,用于训练自编码器模型和对测试集进行去噪。最后绘制去噪前后的信号对比图。需要注意的是,这只是一个简单的示例代码,实际上基于机器学习的信号去噪方法还有很多种,具体选择哪种方法需要根据具体情况进行选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值