[text] Initial implementation of the cosine distance for strings (not sequences)
Repository: commons-text Updated Branches: refs/heads/NEW-METRICS 0404dbf4b -> ff1959c84 Initial implementation of the cosine distance for strings (not sequences) Project: http://git-wip-us.apache.org/repos/asf/commons-text/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-text/commit/ff1959c8 Tree: http://git-wip-us.apache.org/repos/asf/commons-text/tree/ff1959c8 Diff: http://git-wip-us.apache.org/repos/asf/commons-text/diff/ff1959c8 Branch: refs/heads/NEW-METRICS Commit: ff1959c84dce2eac6f2e8432623d2a2a270a5f32 Parents: 0404dbf Author: Bruno P. Kinoshita Authored: Sat Feb 21 22:22:28 2015 -0200 Committer: Bruno P. Kinoshita Committed: Sat Feb 21 22:22:28 2015 -0200 -- .../text/similarity/CosineSimilarity.java | 48 +- .../text/similarity/CosineSimilarityTest.java | 51 2 files changed, 98 insertions(+), 1 deletion(-) -- http://git-wip-us.apache.org/repos/asf/commons-text/blob/ff1959c8/src/main/java/org/apache/commons/text/similarity/CosineSimilarity.java -- diff --git a/src/main/java/org/apache/commons/text/similarity/CosineSimilarity.java b/src/main/java/org/apache/commons/text/similarity/CosineSimilarity.java index 4589c2d..ca9d087 100644 --- a/src/main/java/org/apache/commons/text/similarity/CosineSimilarity.java +++ b/src/main/java/org/apache/commons/text/similarity/CosineSimilarity.java @@ -16,6 +16,52 @@ */ package org.apache.commons.text.similarity; -public class CosineSimilarity { +/** + * Measures the Cosine similarity of two CharSequences. It treats the CharSequences as + * two vectors of an inner product space and compares the angle between them. + * + * + * For further explanation about the Cosine Similarity, take a look at its + * Wikipedia page at http://en.wikipedia.org/wiki/Cosine_similarity. + * + * + * @since 0.1 + */ +public class CosineSimilarity implements StringMetric { + +@Override +public Double compare(CharSequence left, CharSequence right) { +if (left == null || right == null) { +throw new IllegalArgumentException("String parameters must not be null"); +} +long dotProduct = dot(left, right); +double d1 = 0.0d; +for (int i = 0; i < left.length(); ++i) { +d1 += Math.pow(((int) left.charAt(i)), 2); +} +double d2 = 0.0d; +for (int i = 0; i < right.length(); ++i) { +d2 += Math.pow(((int) right.charAt(i)), 2); +} +double cosineSimilarity = dotProduct / (double) (Math.sqrt(d1) * Math.sqrt(d2)); +return cosineSimilarity; +} + +/** + * Computes the dot product of two CharSequences. It ignores remaining characters. It means + * that if a string is longer than other, then a smaller part of it will be used to compute + * the dot product. + * + * @param left left string + * @param right right string + * @return the dot product + */ +protected long dot(CharSequence left, CharSequence right) { +long dotProduct = 0; +for (int i = 0; i < left.length() && i < right.length(); ++i) { +dotProduct += (((int) left.charAt(i)) * ((int) right.charAt(i))); +} +return dotProduct; +} } http://git-wip-us.apache.org/repos/asf/commons-text/blob/ff1959c8/src/test/java/org/apache/commons/text/similarity/CosineSimilarityTest.java -- diff --git a/src/test/java/org/apache/commons/text/similarity/CosineSimilarityTest.java b/src/test/java/org/apache/commons/text/similarity/CosineSimilarityTest.java new file mode 100644 index 000..aa08057 --- /dev/null +++ b/src/test/java/org/apache/commons/text/similarity/CosineSimilarityTest.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.text.similarity; + +import static org.junit.Assert.assertEquals; + +import java.math.BigDecimal; +import java.math.RoundingMode; + +import org.junit.BeforeClass;
[text] Fix package in Javadoc
Repository: commons-text Updated Branches: refs/heads/NEW-METRICS e099c56ea -> 0404dbf4b Fix package in Javadoc Project: http://git-wip-us.apache.org/repos/asf/commons-text/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-text/commit/0404dbf4 Tree: http://git-wip-us.apache.org/repos/asf/commons-text/tree/0404dbf4 Diff: http://git-wip-us.apache.org/repos/asf/commons-text/diff/0404dbf4 Branch: refs/heads/NEW-METRICS Commit: 0404dbf4b15782634b6816d8499ebd64c7aafc48 Parents: e099c56 Author: Bruno P. Kinoshita Authored: Sat Feb 21 22:21:39 2015 -0200 Committer: Bruno P. Kinoshita Committed: Sat Feb 21 22:21:39 2015 -0200 -- .../java/org/apache/commons/text/similarity/FuzzyScoreTest.java| 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) -- http://git-wip-us.apache.org/repos/asf/commons-text/blob/0404dbf4/src/test/java/org/apache/commons/text/similarity/FuzzyScoreTest.java -- diff --git a/src/test/java/org/apache/commons/text/similarity/FuzzyScoreTest.java b/src/test/java/org/apache/commons/text/similarity/FuzzyScoreTest.java index b2fab14..b6519e8 100644 --- a/src/test/java/org/apache/commons/text/similarity/FuzzyScoreTest.java +++ b/src/test/java/org/apache/commons/text/similarity/FuzzyScoreTest.java @@ -24,7 +24,7 @@ import org.junit.BeforeClass; import org.junit.Test; /** - * Unit tests for {@link org.apache.commons.text.FuzzyScore}. + * Unit tests for {@link org.apache.commons.text.similarity.FuzzyScore}. */ public class FuzzyScoreTest {
svn commit: r1661447 - /commons/proper/bcel/trunk/src/main/java/org/apache/bcel/generic/InstructionFactory.java
Author: ebourg Date: Sat Feb 21 22:45:02 2015 New Revision: 1661447 URL: http://svn.apache.org/r1661447 Log: Removed unnecessary casts in InstructionFactory Modified: commons/proper/bcel/trunk/src/main/java/org/apache/bcel/generic/InstructionFactory.java Modified: commons/proper/bcel/trunk/src/main/java/org/apache/bcel/generic/InstructionFactory.java URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/main/java/org/apache/bcel/generic/InstructionFactory.java?rev=1661447&r1=1661446&r2=1661447&view=diff == --- commons/proper/bcel/trunk/src/main/java/org/apache/bcel/generic/InstructionFactory.java (original) +++ commons/proper/bcel/trunk/src/main/java/org/apache/bcel/generic/InstructionFactory.java Sat Feb 21 22:45:02 2015 @@ -304,9 +304,7 @@ public class InstructionFactory implemen case '<': return ISHL; case '>': -return op.equals(">>>") -? (ArithmeticInstruction) IUSHR -: (ArithmeticInstruction) ISHR; +return op.equals(">>>") ? IUSHR : ISHR; default: throw new RuntimeException("Invalid operand " + op); } @@ -334,9 +332,7 @@ public class InstructionFactory implemen case '<': return LSHL; case '>': -return op.equals(">>>") -? (ArithmeticInstruction) LUSHR -: (ArithmeticInstruction) LSHR; +return op.equals(">>>") ? LUSHR : LSHR; default: throw new RuntimeException("Invalid operand " + op); } @@ -408,7 +404,7 @@ public class InstructionFactory implemen * @param size size of operand, either 1 (int, e.g.) or 2 (double) */ public static StackInstruction createPop( int size ) { -return (size == 2) ? (StackInstruction) POP2 : (StackInstruction) POP; +return (size == 2) ? POP2 : POP; } @@ -416,7 +412,7 @@ public class InstructionFactory implemen * @param size size of operand, either 1 (int, e.g.) or 2 (double) */ public static StackInstruction createDup( int size ) { -return (size == 2) ? (StackInstruction) DUP2 : (StackInstruction) DUP; +return (size == 2) ? DUP2 : DUP; } @@ -424,7 +420,7 @@ public class InstructionFactory implemen * @param size size of operand, either 1 (int, e.g.) or 2 (double) */ public static StackInstruction createDup_2( int size ) { -return (size == 2) ? (StackInstruction) DUP2_X2 : (StackInstruction) DUP_X2; +return (size == 2) ? DUP2_X2 : DUP_X2; } @@ -432,7 +428,7 @@ public class InstructionFactory implemen * @param size size of operand, either 1 (int, e.g.) or 2 (double) */ public static StackInstruction createDup_1( int size ) { -return (size == 2) ? (StackInstruction) DUP2_X1 : (StackInstruction) DUP_X1; +return (size == 2) ? DUP2_X1 : DUP_X1; } @@ -636,7 +632,7 @@ public class InstructionFactory implemen } else if (t instanceof ArrayType) { return new ANEWARRAY(cp.addArrayClass((ArrayType) t)); } else { -return new NEWARRAY(((BasicType) t).getType()); +return new NEWARRAY(t.getType()); } } else { ArrayType at;
svn commit: r1661444 - in /commons/proper/pool/trunk/src: changes/ main/java/org/apache/commons/pool2/impl/ test/java/org/apache/commons/pool2/impl/
Author: psteitz Date: Sat Feb 21 22:08:06 2015 New Revision: 1661444 URL: http://svn.apache.org/r1661444 Log: Fixed GKOP evictor capacity leak. JIRA: POOL-287. Reported by Caleb Spare. Patched by Thomas Neidhart. Modified: commons/proper/pool/trunk/src/changes/changes.xml commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/impl/TestGenericKeyedObjectPool.java Modified: commons/proper/pool/trunk/src/changes/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/changes/changes.xml?rev=1661444&r1=1661443&r2=1661444&view=diff == --- commons/proper/pool/trunk/src/changes/changes.xml (original) +++ commons/proper/pool/trunk/src/changes/changes.xml Sat Feb 21 22:08:06 2015 @@ -44,6 +44,10 @@ The type attribute can be add,u + + Fixed capacity leak when an object is offered from a GenericKeyedObjectPool while it is + being validated by the evictor. + Modified: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java?rev=1661444&r1=1661443&r2=1661444&view=diff == --- commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java (original) +++ commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java Sat Feb 21 22:08:06 2015 @@ -21,6 +21,7 @@ import java.io.StringWriter; import java.io.Writer; import java.lang.management.ManagementFactory; import java.lang.ref.WeakReference; +import java.util.Deque; import java.util.Iterator; import java.util.TimerTask; import java.util.concurrent.atomic.AtomicLong; @@ -91,7 +92,7 @@ public abstract class BaseGenericObjectP volatile boolean closed = false; final Object evictionLock = new Object(); private Evictor evictor = null; // @GuardedBy("evictionLock") -Iterator> evictionIterator = null; // @GuardedBy("evictionLock") +EvictionIterator evictionIterator = null; // @GuardedBy("evictionLock") /* * Class loader for evictor thread to use since, in a JavaEE or similar * environment, the context class loader for the evictor thread may not have @@ -1100,4 +1101,55 @@ public abstract class BaseGenericObjectP return (long) result; } } + +/** + * The idle object eviction iterator. Holds a reference to the idle objects. + */ +class EvictionIterator implements Iterator> { + +private final Deque> idleObjects; +private final Iterator> idleObjectIterator; + +/** + * Create an EvictionIterator for the provided idle instance deque. + * @param idleObjects underlying deque + */ +EvictionIterator(final Deque> idleObjects) { +this.idleObjects = idleObjects; + +if (getLifo()) { +idleObjectIterator = idleObjects.descendingIterator(); +} else { +idleObjectIterator = idleObjects.iterator(); +} +} + +/** + * Returns the idle object deque referenced by this iterator. + * @return the idle object deque + */ +public Deque> getIdleObjects() { +return idleObjects; +} + +/** {@inheritDoc} */ +@Override +public boolean hasNext() { +return idleObjectIterator.hasNext(); +} + +/** {@inheritDoc} */ +@Override +public PooledObject next() { +return idleObjectIterator.next(); +} + +/** {@inheritDoc} */ +@Override +public void remove() { +idleObjectIterator.remove(); +} + +} + } Modified: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java?rev=1661444&r1=1661443&r2=1661444&view=diff == --- commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java (original) +++ commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java Sat Feb 21 22:08:06 2015 @@ -17,6 +17,7 @@ package org.apache.commons.pool2.impl; import java.util.ArrayList; +import java.util.Deque; import ja