基于字符串,wordnet以及基于结构的本体匹配Java代码。项目名称 GMR(郭meng ran)-ontology parsing
package com.tylg.maintest;
Main类
package com. tylg. maintest;
public class Main {
public static void main ( String[ ] args) {
OntologyParsing op = new OntologyParsing ( ) ;
String file1 = "D:\\eclipse\\ljw-ontology parsing\\File of ontology\\onto-101.rdf" ;
String file2 = "D:\\eclipse\\ljw-ontology parsing\\File of ontology\\onto-101.rdf" ;
VectorSpaceMatrix vsm = new VectorSpaceMatrix ( ) ;
System. out. println ( "我们要解析的文件是:" + file1) ;
System. out. println ( "我们要解析的文件是:" + file2) ;
double [ ] [ ] classesAboutNGramLMATRIX = new double [ parsingOfClassAboutSpecifiedFile ( op, file1) . length] [ parsingOfClassAboutSpecifiedFile ( op, file2) . length] ;
classesAboutNGramLMATRIX = vsm. vectorSpaceMatrixOfNGramL ( parsingOfClassAboutSpecifiedFile ( op, file1) , parsingOfClassAboutSpecifiedFile ( op, file2) , 3 ) ;
double [ ] [ ] datatypeproperityAboutNGramLMATRIX = new double [ parsingOfDatatypeProperityAboutSpecifiedFile ( op, file1) . length] [ parsingOfDatatypeProperityAboutSpecifiedFile ( op, file2) . length] ;
datatypeproperityAboutNGramLMATRIX = vsm. vectorSpaceMatrixOfNGramL ( parsingOfDatatypeProperityAboutSpecifiedFile ( op, file1) , parsingOfDatatypeProperityAboutSpecifiedFile ( op, file2) , 3 ) ;
double [ ] [ ] objectproperityAboutNGramLMATRIX = new double [ parsingOfObjectProperityAboutSpecifiedFile ( op, file1) . length] [ parsingOfObjectProperityAboutSpecifiedFile ( op, file2) . length] ;
objectproperityAboutNGramLMATRIX = vsm. vectorSpaceMatrixOfNGramL ( parsingOfObjectProperityAboutSpecifiedFile ( op, file1) , parsingOfObjectProperityAboutSpecifiedFile ( op, file2) , 3 ) ;
System. out. println ( "\n*****************************\n* 关于NGramL的相似度矩阵 *\n*****************************" ) ;
System. out. println ( "\n关于class的NGramL相似度矩阵如下:" ) ;
for ( int i = 0 ; i < classesAboutNGramLMATRIX. length; i++ ) {
for ( int j = 0 ; j < classesAboutNGramLMATRIX[ 0 ] . length; j++ ) {
System. out. print ( classesAboutNGramLMATRIX[ i] [ j] + " " ) ;
}
System. out. println ( ) ;
}
System. out. println ( "\n关于datatypeproperity的NGramL相似度矩阵如下:" ) ;
for ( int i = 0 ; i < datatypeproperityAboutNGramLMATRIX. length; i++ ) {
for ( int j = 0 ; j < datatypeproperityAboutNGramLMATRIX[ 0 ] . length; j++ ) {
System. out. print ( datatypeproperityAboutNGramLMATRIX[ i] [ j] + " " ) ;
}
System. out. println ( ) ;
}
System. out. println ( "\n关于objectproperity的NGramL相似度矩阵如下:" ) ;
for ( int i = 0 ; i < objectproperityAboutNGramLMATRIX. length; i++ ) {
for ( int j = 0 ; j < objectproperityAboutNGramLMATRIX[ 0 ] . length; j++ ) {
System. out. print ( objectproperityAboutNGramLMATRIX[ i] [ j] + " " ) ;
}
System. out. println ( ) ;
}
double [ ] [ ] classesAboutwuAndPalmerSimilarMATRIX = new double [ parsingOfClassAboutSpecifiedFile ( op, file1) . length] [ parsingOfClassAboutSpecifiedFile ( op, file2) . length] ;
classesAboutwuAndPalmerSimilarMATRIX = vsm. vectorSpaceMatrixOfwuAndPalmer ( parsingOfClassAboutSpecifiedFile ( op, file1) , parsingOfClassAboutSpecifiedFile ( op, file2) ) ;
double [ ] [ ] datatypeproperityAboutwuAndPalmerSimilarMATRIX = new double [ parsingOfDatatypeProperityAboutSpecifiedFile ( op, file1) . length] [ parsingOfDatatypeProperityAboutSpecifiedFile ( op, file2) . length] ;
datatypeproperityAboutwuAndPalmerSimilarMATRIX = vsm. vectorSpaceMatrixOfwuAndPalmer ( parsingOfDatatypeProperityAboutSpecifiedFile ( op, file1) , parsingOfDatatypeProperityAboutSpecifiedFile ( op, file2) ) ;
double [ ] [ ] objectproperityAboutwuAndPalmerSimilarMATRIX = new double [ parsingOfObjectProperityAboutSpecifiedFile ( op, file1) . length] [ parsingOfObjectProperityAboutSpecifiedFile ( op, file2) . length] ;
objectproperityAboutwuAndPalmerSimilarMATRIX = vsm. vectorSpaceMatrixOfwuAndPalmer ( parsingOfObjectProperityAboutSpecifiedFile ( op, file1) , parsingOfObjectProperityAboutSpecifiedFile ( op, file2) ) ;
System. out. println ( "\n********************************\n* 关于wuAndPalmer的相似度矩阵 *\n********************************" ) ;
System. out. println ( "\n关于class的wuAndPalmer相似度矩阵如下:" ) ;
for ( int i = 0 ; i < classesAboutwuAndPalmerSimilarMATRIX. length; i++ ) {
for ( int j = 0 ; j < classesAboutwuAndPalmerSimilarMATRIX[ 0 ] . length; j++ ) {
System. out. print ( classesAboutwuAndPalmerSimilarMATRIX[ i] [ j] + " " ) ;
}
System. out. println ( ) ;
}
System. out. println ( "\n关于datatypeproperity的wuAndPalmer相似度矩阵如下:" ) ;
for ( int i = 0 ; i < datatypeproperityAboutwuAndPalmerSimilarMATRIX. length; i++ ) {
for ( int j = 0 ; j < datatypeproperityAboutwuAndPalmerSimilarMATRIX[ 0 ] . length; j++ ) {
System. out. print ( datatypeproperityAboutwuAndPalmerSimilarMATRIX[ i] [ j] + " " ) ;
}
System. out. println ( ) ;
}
System. out. println ( "\n关于objectproperity的wuAndPalmer相似度矩阵如下:" ) ;
for ( int i = 0 ; i < objectproperityAboutwuAndPalmerSimilarMATRIX. length; i++ ) {
for ( int j = 0 ; j < objectproperityAboutwuAndPalmerSimilarMATRIX[ 0 ] . length; j++ ) {
System. out. print ( objectproperityAboutwuAndPalmerSimilarMATRIX[ i] [ j] + " " ) ;
}
System. out. println ( ) ;
}
}
public static String[ ] parsingOfObjectProperityAboutSpecifiedFile ( OntologyParsing op, String file1) {
int i3 = op. weGetObjectProperties ( op. buildModel ( file1) ) . length;
String [ ] s3 = new String [ i3] ;
s3 = op. weGetObjectProperties ( op. buildModel ( file1) ) ;
return s3;
}
public static String[ ] parsingOfDatatypeProperityAboutSpecifiedFile ( OntologyParsing op, String file1) {
int i2 = op. weGetDatatypeProperities ( op. buildModel ( file1) ) . length;
String [ ] s2 = new String [ i2] ;
s2 = op. weGetDatatypeProperities ( op. buildModel ( file1) ) ;
return s2;
}
public static String[ ] parsingOfClassAboutSpecifiedFile ( OntologyParsing op, String file1) {
int i1 = op. weGetClasses ( op. buildModel ( file1) ) . length;
String [ ] s1 = new String [ i1] ;
s1 = op. weGetClasses ( op. buildModel ( file1) ) ;
return s1;
}
}
OntologyParsing类
package com. tylg. maintest;
import java. io. FileInputStream;
import java. io. FileNotFoundException;
import java. util. ArrayList;
import java. util. List;
import org. apache. jena. ontology. DatatypeProperty;
import org. apache. jena. ontology. ObjectProperty;
import org. apache. jena. ontology. OntClass;
import org. apache. jena. ontology. OntModel;
import org. apache. jena. ontology. OntModelSpec;
import org. apache. jena. rdf. model. ModelFactory;
import org. apache. jena. util. iterator. ExtendedIterator;
public class OntologyParsing {
public static OntModel buildModel ( String s) {
OntModel ontModel = ModelFactory. createOntologyModel ( OntModelSpec. OWL_MEM) ;
try {
ontModel. read ( new FileInputStream ( s) , "" ) ;
} catch ( FileNotFoundException e1) {
e1. printStackTrace ( ) ;
}
return ontModel;
}
public String[ ] weGetClasses ( OntModel ontModel) {
List< String> list = new ArrayList < > ( ) ;
ExtendedIterator exIter = ontModel. listClasses ( ) ;
int i1 = 0 ;
while ( exIter. hasNext ( ) ) {
OntClass oc = ( OntClass) exIter. next ( ) ;
if ( ( oc. getURI ( ) != null) && ( oc. getURI ( ) . toString ( ) . startsWith ( "http://oaei" ) ) ) {
i1++ ;
String s1 = oc. getURI ( ) + "" ;
String s2 = s1. substring ( 0 , s1. indexOf ( "#" ) ) ;
String s3 = s1. substring ( s2. length ( ) + 1 , s1. length ( ) ) ;
list. add ( s3) ;
}
}
String[ ] classes = ( String[ ] ) list. toArray ( new String [ i1] ) ;
return classes;
}
public String[ ] weGetDatatypeProperities ( OntModel ontModel) {
List< String> list = new ArrayList < > ( ) ;
int i2 = 0 ;
ExtendedIterator exIter1 = ontModel. listDatatypeProperties ( ) ;
while ( exIter1. hasNext ( ) ) {
DatatypeProperty oc = ( DatatypeProperty) exIter1. next ( ) ;
if ( oc. getURI ( ) != null && ( oc. getURI ( ) . toString ( ) . startsWith ( "http://oaei" ) ) ) {
i2++ ;
String s1 = oc. getURI ( ) + "" ;
String s2 = s1. substring ( 0 , s1. indexOf ( "#" ) ) ;
String s3 = s1. substring ( s2. length ( ) + 1 , s1. length ( ) ) ;
list. add ( s3) ;
}
}
String[ ] datatypeproperities = ( String[ ] ) list. toArray ( new String [ i2] ) ;
return datatypeproperities;
}
public String[ ] weGetObjectProperties ( OntModel ontModel) {
ExtendedIterator exIter2 = ontModel. listObjectProperties ( ) ;
int i3 = 0 ;
List< String> list = new ArrayList < > ( ) ;
while ( exIter2. hasNext ( ) ) {
ObjectProperty oc = ( ObjectProperty) exIter2. next ( ) ;
if ( oc. getURI ( ) != null && ( oc. getURI ( ) . toString ( ) . startsWith ( "http://oaei" ) ) ) {
i3++ ;
String s1 = oc. getURI ( ) + "" ;
String s2 = s1. substring ( 0 , s1. indexOf ( "#" ) ) ;
String s3 = s1. substring ( s2. length ( ) + 1 , s1. length ( ) ) ;
list. add ( s3) ;
}
}
String[ ] objectProperities = ( String[ ] ) list. toArray ( new String [ i3] ) ;
return objectProperities;
}
}
VectorSpaceMatrix类
package com. tylg. maintest;
import com. tylg. maintest. WaysOfSimilary;
public class VectorSpaceMatrix {
public static double [ ] [ ] vectorSpaceMatrixOfNGramL ( String[ ] s1, String[ ] s2, int n) {
int len1 = s1. length;
int len2 = s2. length;
double [ ] [ ] result = new double [ len1] [ len2] ;
WaysOfSimilary wos = new WaysOfSimilary ( ) ;
for ( int i = 0 ; i < len1; i++ ) {
for ( int j = 0 ; j < len2; j++ ) {
result[ i] [ j] = wos. NGramL ( s1[ i] , s2[ j] , n) ;
}
}
return result;
}
public static double [ ] [ ] vectorSpaceMatrixOfwuAndPalmer ( String[ ] s1, String[ ] s2) {
int len1 = s1. length;
int len2 = s2. length;
double [ ] [ ] result = new double [ len1] [ len2] ;
WaysOfSimilary wos = new WaysOfSimilary ( ) ;
for ( int i = 0 ; i < len1; i++ ) {
for ( int j = 0 ; j < len2; j++ ) {
result[ i] [ j] = wos. wuAndPalmerWordSimilarity ( s1[ i] , s2[ j] ) ;
}
}
return result;
}
}
WaysOfSimilary类
package com. tylg. maintest;
import java. util. HashMap;
import java. util. Iterator;
import java. util. Map;
import org. apache. lucene. search. spell. NGramDistance;
import com. tylg. math. MathTool;
import edu. sussex. nlp. jws. JWS;
import edu. sussex. nlp. jws. WuAndPalmer;
public class WaysOfSimilary {
private static String dir = "D:\\Wordnet-2.1\\WordNet" ;
private static JWS ws = new JWS ( dir, "2.1" ) ;
private static WuAndPalmer wu = ws. getWuAndPalmer ( ) ;
public static double wuAndPalmerWordSimilarity ( String str1, String str2) {
double sc = wu. max ( str1, str2, "n" ) ;
return MathTool. decimalType ( sc) ;
}
public static double NGramL ( String a, String b, int n) {
NGramDistance ng = new NGramDistance ( n) ;
return MathTool. decimalType ( ng. getDistance ( a, b) ) ;
}
}
package com.tylg.math
MathTool
package com. tylg. math;
import java. math. BigDecimal;
public class MathTool {
public static double decimalType ( double x) {
BigDecimal b = new BigDecimal ( x) ;
double f = b. setScale ( 2 , BigDecimal. ROUND_HALF_UP) . doubleValue ( ) ;
return f;
}
}
package com.tylg.out_in_degree;
In_degree类
package com. tylg. out_in_degree;
import java. io. BufferedReader;
import java. io. FileReader;
import java. io. IOException;
import java. util. ArrayList;
import java. util. HashMap;
import java. util. Map;
public class In_degree {
public static Map< String, Integer> calculationOfIn_degree ( String filename) throws IOException {
BufferedReader br = new BufferedReader ( new FileReader ( filename) ) ;
ArrayList< String> arrayList = new ArrayList < String> ( ) ;
String sline;
while ( ( sline = br. readLine ( ) ) != null) {
arrayList. add ( sline) ;
}
br. close ( ) ;
int lengthOfMap = arrayList. size ( ) ;
Map< String, Integer> map = new HashMap < String, Integer> ( ) ;
String[ ] strArray = arrayList. toArray ( new String [ lengthOfMap] ) ;
String[ ] headOfOneLine = new String [ lengthOfMap] ;
int flag = 0 ;
for ( int i = 0 ; i < strArray. length; i++ ) {
String[ ] strOfOneLine = strArray[ i] . split ( " " ) ;
headOfOneLine[ i] = strOfOneLine[ 0 ] ;
map. put ( headOfOneLine[ i] , flag) ;
}
for ( int i = 0 ; i < strArray. length; i++ ) {
String[ ] strOfOneLine = strArray[ i] . split ( " " ) ;
for ( int j = 1 ; j < strOfOneLine. length; j++ ) {
if ( map. containsKey ( strOfOneLine[ j] ) ) {
map. replace ( strOfOneLine[ j] , map. get ( strOfOneLine[ j] ) + 1 ) ;
} else {
map. put ( strOfOneLine[ j] , 1 ) ;
}
}
}
return map;
}
}
Out_degree类
package com. tylg. out_in_degree;
import java. io. BufferedReader;
import java. io. FileNotFoundException;
import java. io. FileReader;
import java. io. IOException;
import java. util. ArrayList;
import java. util. HashMap;
import java. util. Iterator;
import java. util. Map;
import org. apache. commons. math3. linear. RealMatrix;
public class Out_degree {
public static Map< String, Integer> calculationOfOut_degree ( String filename) throws IOException {
BufferedReader br = new BufferedReader ( new FileReader ( filename) ) ;
ArrayList< String> arrayList = new ArrayList < String> ( ) ;
String sline;
while ( ( sline = br. readLine ( ) ) != null) {
arrayList. add ( sline) ;
}
br. close ( ) ;
Iterator< String> iteratorOfArrayList = arrayList. iterator ( ) ;
Map< String, Integer> map = new HashMap < String, Integer> ( ) ;
while ( iteratorOfArrayList. hasNext ( ) ) {
String str = iteratorOfArrayList. next ( ) ;
String[ ] strArr = str. split ( " " ) ;
for ( int i = 0 ; i < strArr. length; i++ ) {
map. put ( strArr[ i] , 0 ) ;
}
}
String[ ] all = arrayList. toArray ( new String [ arrayList. size ( ) ] ) ;
for ( String string : all) {
String[ ] strArrOneLine = string. split ( " " ) ;
map. put ( strArrOneLine[ 0 ] , strArrOneLine. length - 1 ) ;
}
return map;
}
}
package com.tylg.simrank;
MyMatrix类
package com. tylg. simrank;
import org. apache. commons. math3. linear. Array2DRowRealMatrix;
import org. apache. commons. math3. linear. LUDecomposition;
import org. apache. commons. math3. linear. RealMatrix;
public class MyMatrix {
public static RealMatrix arrayToMatrix ( double [ ] [ ] array) {
RealMatrix rm = new Array2DRowRealMatrix ( array) ;
return rm;
}
public static RealMatrix transposedMatrix ( RealMatrix rm) {
RealMatrix rmtranspose = rm. transpose ( ) ;
return rmtranspose;
}
public static RealMatrix multiplyMatrix ( RealMatrix rm1, RealMatrix rm2) {
RealMatrix rm = rm1. multiply ( rm2) ;
return rm;
}
public static RealMatrix inverseMatrix ( RealMatrix a) {
RealMatrix result = new LUDecomposition ( a) . getSolver ( ) . getInverse ( ) ;
return result;
}
public static RealMatrix addMatrix ( RealMatrix a, RealMatrix b) {
RealMatrix result = a. add ( b) ;
return result;
}
}
SimRank类
package com. tylg. simrank;
import java. io. BufferedReader;
import java. io. File;
import java. io. FileNotFoundException;
import java. io. FileReader;
import java. io. IOException;
import java. util. ArrayList;
import java. util. HashMap;
import java. util. Map;
import org. apache. commons. math3. linear. RealMatrix;
import fr. inrialpes. exmo. ontosim. util. matrix. Matrix;
public class SimRank {
private static double C = 0.8 ;
private static int TIMES_OF_ITERATION = 3 ;
public static RealMatrix simMatrixOfSimRank ( String filename) throws IOException {
BufferedReader br = new BufferedReader ( new FileReader ( filename) ) ;
String s;
ArrayList< String> arrayList = new ArrayList < String> ( ) ;
while ( ( s = br. readLine ( ) ) != null) {
arrayList. add ( s) ;
}
br. close ( ) ;
String[ ] str = ( String[ ] ) arrayList. toArray ( new String [ arrayList. size ( ) ] ) ;
br. close ( ) ;
String[ ] codeName = new String [ str. length] ;
int flag = 1 ;
int [ ] [ ] flagQ = new int [ str. length] [ str. length] ;
for ( int i = 0 ; i < str. length; i++ ) {
String[ ] line = str[ i] . split ( " " ) ;
codeName[ i] = line[ 0 ] ;
}
for ( int i = 0 ; i < str. length; i++ ) {
String[ ] line = str[ i] . split ( " " ) ;
int sizeOfline = line. length;
for ( int j = 0 ; j < sizeOfline; j++ ) {
int index = getIndex ( codeName, line[ j] ) ;
if ( ( index != - 1 ) && ( index != i) ) {
flagQ[ index] [ i] += flag;
}
}
}
int [ ] totalOfRow = new int [ flagQ[ 0 ] . length] ;
for ( int i = 0 ; i < flagQ[ 0 ] . length; i++ ) {
for ( int j = 0 ; j < flagQ. length; j++ ) {
totalOfRow[ i] += flagQ[ j] [ i] ;
}
}
double [ ] [ ] Q_ = new double [ flagQ. length] [ flagQ[ 0 ] . length] ;
for ( int i = 0 ; i < flagQ[ 0 ] . length; i++ ) {
for ( int j = 0 ; j < flagQ. length; j++ ) {
if ( totalOfRow[ i] != 0 ) {
Q_[ j] [ i] = ( double ) flagQ[ j] [ i] / totalOfRow[ i] ;
}
}
}
RealMatrix Q = MyMatrix. arrayToMatrix ( Q_) ;
RealMatrix initial_S = diagonalMatrix ( 1 - C, flagQ. length) ;
RealMatrix final_S = iterationOfMatrix ( initial_S, Q, C, TIMES_OF_ITERATION, flagQ. length) ;
return final_S;
}
private static RealMatrix iterationOfMatrix ( RealMatrix initial_S, RealMatrix Q, double c2, int timeOfIteration,
int len) {
RealMatrix midMatrix = initial_S;
double [ ] [ ] c_ = new double [ len] [ len] ;
for ( int i = 0 ; i < len; i++ ) {
c_[ i] [ i] = c2;
}
RealMatrix C_Matrix = MyMatrix. arrayToMatrix ( c_) ;
for ( int i = 0 ; i < timeOfIteration; i++ ) {
midMatrix = MyMatrix. addMatrix ( MyMatrix. multiplyMatrix (
MyMatrix. multiplyMatrix ( MyMatrix. multiplyMatrix ( C_Matrix, MyMatrix. transposedMatrix ( Q) ) , midMatrix) ,
Q) , initial_S) ;
}
RealMatrix finalMatrix = midMatrix;
return finalMatrix;
}
private static RealMatrix diagonalMatrix ( double d, int length) {
double [ ] [ ] A = new double [ length] [ length] ;
for ( int i = 0 ; i < length; i++ ) {
A[ i] [ i] = d;
}
return MyMatrix. arrayToMatrix ( A) ;
}
public static int getIndex ( String[ ] codeName, String string) {
for ( int i = 0 ; i < codeName. length; i++ ) {
if ( string. equals ( codeName[ i] ) ) {
return i;
}
}
return - 1 ;
}
}
Test类
package com. tylg. simrank;
import java. io. IOException;
import java. util. HashMap;
import java. util. Map;
import com. tylg. out_in_degree. In_degree;
import com. tylg. out_in_degree. Out_degree;
public class Test {
public static void main ( String[ ] args) throws IOException {
Map< String, Integer> map1 = Out_degree. calculationOfOut_degree ( "test1" ) ;
System. out. print ( "出度为:" ) ;
System. out. println ( map1) ;
Map< String, Integer> map2 = In_degree. calculationOfIn_degree ( "test1" ) ;
System. out. print ( "入度为:" ) ;
System. out. println ( map2) ;
}
}