001    package jcolibri.method.retrieve.NNretrieval.similarity.local;
002    
003    
004    import jcolibri.method.retrieve.NNretrieval.similarity.LocalSimilarityFunction;
005    
006    /**
007     * This function returns a similarity value depending of the biggest substring
008     * that belong to both strings.
009     */
010    public class MaxString implements LocalSimilarityFunction {
011    
012    
013            /**
014             * Applies the similarity function.
015             * 
016             * @param s
017             *            String.
018             * @param t
019             *            String.
020             * @return result of apply the similarity funciton.
021             */
022            public double compute(Object s, Object t) throws jcolibri.exception.NoApplicableSimilarityFunctionException
023    {
024                    if ((s == null) || (t == null))
025                            return 0;
026                    if (!(s instanceof java.lang.String))
027                            throw new jcolibri.exception.NoApplicableSimilarityFunctionException(this.getClass(), s.getClass());
028                    if (!(t instanceof java.lang.String))
029                            throw new jcolibri.exception.NoApplicableSimilarityFunctionException(this.getClass(), s.getClass());
030    
031    
032                    String news = (String) s;
033                    String newt = (String) t;
034                    if (news.equals(newt))
035                            return 1.0;
036                    else
037                            return ((double) MaxSubString(news, newt) / (double) Math.max(news
038                                            .length(), newt.length()));
039            }
040    
041            /**
042             * Returns the length of the biggest substring that belong to both strings.
043             * 
044             * @param s
045             * @param t
046             * @return
047             */
048            private int MaxSubString(String s, String t) {
049                    String shorter = (s.length() > t.length()) ? t : s;
050                    String longer = (shorter.equals(s)) ? t : s;
051                    int best = 0;
052                    for (int i = 0; i < shorter.length(); i++) {
053                            for (int j = shorter.length(); j > i; j--) {
054                                    if (longer.indexOf(shorter.substring(i, j)) != -1)
055                                            best = Math.max(best, j - i);
056                            }
057                    }
058                    return best;
059            }
060            
061            /** Applicable to String */
062            public boolean isApplicable(Object o1, Object o2)
063            {
064                    if((o1==null)&&(o2==null))
065                            return true;
066                    else if(o1==null)
067                            return o2 instanceof String;
068                    else if(o2==null)
069                            return o1 instanceof String;
070                    else
071                            return (o1 instanceof String)&&(o2 instanceof String);
072            }
073    }