Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def extract_polyglot(self, text):
try:
parsed = Text(text)
lang = parsed.language
if lang.confidence > 90:
yield self.make_entity(lang.code,
ExtractedEntity.LANGUAGE,
0, len(text))
if lang.code not in POLYGLOT_LANGUAGES:
return
for entity in parsed.entities:
label = ' '.join(entity)
type_ = POLYGLOT_TYPES.get(entity.tag)
if type_ is not None:
yield self.make_entity(label, type_,
entity.start,
entity.end)
except Exception as ex:
log.warning("Polyglot failed: %s" % ex)
if self.valid and ' ' not in self.label:
self.valid = False
class LocationResult(NamedResult):
"""Locations are being mapped to countries."""
resolver = LocationResolver()
category = ExtractedEntity.LOCATION
def __init__(self, ctx, label, start, end):
super(LocationResult, self).__init__(ctx, label, start, end)
self.countries = self.resolver.get_countries(label)
class LanguageResult(Result):
category = ExtractedEntity.LANGUAGE
def __init__(self, ctx, label, start, end):
label = label.strip().lower()
super(LanguageResult, self).__init__(ctx, label, start, end)
class IPAddressResult(Result):
"""Pull IPv4, IPv6 - and validate using on-board Python tools."""
category = ExtractedEntity.IPADDRESS
def __init__(self, ctx, label, start, end):
super(IPAddressResult, self).__init__(ctx, label, start, end)
try:
ip = ip_address(label)
self.key = self.label = str(ip)
except ValueError: