| 1 |
# Copyright DataStax, Inc. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
import math |
| 16 |
|
| 17 |
from gremlin_python.process.traversal import P |
| 18 |
|
| 19 |
from cassandra.util import Distance |
| 20 |
|
| 21 |
__all__ = ['GeoP', 'TextDistanceP', 'Search', 'GeoUnit', 'Geo', 'CqlCollection'] |
| 22 |
|
| 23 |
|
| 24 |
class GeoP(object): |
| 25 |
|
| 26 |
def __init__(self, operator, value, other=None): |
| 27 |
self.operator = operator |
| 28 |
self.value = value |
| 29 |
self.other = other |
| 30 |
|
| 31 |
@staticmethod |
| 32 |
def inside(*args, **kwargs): |
| 33 |
return GeoP("inside", *args, **kwargs) |
| 34 |
|
| 35 |
def __eq__(self, other): |
| 36 |
return isinstance(other, |
| 37 |
self.__class__) and self.operator == other.operator and self.value == other.value and self.other == other.other |
| 38 |
|
| 39 |
def __repr__(self): |
| 40 |
return self.operator + "(" + str(self.value) + ")" if self.other is None else self.operator + "(" + str( |
| 41 |
self.value) + "," + str(self.other) + ")" |
| 42 |
|
| 43 |
|
| 44 |
class TextDistanceP(object): |
| 45 |
|
| 46 |
def __init__(self, operator, value, distance): |
| 47 |
self.operator = operator |
| 48 |
self.value = value |
| 49 |
self.distance = distance |
| 50 |
|
| 51 |
@staticmethod |
| 52 |
def fuzzy(*args): |
| 53 |
return TextDistanceP("fuzzy", *args) |
| 54 |
|
| 55 |
@staticmethod |
| 56 |
def token_fuzzy(*args): |
| 57 |
return TextDistanceP("tokenFuzzy", *args) |
| 58 |
|
| 59 |
@staticmethod |
| 60 |
def phrase(*args): |
| 61 |
return TextDistanceP("phrase", *args) |
| 62 |
|
| 63 |
def __eq__(self, other): |
| 64 |
return isinstance(other, |
| 65 |
self.__class__) and self.operator == other.operator and self.value == other.value and self.distance == other.distance |
| 66 |
|
| 67 |
def __repr__(self): |
| 68 |
return self.operator + "(" + str(self.value) + "," + str(self.distance) + ")" |
| 69 |
|
| 70 |
|
| 71 |
class Search(object): |
| 72 |
|
| 73 |
@staticmethod |
| 74 |
def token(value): |
| 75 |
""" |
| 76 |
Search any instance of a certain token within the text property targeted. |
| 77 |
:param value: the value to look for. |
| 78 |
""" |
| 79 |
return P('token', value) |
| 80 |
|
| 81 |
@staticmethod |
| 82 |
def token_prefix(value): |
| 83 |
""" |
| 84 |
Search any instance of a certain token prefix withing the text property targeted. |
| 85 |
:param value: the value to look for. |
| 86 |
""" |
| 87 |
return P('tokenPrefix', value) |
| 88 |
|
| 89 |
@staticmethod |
| 90 |
def token_regex(value): |
| 91 |
""" |
| 92 |
Search any instance of the provided regular expression for the targeted property. |
| 93 |
:param value: the value to look for. |
| 94 |
""" |
| 95 |
return P('tokenRegex', value) |
| 96 |
|
| 97 |
@staticmethod |
| 98 |
def prefix(value): |
| 99 |
""" |
| 100 |
Search for a specific prefix at the beginning of the text property targeted. |
| 101 |
:param value: the value to look for. |
| 102 |
""" |
| 103 |
return P('prefix', value) |
| 104 |
|
| 105 |
@staticmethod |
| 106 |
def regex(value): |
| 107 |
""" |
| 108 |
Search for this regular expression inside the text property targeted. |
| 109 |
:param value: the value to look for. |
| 110 |
""" |
| 111 |
return P('regex', value) |
| 112 |
|
| 113 |
@staticmethod |
| 114 |
def fuzzy(value, distance): |
| 115 |
""" |
| 116 |
Search for a fuzzy string inside the text property targeted. |
| 117 |
:param value: the value to look for. |
| 118 |
:param distance: The distance for the fuzzy search. ie. 1, to allow a one-letter misspellings. |
| 119 |
""" |
| 120 |
return TextDistanceP.fuzzy(value, distance) |
| 121 |
|
| 122 |
@staticmethod |
| 123 |
def token_fuzzy(value, distance): |
| 124 |
""" |
| 125 |
Search for a token fuzzy inside the text property targeted. |
| 126 |
:param value: the value to look for. |
| 127 |
:param distance: The distance for the token fuzzy search. ie. 1, to allow a one-letter misspellings. |
| 128 |
""" |
| 129 |
return TextDistanceP.token_fuzzy(value, distance) |
| 130 |
|
| 131 |
@staticmethod |
| 132 |
def phrase(value, proximity): |
| 133 |
""" |
| 134 |
Search for a phrase inside the text property targeted. |
| 135 |
:param value: the value to look for. |
| 136 |
:param proximity: The proximity for the phrase search. ie. phrase('David Felcey', 2).. to find 'David Felcey' with up to two middle names. |
| 137 |
""" |
| 138 |
return TextDistanceP.phrase(value, proximity) |
| 139 |
|
| 140 |
|
| 141 |
class CqlCollection(object): |
| 142 |
|
| 143 |
@staticmethod |
| 144 |
def contains(value): |
| 145 |
""" |
| 146 |
Search for a value inside a cql list/set column. |
| 147 |
:param value: the value to look for. |
| 148 |
""" |
| 149 |
return P('contains', value) |
| 150 |
|
| 151 |
@staticmethod |
| 152 |
def contains_value(value): |
| 153 |
""" |
| 154 |
Search for a map value. |
| 155 |
:param value: the value to look for. |
| 156 |
""" |
| 157 |
return P('containsValue', value) |
| 158 |
|
| 159 |
@staticmethod |
| 160 |
def contains_key(value): |
| 161 |
""" |
| 162 |
Search for a map key. |
| 163 |
:param value: the value to look for. |
| 164 |
""" |
| 165 |
return P('containsKey', value) |
| 166 |
|
| 167 |
@staticmethod |
| 168 |
def entry_eq(value): |
| 169 |
""" |
| 170 |
Search for a map entry. |
| 171 |
:param value: the value to look for. |
| 172 |
""" |
| 173 |
return P('entryEq', value) |
| 174 |
|
| 175 |
|
| 176 |
class GeoUnit(object): |
| 177 |
_EARTH_MEAN_RADIUS_KM = 6371.0087714 |
| 178 |
_DEGREES_TO_RADIANS = math.pi / 180 |
| 179 |
_DEG_TO_KM = _DEGREES_TO_RADIANS * _EARTH_MEAN_RADIUS_KM |
| 180 |
_KM_TO_DEG = 1 / _DEG_TO_KM |
| 181 |
_MILES_TO_KM = 1.609344001 |
| 182 |
|
| 183 |
MILES = _MILES_TO_KM * _KM_TO_DEG |
| 184 |
KILOMETERS = _KM_TO_DEG |
| 185 |
METERS = _KM_TO_DEG / 1000.0 |
| 186 |
DEGREES = 1 |
| 187 |
|
| 188 |
|
| 189 |
class Geo(object): |
| 190 |
|
| 191 |
@staticmethod |
| 192 |
def inside(value, units=GeoUnit.DEGREES): |
| 193 |
""" |
| 194 |
Search any instance of geometry inside the Distance targeted. |
| 195 |
:param value: A Distance to look for. |
| 196 |
:param units: The units for ``value``. See GeoUnit enum. (Can also |
| 197 |
provide an integer to use as a multiplier to convert ``value`` to |
| 198 |
degrees.) |
| 199 |
""" |
| 200 |
return GeoP.inside( |
| 201 |
value=Distance(x=value.x, y=value.y, radius=value.radius * units) |
| 202 |
) |