Do magic matching of single words or quoted string

def _string_to_dictsql(self, part):
        """ Do magic matching of single words or quoted string
        """
        self._logger.debug("parsing string: " + unicode(part[0]) + " of type: " + part.getName())

        if part.getName() == 'tag':
            self._logger.debug("Query part '" + part[0] + "' interpreted as tag")
            dictsql = {
                    'interpretation': {
                        'string': part[0],
                        'interpretation': '(inherited) tag',
                        'attribute': 'tag',
                        'operator': 'equals_any',
                        'error': False
                    },
                    'operator': 'or',
                    'val1': {
                        'operator': 'equals_any',
                        'val1': 'tags',
                        'val2': part[0][1:]
                    },
                    'val2': {
                        'operator': 'equals_any',
                        'val1': 'inherited_tags',
                        'val2': part[0][1:]
                    }
            }

        elif part.getName() == 'vrf_rt':
            self._logger.debug("Query part '" + part.vrf_rt + "' interpreted as VRF RT")
            dictsql = {
                    'interpretation': {
                        'attribute': 'VRF RT',
                        'interpretation': 'vrf_rt',
                        'operator': 'equals',
                        'string': part.vrf_rt,
                        'error': False
                        },
                    'operator': 'equals',
                    'val1': 'vrf_rt',
                    'val2': part.vrf_rt
                    }

        elif part.getName() == 'ipv6_address':
            self._logger.debug("Query part '" + part.ipv6_address + "' interpreted as IPv6 address")
            dictsql = {
                'interpretation': {
                    'string': part.ipv6_address,
                    'interpretation': 'IPv6 address',
                    'attribute': 'prefix',
                    'operator': 'contains_equals',
                    'error': False
                },
                'operator': 'contains_equals',
                'val1': 'prefix',
                'val2': part.ipv6_address
            }

        elif part.getName() == 'ipv6_prefix':
            self._logger.debug("Query part '" + part.ipv6_prefix[0] + "' interpreted as IPv6 prefix")

            strict_prefix = unicode(IPy.IP(part.ipv6_prefix[0], make_net=True))
            interp = {
                    'string': part.ipv6_prefix[0],
                    'interpretation': 'IPv6 prefix',
                    'attribute': 'prefix',
                    'operator': 'contained_within_equals',
                    'error': False
                }
            if part.ipv6_prefix[0] != strict_prefix:
                interp['strict_prefix'] = strict_prefix

            dictsql = {
                'interpretation': interp,
                'operator': 'contained_within_equals',
                'val1': 'prefix',
                'val2': strict_prefix
            }

        else:
            # since it's difficult to parse shortened IPv4 addresses (like 10/8)
            # using pyparsing we do a bit of good ol parsing here

            if self._get_afi(part[0]) == 4 and len(part[0].split('/')) == 2:
                self._logger.debug("Query part '" + part[0] + "' interpreted as prefix")
                address, prefix_length = part[0].split('/')

                # complete a prefix to it's fully expanded form
                # 10/8 will be expanded into 10.0.0.0/8 which PostgreSQL can
                # parse correctly
                while len(address.split('.')) < 4:
                    address += '.0'

                prefix = address + '/' + prefix_length
                strict_prefix = unicode(IPy.IP(part[0], make_net=True))

                interp = {
                        'string': part[0],
                        'interpretation': 'IPv4 prefix',
                        'attribute': 'prefix',
                        'operator': 'contained_within_equals',
                        'error': False
                    }

                if prefix != part[0]:
                    interp['expanded'] = prefix

                if prefix != strict_prefix:
                    interp['strict_prefix'] = strict_prefix

                dictsql = {
                    'interpretation': interp,
                    'operator': 'contained_within_equals',
                    'val1': 'prefix',
                    'val2': strict_prefix
                }

            # IPv4 address
            # split on dot to make sure we have all four octets before we do a
            # search
            elif self._get_afi(part[0]) == 4 and len(part[0].split('.')) == 4:
                self._logger.debug("Query part '" + part[0] + "' interpreted as prefix")
                address = unicode(IPy.IP(part[0]))
                dictsql = {
                    'interpretation': {
                        'string': address,
                        'interpretation': 'IPv4 address',
                        'attribute': 'prefix',
                        'operator': 'contains_equals',
                        'error': False
                    },
                    'operator': 'contains_equals',
                    'val1': 'prefix',
                    'val2': address
                }

            else:
                # Description or comment
                self._logger.debug("Query part '" + part[0] + "' interpreted as text")
                dictsql = {
                        'interpretation': {
                            'string': part[0],
                            'interpretation': 'text',
                            'attribute': 'description or comment or node or order_id or customer_id',
                            'operator': 'regex',
                            'error': False
                        },
                        'operator': 'or',
                        'val1': {
                            'operator': 'or',
                            'val1': {
                                'operator': 'or',
                                'val1': {
                                    'operator': 'or',
                                    'val1': {
                                        'operator': 'regex_match',
                                        'val1': 'comment',
                                        'val2': part[0]
                                        },
                                    'val2': {
                                        'operator': 'regex_match',
                                        'val1': 'description',
                                        'val2': part[0]
                                        }
                                    },
                                'val2': {
                                    'operator': 'regex_match',
                                    'val1': 'node',
                                    'val2': part[0]
                                    }
                                },
                            'val2': {
                                'operator': 'regex_match',
                                'val1': 'order_id',
                                'val2': part[0]
                                },
                            },
                        'val2': {
                            'operator': 'regex_match',
                            'val1': 'customer_id',
                            'val2': part[0]
                            }
                        }

        return dictsql