예제 #1
0
    def _find_state(self, state, best_match=True):
        """Fuzzy search correct state.

        :param best_match: bool, when True, only one state will return. 
          otherwise, will return all matching states.
        """
        result = list()

        # check if it is a abbreviate name
        if state.upper() in self.all_state_short:
            result.append(state.upper())
        # if not, find out what is the state that user looking for
        else:
            if best_match:
                choice, confidence = extractOne(
                    state.lower(), self.all_state_long)
                if confidence >= 70:
                    result.append(STATE_ABBR_LONG_TO_SHORT[choice])
            else:
                for choice, confidence in extract(state.lower(), self.all_state_long):
                    if confidence >= 70:
                        result.append(STATE_ABBR_LONG_TO_SHORT[choice])

        if len(result) == 0:
            message = ("'%s' is not a valid state name, use 2 letter "
                       "short name or correct full name please.")
            raise ValueError(message % state)

        return result
예제 #2
0
    def _find_state(self, state, best_match=True):
        """Fuzzy search correct state.

        :param best_match: bool, when True, only one state will return. 
          otherwise, will return all matching states.
        """
        result = list()

        # check if it is a abbreviate name
        if state.upper() in self.all_state_short:
            result.append(state.upper())
        # if not, find out what is the state that user looking for
        else:
            if best_match:
                choice, confidence = extractOne(
                    state.lower(), self.all_state_long)
                if confidence >= 70:
                    result.append(STATE_ABBR_LONG_TO_SHORT[choice])
            else:
                for choice, confidence in extract(state.lower(), self.all_state_long):
                    if confidence >= 70:
                        result.append(STATE_ABBR_LONG_TO_SHORT[choice])

        if len(result) == 0:
            message = ("'%s' is not a valid state name, use 2 letter "
                       "short name or correct full name please.")
            raise ValueError(message % state)

        return result
예제 #3
0
def test_all():
    text = "playboy"
    choice = ["a cow boy", "play boy", "playboy magazine"]
    res = process.extract(text, choice)
    assert res[0][0] == "play boy"
    assert res[1][0] == "playboy magazine"
    assert res[2][0] == "a cow boy"
 def test_all(self):
     text = "playboy"
     choice = ["a cow boy", "play boy", "playboy magazine"]
     res = process.extract(text, choice)
     self.assertEqual(res[0][0], "play boy")
     self.assertEqual(res[1][0], "playboy magazine")
     self.assertEqual(res[2][0], "a cow boy")
예제 #5
0
    def _find_city(self, city, state=None, best_match=True):
        """Fuzzy search correct city.

        :param city: city name.
        :param state: search city in specified state.
        :param best_match: bool, when True, only one city will return. 
          otherwise, will return all matching cities.

        **中文文档**

        如果给定了state, 则只在state里的城市中寻找, 否则, 在全国所有的城市中
        寻找。 
        """
        # find out what is the city that user looking for
        if state:
            state = self._find_state(state, best_match=True)[0]
            select_sql = "SELECT DISTINCT City FROM zipcode WHERE State == '%s'" % state
        else:
            select_sql = "SELECT DISTINCT City FROM zipcode"

        all_city = [row[0] for row in self.cursor.execute(select_sql)]
        if len(all_city) == 0:
            raise ValueError("No city is available in state('%s')" % state)

        result = list()

        if best_match:
            choice, confidence = extractOne(city.lower(), all_city)
            if confidence >= 70:
                result.append(choice)
        else:
            for choice, confidence in extract(city.lower(), all_city):
                if confidence >= 70:
                    result.append(choice)

        if len(result) == 0:
            raise ValueError("'%s' is not a valid city name" % city)

        return result
예제 #6
0
    def _find_city(self, city, state=None, best_match=True):
        """Fuzzy search correct city.

        :param city: city name.
        :param state: search city in specified state.
        :param best_match: bool, when True, only one city will return. 
          otherwise, will return all matching cities.

        **中文文档**

        如果给定了state, 则只在state里的城市中寻找, 否则, 在全国所有的城市中
        寻找。 
        """
        # find out what is the city that user looking for
        if state:
            state = self._find_state(state, best_match=True)[0]
            select_sql = "SELECT DISTINCT City FROM zipcode WHERE State == '%s'" % state
        else:
            select_sql = "SELECT DISTINCT City FROM zipcode"

        all_city = [row[0] for row in self.cursor.execute(select_sql)]
        if len(all_city) == 0:
            raise ValueError("No city is available in state('%s')" % state)

        result = list()

        if best_match:
            choice, confidence = extractOne(city.lower(), all_city)
            if confidence >= 70:
                result.append(choice)
        else:
            for choice, confidence in extract(city.lower(), all_city):
                if confidence >= 70:
                    result.append(choice)

        if len(result) == 0:
            raise ValueError("'%s' is not a valid city name" % city)

        return result