等我学会了RDF和OWL,我就可以把你放到数据库了。。。
如果不了解什么是OWL 请戳这里点击打开链接
protege是构建领域本体的一个重要工具,他由standford开发,基于Java语言开发的本体编辑和知识获取软件,或者说是本体开发工具,也是基于知识的编辑器,属于开放源代码软件。
他提供了一个可视化的界面来创建和编辑本体,但不能解决我们批量导入数据,生成领域知识库的需求。
protege底层用的API是OWL API(http://owlapi.sourceforge.net/),javadoc请戳这点击打开链接
ZWQ师兄抓取全国旅游景点信息结构:
ChName 北天门
EnName bei tian men
score 3.6分
addr 地址:泰安泰山区
summary 自岱顶丈人峰顺坡而下,经过后石坞索道站,至山坳处有北天门石坊,1984年重立。原坊额称“元武”,即北方之神玄武,清代废。坊北是摩云岭,丹嶂凌空。东侧有八仙洞,危若鹘栖,传吕洞宾曾在此学道术炼丹。清代在洞口筑吕祖庙,民国间坍毁。
Country 山东
City 泰山
URL http://you.ctrip.com/sight/taishan6/132103.html
X 36.1365547180176
Y 117.089393615723
类型 历史建筑
Y以上类型每个景点都有,比较规整,下面的属性比较杂,还需要进行清洗。
创建一个本体管理的对象,并获得他的数据DataFactory
static OWLOntologyManager m = OWLManager.createOWLOntologyManager();
static OWLDataFactory dataFactory = m.getOWLDataFactory();
分别创建景点,城市,省份的Class
static OWLClass provinceClass = dataFactory.getOWLClass(IRI.create(base + "#province"));
static OWLClass cityClass = dataFactory.getOWLClass(IRI.create(base + "#city"));
static OWLClass siteClass = dataFactory.getOWLClass(IRI.create(base + "#site"));
创建景点位于城市、省份,城市位于省份的ObjectProperty
static OWLObjectProperty locateProvince = dataFactory.getOWLObjectProperty(IRI.create(base + "#locateProvince"));
static OWLObjectProperty locateCity = dataFactory.getOWLObjectProperty(IRI.create(base + "#locateCity"));
static OWLObjectProperty cityInpro = dataFactory.getOWLObjectProperty(IRI.create(base + "#cityInpro"));
命名景点对象拥有的各种属性DataProperty
static OWLDataProperty hasName = dataFactory.getOWLDataProperty(IRI.create(base + "#hasName"));
static OWLDataProperty hasScore = dataFactory.getOWLDataProperty(IRI.create(base + "#hasScore"));
static OWLDataProperty hasAddr = dataFactory.getOWLDataProperty(IRI.create(base + "#hasAddr"));
static OWLDataProperty hasSummary = dataFactory.getOWLDataProperty(IRI.create(base + "#hasSummary"));
static OWLDataProperty hasURL = dataFactory.getOWLDataProperty(IRI.create(base + "#hasURL"));
static OWLDataProperty hasWD = dataFactory.getOWLDataProperty(IRI.create(base + "#hasWD"));
static OWLDataProperty hasJD = dataFactory.getOWLDataProperty(IRI.create(base + "#hasJD"));
定义两种数据类型
static OWLDatatype stringDatatype = dataFactory.getOWLDatatype(OWL2Datatype.XSD_STRING.getIRI());
static OWLDatatype doubleDatatype = dataFactory.getOWLDatatype(OWL2Datatype.XSD_DOUBLE.getIRI());
主函数,创建本体,读取目录下的全部旅游文件,并通过solve函数加入本体,最后保存本地。
public static void main(String[] args) throws OWLOntologyCreationException, OWLOntologyStorageException{
OWLOntology ont = m.createOntology(ontologyIRI);
String path = "/home/fssqawj/下载/information/";
File file = new File(path);
String[] filelist = file.list();
for(String key : filelist){
File readfile = new File(path + "/" + key);
String[] flist = readfile.list();
for(String fname : flist){
solve(m, ont, readfile.getPath() + "/" + fname);
}
}
m.saveOntology(ont,IRI.create("file:/home/fssqawj/example.owl"));
System.out.println("done!");
}
solve函数参数,OWLOntologyManager m, OWLOntology ont, String filename
首先将文件信息读出来,保存为键值对
Map<String, String> tem = reof.read(filename);
创建景点类的一个实例
OWLNamedIndividual site = dataFactory.getOWLNamedIndividual(IRI.create(base + "#" + tem.get("ChName")));
为景点实例添加各种属性,这里只展示添加name属性的代码
OWLLiteral literal = dataFactory.getOWLLiteral(tem.get("ChName"), stringDatatype);
// Create the property assertion and add it to the ontology
OWLAxiom ax = dataFactory.getOWLDataPropertyAssertionAxiom(hasName, site,literal);
m.addAxiom(ont, ax);
创建城市和省份的实例类似,下面是添加类之间关系的代码
OWLObjectPropertyAssertionAxiom assertionpro = dataFactory.getOWLObjectPropertyAssertionAxiom(locateProvince, site, province);
AddAxiom addAxiomChangepro = new AddAxiom(ont, assertionpro);
m.applyChange(addAxiomChangepro);
最后提供OWL API example 请戳这里点击打开链接