|
1 |
| -# FuzzyMap <img src="https://github.com/pysnippet.png" align="right" height="64" /> |
| 1 | +# Fuzzy Map <img src="https://github.com/pysnippet.png" align="right" height="64" /> |
2 | 2 |
|
3 | 3 | [](https://pypi.org/project/fuzzymap/)
|
4 |
| -[](https://github.com/pysnippet/fuzzymap/blob/master/LICENSE) |
| 4 | +[](https://github.com/pysnippet/fuzzymap/blob/master/LICENSE) |
5 | 5 | [](https://app.fossa.com/projects/git%2Bgithub.com%2Fpysnippet%2Ffuzzymap?ref=badge_shield)
|
| 6 | +[](https://github.com/pysnippet/fuzzymap/actions/workflows/tests.yml) |
6 | 7 |
|
7 |
| -## What is FuzzyMap? |
| 8 | +## What is the Fuzzy Map? |
8 | 9 |
|
9 |
| -`FuzzyMap` is a polymorph Python dictionary. This kind of dictionary returns the value of the exact key if there is such |
10 |
| -a key. Otherwise, it will return the value of the most similar key satisfying the given ratio. The same mechanism works |
11 |
| -when setting a new or replacing an old key in the dictionary. If the key is not found and does not match any of the keys |
12 |
| -by the given ratio, it returns `None`. |
| 10 | +The Fuzzy Map is a polymorph Python dictionary that always returns the value of the closest similar key. This kind of |
| 11 | +dictionary returns the value of the exact key if there is such a key. Otherwise, it will return the value of the most |
| 12 | +similar key satisfying the given ratio. The exact mechanism works when setting a new or replacing an old key in the |
| 13 | +dictionary. If the key is not found and does not match any of the keys by the given ratio, it returns none. |
13 | 14 |
|
14 |
| -## How does it work? |
| 15 | +## Usage with a real-world example |
15 | 16 |
|
16 |
| -Suppose you have scraped data from multiple sources that do not have a unique identifier, and you want to compare the |
17 |
| -values of the items having the same identifiers. Sure there will be found a field that mostly has an equivalent value |
18 |
| -at each source. And you can use that field to identify the corresponding items of other sources' data. |
19 |
| - |
20 |
| -## Let's look at the following example |
21 |
| - |
22 |
| -There is a live data parser that collects the coefficients of football matches from different bookmakers at once, then |
23 |
| -calculates and logs the existing forks. Many bookmakers change the name of the teams to be incomparable with names on |
24 |
| -other sites. |
| 17 | +A live data parser collects the coefficients of sports games from different bookmakers at once, and then an analyzer |
| 18 | +tries to find the existing forks. Different bookmakers use different names for the same games. Some of them use the full |
| 19 | +names, and others use names with a partial abbreviation that makes the analyzer's job harder to find and compare the |
| 20 | +coefficients of the same game. Rather this could be hard without `FuzzyMap` that can find the game using the name used |
| 21 | +in one of the sources. |
25 | 22 |
|
26 | 23 | ```python
|
27 | 24 | from fuzzymap import FuzzyMap
|
28 | 25 |
|
29 |
| -src1 = { |
| 26 | +source_1 = { |
30 | 27 | 'Rapid Wien - First Vienna': {'w1': 1.93, 'x': 2.32, 'w2': 7.44},
|
31 | 28 | 'Al Bourj - Al Nejmeh': {'w1': 26, 'x': 11.5, 'w2': 1.05},
|
32 |
| - # hundreds of other teams' data |
| 29 | + # hundreds of other games' data |
33 | 30 | }
|
34 | 31 |
|
35 |
| -src2 = FuzzyMap({ |
| 32 | +source_2 = FuzzyMap({ |
36 | 33 | 'Bourj FC - Nejmeh SC Beirut': {'w1': 32, 'x': 12, 'w2': 1.05},
|
37 | 34 | 'SK Rapid Wien - First Vienna FC': {'w1': 1.97, 'x': 2.3, 'w2': 8.2},
|
38 |
| - # hundreds of other teams' data |
| 35 | + # hundreds of other games' data |
39 | 36 | })
|
40 | 37 |
|
41 |
| -for team, coefs1 in src1.items(): |
42 |
| - coefs2 = src2[team] |
| 38 | +for game, odds1 in source_1.items(): |
| 39 | + odds2 = source_2[game] |
43 | 40 |
|
44 |
| - # coefs1 = {"w1": 1.93, "x": 2.32, "w2": 7.44} |
45 |
| - # coefs2 = {"w1": 1.97, "x": 2.3, "w2": 8.2} |
46 |
| - handle_fork(coefs1, coefs2) |
| 41 | + # odds1 = {"w1": 1.93, "x": 2.32, "w2": 7.44} |
| 42 | + # odds2 = {"w1": 1.97, "x": 2.3, "w2": 8.2} |
| 43 | + handle_fork(odds1, odds2) |
47 | 44 | ```
|
48 | 45 |
|
49 |
| -With a human brain, it is not difficult to identify that "Rapid Wien - First Vienna" and "SK Rapid Wien - First Vienna |
50 |
| -FC" matches are the same. In the above example, the `src2` is defined as `FuzzyMap`, it makes its keys fuzzy-matchable, |
51 |
| -and we can get an item corresponding to the key of `src1`. See the below graph demonstrating the associations of |
52 |
| -`FuzzyMap` keys. |
| 46 | +In this code example, `source_1` and `source_2` are the dictionary of game and coefficients key-value pairs parsed from |
| 47 | +different sources. And converting the `source_2` dictionary to the `FuzzyMap` dictionary makes it able to find the |
| 48 | +corresponding game using the game's key used in the `source_1` dictionary. |
53 | 49 |
|
54 | 50 | ```mermaid
|
55 | 51 | graph LR
|
56 |
| - src1team1[Rapid Wien - First Vienna]-->src1coefs1["{'w1': 1.93, 'x': 2.32, 'w2': 7.44}"] |
57 |
| - src1team2[Al Bourj - Al Nejmeh]-->src1coefs2["{'w1': 26, 'x': 11.5, 'w2': 1.05}"] |
58 |
| - src2team1[SK Rapid Wien - First Vienna FC]-->src2coefs1["{'w1': 1.97, 'x': 2.3, 'w2': 8.2}"] |
59 |
| - src2team2[Bourj FC - Nejmeh SC Beirut]-->src2coefs2["{'w1': 32, 'x': 12, 'w2': 1.05}"] |
60 |
| - src1team1-->src2coefs1 |
61 |
| - src1team2-->src2coefs2 |
| 52 | + src1team1[Rapid Wien - First Vienna] --> src1coefs1["{'w1': 1.93, 'x': 2.32, 'w2': 7.44}"] |
| 53 | + src1team2[Al Bourj - Al Nejmeh] --> src1coefs2["{'w1': 26, 'x': 11.5, 'w2': 1.05}"] |
| 54 | + src2team1[SK Rapid Wien - First Vienna FC] --> src2coefs1["{'w1': 1.97, 'x': 2.3, 'w2': 8.2}"] |
| 55 | + src2team2[Bourj FC - Nejmeh SC Beirut] --> src2coefs2["{'w1': 32, 'x': 12, 'w2': 1.05}"] |
| 56 | + src1team1 --> src2coefs1 |
| 57 | + src1team2 --> src2coefs2 |
62 | 58 | ```
|
63 | 59 |
|
64 | 60 | ## License
|
|
0 commit comments