lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From synhers...@apache.org
Subject svn commit: r1375234 - in /incubator/lucene.net/trunk/src/contrib/Spatial: BBox/ BBox/AreaSimilarity.cs BBox/BBoxSimilarity.cs BBox/BBoxSimilarityValueSource.cs BBox/BBoxStrategy.cs Contrib.Spatial.csproj
Date Mon, 20 Aug 2012 21:38:08 GMT
Author: synhershko
Date: Mon Aug 20 21:38:07 2012
New Revision: 1375234

URL: http://svn.apache.org/viewvc?rev=1375234&view=rev
Log:
Adding BBox stuff (minus BBoxStrategy still incomplete)

Added:
    incubator/lucene.net/trunk/src/contrib/Spatial/BBox/
    incubator/lucene.net/trunk/src/contrib/Spatial/BBox/AreaSimilarity.cs
    incubator/lucene.net/trunk/src/contrib/Spatial/BBox/BBoxSimilarity.cs
    incubator/lucene.net/trunk/src/contrib/Spatial/BBox/BBoxSimilarityValueSource.cs
    incubator/lucene.net/trunk/src/contrib/Spatial/BBox/BBoxStrategy.cs
Modified:
    incubator/lucene.net/trunk/src/contrib/Spatial/Contrib.Spatial.csproj

Added: incubator/lucene.net/trunk/src/contrib/Spatial/BBox/AreaSimilarity.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/src/contrib/Spatial/BBox/AreaSimilarity.cs?rev=1375234&view=auto
==============================================================================
--- incubator/lucene.net/trunk/src/contrib/Spatial/BBox/AreaSimilarity.cs (added)
+++ incubator/lucene.net/trunk/src/contrib/Spatial/BBox/AreaSimilarity.cs Mon Aug 20 21:38:07
2012
@@ -0,0 +1,237 @@
+/*
+ * 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.
+ */
+
+using System;
+using Lucene.Net.Search;
+using Spatial4n.Core.Shapes;
+
+namespace Lucene.Net.Spatial.BBox
+{
+	/// <summary>
+	/**
+	 * The algorithm is implemented as envelope on envelope overlays rather than
+	 * complex polygon on complex polygon overlays.
+	 * <p/>
+	 * <p/>
+	 * Spatial relevance scoring algorithm:
+	 * <p/>
+	 * <br/>  queryArea = the area of the input query envelope
+	 * <br/>  targetArea = the area of the target envelope (per Lucene document)
+	 * <br/>  intersectionArea = the area of the intersection for the query/target envelopes
+	 * <br/>  queryPower = the weighting power associated with the query envelope (default
= 1.0)
+	 * <br/>  targetPower =  the weighting power associated with the target envelope (default
= 1.0)
+	 * <p/>
+	 * <br/>  queryRatio  = intersectionArea / queryArea;
+	 * <br/>  targetRatio = intersectionArea / targetArea;
+	 * <br/>  queryFactor  = Math.pow(queryRatio,queryPower);
+	 * <br/>  targetFactor = Math.pow(targetRatio,targetPower);
+	 * <br/>  score = queryFactor * targetFactor;
+	 * <p/>
+	 * Based on Geoportal's
+	 * <a href="http://geoportal.svn.sourceforge.net/svnroot/geoportal/Geoportal/trunk/src/com/esri/gpt/catalog/lucene/SpatialRankingValueSource.java">
+	 *   SpatialRankingValueSource</a>.
+	 *
+	 * @lucene.experimental
+	 */
+	/// </summary>
+	public class AreaSimilarity : BBoxSimilarity
+	{
+	   /**
+		* Properties associated with the query envelope
+		*/
+		private Rectangle queryExtent;
+		private double queryArea;
+
+		private double targetPower;
+		private double queryPower;
+
+		public AreaSimilarity(Rectangle queryExtent, double queryPower, double targetPower)
+		{
+			Init(queryExtent, queryPower, targetPower);
+		}
+
+		public AreaSimilarity(Rectangle queryExtent)
+		{
+			Init(queryExtent, 2.0, 0.5);
+		}
+
+		public void Init(Rectangle queryExtent, double queryPower, double targetPower)
+		{
+			this.queryExtent = queryExtent;
+			this.queryArea = queryExtent.GetArea();
+
+			this.queryPower = queryPower;
+			this.targetPower = targetPower;
+
+			//  if (this.qryMinX > queryExtent.getMaxX()) {
+			//    this.qryCrossedDateline = true;
+			//    this.qryArea = Math.abs(qryMaxX + 360.0 - qryMinX) * Math.abs(qryMaxY - qryMinY);
+			//  } else {
+			//    this.qryArea = Math.abs(qryMaxX - qryMinX) * Math.abs(qryMaxY - qryMinY);
+			//  }
+		}
+
+		public String GetDelimiterQueryParameters()
+		{
+			return queryExtent + ";" + queryPower + ";" + targetPower;
+		}
+
+		public double Score(Rectangle target, Explanation exp)
+		{
+			if (target == null || queryArea <= 0)
+			{
+				return 0;
+			}
+			double targetArea = target.GetArea();
+			if (targetArea <= 0)
+			{
+				return 0;
+			}
+			double score = 0;
+
+			double top = Math.Min(queryExtent.GetMaxY(), target.GetMaxY());
+			double bottom = Math.Max(queryExtent.GetMinY(), target.GetMinY());
+			double height = top - bottom;
+			double width = 0;
+
+			// queries that cross the date line
+			if (queryExtent.GetCrossesDateLine())
+			{
+				// documents that cross the date line
+				if (target.GetCrossesDateLine())
+				{
+					double left = Math.Max(queryExtent.GetMinX(), target.GetMinX());
+					double right = Math.Min(queryExtent.GetMaxX(), target.GetMaxX());
+					width = right + 360.0 - left;
+				}
+				else
+				{
+					double qryWestLeft = Math.Max(queryExtent.GetMinX(), target.GetMaxX());
+					double qryWestRight = Math.Min(target.GetMaxX(), 180.0);
+					double qryWestWidth = qryWestRight - qryWestLeft;
+					if (qryWestWidth > 0)
+					{
+						width = qryWestWidth;
+					}
+					else
+					{
+						double qryEastLeft = Math.Max(target.GetMaxX(), -180.0);
+						double qryEastRight = Math.Min(queryExtent.GetMaxX(), target.GetMaxX());
+						double qryEastWidth = qryEastRight - qryEastLeft;
+						if (qryEastWidth > 0)
+						{
+							width = qryEastWidth;
+						}
+					}
+				}
+			}
+			else
+			{ // queries that do not cross the date line
+
+				if (target.GetCrossesDateLine())
+				{
+					double tgtWestLeft = Math.Max(queryExtent.GetMinX(), target.GetMinX());
+					double tgtWestRight = Math.Min(queryExtent.GetMaxX(), 180.0);
+					double tgtWestWidth = tgtWestRight - tgtWestLeft;
+					if (tgtWestWidth > 0)
+					{
+						width = tgtWestWidth;
+					}
+					else
+					{
+						double tgtEastLeft = Math.Max(queryExtent.GetMinX(), -180.0);
+						double tgtEastRight = Math.Min(queryExtent.GetMaxX(), target.GetMaxX());
+						double tgtEastWidth = tgtEastRight - tgtEastLeft;
+						if (tgtEastWidth > 0)
+						{
+							width = tgtEastWidth;
+						}
+					}
+				}
+				else
+				{
+					double left = Math.Max(queryExtent.GetMinX(), target.GetMinX());
+					double right = Math.Min(queryExtent.GetMaxX(), target.GetMaxX());
+					width = right - left;
+				}
+			}
+
+
+			// calculate the score
+			if ((width > 0) && (height > 0))
+			{
+				double intersectionArea = width * height;
+				double queryRatio = intersectionArea / queryArea;
+				double targetRatio = intersectionArea / targetArea;
+				double queryFactor = Math.Pow(queryRatio, queryPower);
+				double targetFactor = Math.Pow(targetRatio, targetPower);
+				score = queryFactor * targetFactor * 10000.0;
+
+				if (exp != null)
+				{
+					//        StringBuilder sb = new StringBuilder();
+					//        sb.append("\nscore=").append(score);
+					//        sb.append("\n  query=").append();
+					//        sb.append("\n  target=").append(target.toString());
+					//        sb.append("\n  intersectionArea=").append(intersectionArea);
+					//        
+					//        sb.append(" queryArea=").append(queryArea).append(" targetArea=").append(targetArea);
+					//        sb.append("\n  queryRatio=").append(queryRatio).append(" targetRatio=").append(targetRatio);
+					//        sb.append("\n  queryFactor=").append(queryFactor).append(" targetFactor=").append(targetFactor);
+					//        sb.append(" (queryPower=").append(queryPower).append(" targetPower=").append(targetPower).append(")");
+
+					exp.Value = (float) score;
+					exp.Description = GetType().Name;
+
+					Explanation e = null;
+
+					exp.AddDetail(e = new Explanation((float)intersectionArea, "IntersectionArea"));
+					e.AddDetail(new Explanation((float)width, "width; Query: " + queryExtent));
+					e.AddDetail(new Explanation((float)height, "height; Target: " + target));
+
+					exp.AddDetail(e = new Explanation((float)queryFactor, "Query"));
+					e.AddDetail(new Explanation((float)queryArea, "area"));
+					e.AddDetail(new Explanation((float)queryRatio, "ratio"));
+					e.AddDetail(new Explanation((float)queryPower, "power"));
+
+					exp.AddDetail(e = new Explanation((float)targetFactor, "Target"));
+					e.AddDetail(new Explanation((float)targetArea, "area"));
+					e.AddDetail(new Explanation((float)targetRatio, "ratio"));
+					e.AddDetail(new Explanation((float)targetPower, "power"));
+				}
+			}
+			else if (exp != null)
+			{
+				exp.Value = 0;
+				exp.Description = "Shape does not intersect";
+			}
+			return score;
+		}
+
+		public override bool Equals(object obj)
+		{
+			var other = obj as AreaSimilarity;
+			if (other == null) return false;
+			return GetDelimiterQueryParameters().Equals(other.GetDelimiterQueryParameters());
+		}
+
+		public override int GetHashCode()
+		{
+			return GetDelimiterQueryParameters().GetHashCode();
+		} 
+	}
+}

Added: incubator/lucene.net/trunk/src/contrib/Spatial/BBox/BBoxSimilarity.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/src/contrib/Spatial/BBox/BBoxSimilarity.cs?rev=1375234&view=auto
==============================================================================
--- incubator/lucene.net/trunk/src/contrib/Spatial/BBox/BBoxSimilarity.cs (added)
+++ incubator/lucene.net/trunk/src/contrib/Spatial/BBox/BBoxSimilarity.cs Mon Aug 20 21:38:07
2012
@@ -0,0 +1,27 @@
+/*
+ * 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.
+ */
+
+using Lucene.Net.Search;
+using Spatial4n.Core.Shapes;
+
+namespace Lucene.Net.Spatial.BBox
+{
+	public interface BBoxSimilarity
+	{
+		double Score(Rectangle extent, Explanation exp);
+	}
+}

Added: incubator/lucene.net/trunk/src/contrib/Spatial/BBox/BBoxSimilarityValueSource.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/src/contrib/Spatial/BBox/BBoxSimilarityValueSource.cs?rev=1375234&view=auto
==============================================================================
--- incubator/lucene.net/trunk/src/contrib/Spatial/BBox/BBoxSimilarityValueSource.cs (added)
+++ incubator/lucene.net/trunk/src/contrib/Spatial/BBox/BBoxSimilarityValueSource.cs Mon Aug
20 21:38:07 2012
@@ -0,0 +1,118 @@
+/*
+ * 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.
+ */
+
+using Lucene.Net.Index;
+using Lucene.Net.Search;
+using Lucene.Net.Search.Function;
+using Lucene.Net.Spatial.Util;
+using Spatial4n.Core.Shapes;
+using Spatial4n.Core.Shapes.Impl;
+
+namespace Lucene.Net.Spatial.BBox
+{
+	public class BBoxSimilarityValueSource : ValueSource
+	{
+		private readonly BBoxStrategy strategy;
+		private readonly BBoxSimilarity similarity;
+
+		public BBoxSimilarityValueSource(BBoxStrategy strategy, BBoxSimilarity similarity)
+		{
+			this.strategy = strategy;
+			this.similarity = similarity;
+		}
+
+		private class BBoxSimilarityValueSourceDocValues : DocValues
+		{
+			private readonly BBoxSimilarityValueSource _enclosingInstance;
+			private readonly double[] minX;
+			private readonly double[] minY;
+			private readonly double[] maxX;
+			private readonly double[] maxY;
+
+			private readonly Bits validMinX;
+			private readonly Bits validMaxX;
+
+			public BBoxSimilarityValueSourceDocValues(IndexReader reader, BBoxSimilarityValueSource
enclosingInstance)
+			{
+				_enclosingInstance = enclosingInstance;
+
+				minX = FieldCache_Fields.DEFAULT.GetDoubles(reader, enclosingInstance.strategy.field_minX,
true);
+				minY = FieldCache_Fields.DEFAULT.GetDoubles(reader, enclosingInstance.strategy.field_minY,
true);
+				maxX = FieldCache_Fields.DEFAULT.GetDoubles(reader, enclosingInstance.strategy.field_maxX,
true);
+				maxY = FieldCache_Fields.DEFAULT.GetDoubles(reader, enclosingInstance.strategy.field_maxY,
true);
+
+				validMinX = FieldCache_Fields.DEFAULT.GetDoubles(reader, enclosingInstance.strategy.field_minX);
+				validMaxX = FieldCache_Fields.DEFAULT.GetDoubles(reader, enclosingInstance.strategy.field_maxX);
+			}
+
+			public override float FloatVal(int doc)
+			{
+				// make sure it has minX and area
+				if (validMinX.Get(doc) && validMaxX.Get(doc))
+				{
+					Rectangle rect = new RectangleImpl(
+						minX[doc], maxX[doc],
+						minY[doc], maxY[doc]);
+					return (float)_enclosingInstance.similarity.Score(rect, null);
+				}
+				return 0;
+			}
+
+			public override Explanation Explain(int doc)
+			{
+				// make sure it has minX and area
+				if (validMinX.Get(doc) && validMaxX.Get(doc))
+				{
+					Rectangle rect = new RectangleImpl(
+						minX[doc], maxX[doc],
+						minY[doc], maxY[doc]);
+					var exp = new Explanation();
+					_enclosingInstance.similarity.Score(rect, exp);
+					return exp;
+				}
+				return new Explanation(0, "No BBox");
+			}
+
+			public override string ToString(int doc)
+			{
+				return _enclosingInstance.Description() + "=" + FloatVal(doc);
+			}
+		}
+
+		public override DocValues GetValues(IndexReader reader)
+		{
+			return new BBoxSimilarityValueSourceDocValues(reader, this);
+		}
+
+		public override string Description()
+		{
+			return "BBoxSimilarityValueSource(" + similarity + ")";
+		}
+
+		public override bool Equals(object o)
+		{
+			var other = o as BBoxSimilarityValueSource;
+			if (other == null) return false;
+			return similarity.Equals(other.similarity);
+		}
+
+		public override int GetHashCode()
+		{
+			return typeof(BBoxSimilarityValueSource).GetHashCode() + similarity.GetHashCode();
+		}
+	}
+}

Added: incubator/lucene.net/trunk/src/contrib/Spatial/BBox/BBoxStrategy.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/src/contrib/Spatial/BBox/BBoxStrategy.cs?rev=1375234&view=auto
==============================================================================
--- incubator/lucene.net/trunk/src/contrib/Spatial/BBox/BBoxStrategy.cs (added)
+++ incubator/lucene.net/trunk/src/contrib/Spatial/BBox/BBoxStrategy.cs Mon Aug 20 21:38:07
2012
@@ -0,0 +1,93 @@
+/*
+ * 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.
+ */
+
+using System;
+using Lucene.Net.Documents;
+using Lucene.Net.Search;
+using Lucene.Net.Search.Function;
+using Spatial4n.Core.Context;
+using Spatial4n.Core.Query;
+using Spatial4n.Core.Shapes;
+
+namespace Lucene.Net.Spatial.BBox
+{
+	public class BBoxStrategy : SpatialStrategy<SimpleSpatialFieldInfo>
+	{
+		public static String SUFFIX_MINX = "__minX";
+		public static String SUFFIX_MAXX = "__maxX";
+		public static String SUFFIX_MINY = "__minY";
+		public static String SUFFIX_MAXY = "__maxY";
+		public static String SUFFIX_XDL = "__xdl";
+
+		/*
+		 * The Bounding Box gets stored as four fields for x/y min/max and a flag
+		 * that says if the box crosses the dateline (xdl).
+		 */
+		public readonly String field_bbox;
+		public readonly String field_minX;
+		public readonly String field_minY;
+		public readonly String field_maxX;
+		public readonly String field_maxY;
+		public readonly String field_xdl; // crosses dateline
+
+		public readonly double queryPower = 1.0;
+		public readonly double targetPower = 1.0f;
+		public int precisionStep = 8; // same as solr default
+
+		public BBoxStrategy(SpatialContext ctx, String fieldNamePrefix)
+			: base(ctx/*, fieldNamePrefix*/)
+		{
+			field_bbox = fieldNamePrefix;
+			field_minX = fieldNamePrefix + SUFFIX_MINX;
+			field_maxX = fieldNamePrefix + SUFFIX_MAXX;
+			field_minY = fieldNamePrefix + SUFFIX_MINY;
+			field_maxY = fieldNamePrefix + SUFFIX_MAXY;
+			field_xdl = fieldNamePrefix + SUFFIX_XDL;
+		}
+
+		public void SetPrecisionStep(int p)
+		{
+			precisionStep = p;
+			if (precisionStep <= 0 || precisionStep >= 64)
+				precisionStep = int.MaxValue;
+		}
+
+		//---------------------------------
+		// Indexing
+		//---------------------------------
+
+		public override Field CreateField(SimpleSpatialFieldInfo fieldInfo, Shape shape, bool index,
bool store)
+		{
+			throw new NotImplementedException();
+		}
+
+		public override ValueSource MakeValueSource(SpatialArgs args, SimpleSpatialFieldInfo fieldInfo)
+		{
+			throw new NotImplementedException();
+		}
+
+		public override Query MakeQuery(SpatialArgs args, SimpleSpatialFieldInfo fieldInfo)
+		{
+			throw new NotImplementedException();
+		}
+
+		public override Filter MakeFilter(SpatialArgs args, SimpleSpatialFieldInfo fieldInfo)
+		{
+			throw new NotImplementedException();
+		}
+	}
+}

Modified: incubator/lucene.net/trunk/src/contrib/Spatial/Contrib.Spatial.csproj
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/src/contrib/Spatial/Contrib.Spatial.csproj?rev=1375234&r1=1375233&r2=1375234&view=diff
==============================================================================
--- incubator/lucene.net/trunk/src/contrib/Spatial/Contrib.Spatial.csproj (original)
+++ incubator/lucene.net/trunk/src/contrib/Spatial/Contrib.Spatial.csproj Mon Aug 20 21:38:07
2012
@@ -30,7 +30,8 @@
     <RootNamespace>Lucene.Net.Spatial</RootNamespace>
     <AssemblyName>Lucene.Net.Contrib.Spatial</AssemblyName>
     <FileAlignment>512</FileAlignment>
-    <FileUpgradeFlags></FileUpgradeFlags>
+    <FileUpgradeFlags>
+    </FileUpgradeFlags>
     <OldToolsVersion>3.5</OldToolsVersion>
     <UpgradeBackupLocation />
     <PublishUrl>publish\</PublishUrl>
@@ -59,7 +60,8 @@
     <DefineConstants>DEBUG;TRACE;$(Framework)</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>
-    <DocumentationFile></DocumentationFile>
+    <DocumentationFile>
+    </DocumentationFile>
     <NoWarn>618</NoWarn>
     <OutputType>Library</OutputType>
   </PropertyGroup>
@@ -73,7 +75,8 @@
     <DefineConstants>DEBUG;TRACE;$(Framework)</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>
-    <DocumentationFile></DocumentationFile>
+    <DocumentationFile>
+    </DocumentationFile>
     <NoWarn>618</NoWarn>
     <OutputType>Library</OutputType>
   </PropertyGroup>
@@ -121,6 +124,10 @@
     <Reference Include="System.Data" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="BBox\AreaSimilarity.cs" />
+    <Compile Include="BBox\BBoxSimilarity.cs" />
+    <Compile Include="BBox\BBoxSimilarityValueSource.cs" />
+    <Compile Include="BBox\BBoxStrategy.cs" />
     <Compile Include="Prefix\PointPrefixTreeFieldCacheProvider.cs" />
     <Compile Include="Prefix\PrefixCellsTokenizer.cs" />
     <Compile Include="Prefix\PrefixTreeStrategy.cs" />



Mime
View raw message