protected Map < Page , Double > getSimilarPages ( String pPattern , int pSize ) throws WikiApiException { Title title = new Title ( pPattern ) ; String pattern = title . getWikiStyleTitle ( ) ; // a mapping of the most similar pages and their similarity values // It is returned by this method. Map < Page , Double > pageMap = new HashMap < Page , Double > ( ) ; // holds a mapping of the best distance values to page IDs Map < Integer , Double > distanceMap = new HashMap < Integer , Double > ( ) ; Session session = this . __getHibernateSession ( ) ; session . beginTransaction ( ) ; Iterator results = session . createQuery ( "select pml.pageID, pml.name from PageMapLine as pml" ) . list ( ) . iterator ( ) ; while ( results . hasNext ( ) ) { Object [ ] row = ( Object [ ] ) results . next ( ) ; int pageID = ( Integer ) row [ 0 ] ; String pageName = ( String ) row [ 1 ] ; // this returns a similarity - if we want to use it, we have to change the semantics the ordering of the results // double distance = new Levenshtein().getSimilarity(pageName, pPattern); double distance = new LevenshteinStringDistance ( ) . distance ( pageName , pattern ) ; distanceMap . put ( pageID , distance ) ; // if there are more than "pSize" entries in the map remove the last one (it has the biggest distance) if ( distanceMap . size ( ) > pSize ) { Set < Map . Entry < Integer , Double > > valueSortedSet = new TreeSet < Map . Entry < Integer , Double > > ( new ValueComparator ( ) ) ; valueSortedSet . addAll ( distanceMap . entrySet ( ) ) ; Iterator it = valueSortedSet . iterator ( ) ; // remove the first element if ( it . hasNext ( ) ) { // get the id of this entry and remove it in the distanceMap Map . Entry entry = ( Map . Entry ) it . next ( ) ; distanceMap . remove ( entry . getKey ( ) ) ; } } } session . getTransaction ( ) . commit ( ) ; for ( int pageID : distanceMap . keySet ( ) ) { Page page = null ; try { page = this . getPage ( pageID ) ; } catch ( WikiPageNotFoundException e ) { logger . error ( "Page with pageID " + pageID + " could not be found. Fatal error. Terminating." ) ; e . printStackTrace ( ) ; System . exit ( 1 ) ; } pageMap . put ( page , distanceMap . get ( pageID ) ) ; } return pageMap ; }
Gets the pages or redirects with a name similar to the pattern . Calling this method is quite costly as similarity is computed for all names .