{"id":21206233,"url":"https://github.com/rtmigo/gifts_py","last_synced_at":"2026-05-19T17:32:40.538Z","repository":{"id":138532659,"uuid":"445523156","full_name":"rtmigo/gifts_py","owner":"rtmigo","description":"Search for most relevant documents containing words from query. Pure Python implementation without dependencies","archived":false,"fork":false,"pushed_at":"2022-01-09T22:47:38.000Z","size":55,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"staging","last_synced_at":"2025-01-21T15:32:19.366Z","etag":null,"topics":["cosine-similarity","full-text-search","information-retrieval","python","text-mining","tf-idf"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rtmigo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-01-07T13:02:59.000Z","updated_at":"2022-10-14T15:03:34.000Z","dependencies_parsed_at":"2023-07-06T16:00:12.798Z","dependency_job_id":null,"html_url":"https://github.com/rtmigo/gifts_py","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rtmigo%2Fgifts_py","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rtmigo%2Fgifts_py/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rtmigo%2Fgifts_py/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rtmigo%2Fgifts_py/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rtmigo","download_url":"https://codeload.github.com/rtmigo/gifts_py/tar.gz/refs/heads/staging","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243658274,"owners_count":20326467,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cosine-similarity","full-text-search","information-retrieval","python","text-mining","tf-idf"],"created_at":"2024-11-20T20:54:56.603Z","updated_at":"2025-12-29T17:51:31.272Z","avatar_url":"https://github.com/rtmigo.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# [gifts](https://github.com/rtmigo/gifts_py)\n\nSearching for elements that have the common features with the query.\n\n```python3\nquery = ['A', 'B']\n\nelements = [\n    ['N', 'A', 'M'],  # common features: 'A'\n    ['C', 'B', 'A'],  # common features: 'A', 'B'  \n    ['X', 'Y']  # no common features\n]\n```\n\nIn this case, the search with return `['C', 'B', 'A']` and `['N', 'A',\n'M']` in that particular order.\n\n## Use for full-text search\n\nFinding documents that contain words from the query.\n\n```python3\nfrom gifts import SmoothFts\n\nfts = SmoothFts()\n\nfts.add([\"wait\", \"mister\", \"postman\"],\n        doc_id=\"doc1\")\n\nfts.add([\"please\", \"mister\", \"postman\", \"look\", \"and\", \"see\"],\n        doc_id=\"doc2\")\n\nfts.add([\"oh\", \"yes\", \"wait\", \"a\", \"minute\", \"mister\", \"postman\"],\n        doc_id=\"doc3\")\n\n# print IDs of documents in which at least one word of the query occurs, \n# starting with the most relevant matches\nfor doc_id in fts.search(['postman', 'wait']):\n    print(doc_id)\n```\n\n## Use for abstract data mining\n\nIn the examples above, the words were literally words as strings. But they can\nbe any objects suitable as `dict` keys.\n\n```python3\nfrom gifts import SmoothFts\n\nfts = SmoothFts()\n\nfts.add([3, 1, 4, 1, 5, 9, 2], doc_id=\"doc1\")\nfts.add([6, 5, 3, 5], doc_id=\"doc2\")\nfts.add([8, 9, 7, 9, 3, 2], doc_id=\"doc3\")\n\nfor doc_id in fts.search([5, 3, 7]):\n    print(doc_id)\n```\n\n## Implementation details\n\nWhen ranking the results, the algorithm takes into account::\n\n- the number of matching words\n- the rarity of such words in the database\n- the frequency of occurrence of words in the document\n\n### SmoothFts\n\n```python3\nfrom gifts import SmoothFts\n```\n\nIt uses logarithmic [tf-idf](https://en.wikipedia.org/wiki/Tf%E2%80%93idf) for\nweighting the words\nand [cosine similarity](https://en.wikipedia.org/wiki/Cosine_similarity)\nfor scoring the matches.\n\n### SimpleFts\n\n```python3\nfrom gifts import SimpleFts\n```\n\nMinimalistic approach: weigh, multiply, compare. This object is noticeably\nfaster than `SmoothFts`.\n\n## Install\n\n### pip\n\n```bash\npip3 install git+https://github.com/rtmigo/gifts_py#egg=gifts\n```\n\n### setup.py\n\n```python3\ninstall_requires = [\n    \"gifts@ git+https://github.com/rtmigo/gifts_py\"\n]\n```\n\n## See also\n\nThe [skifts](https://github.com/rtmigo/skifts_py#readme) package \ndoes the same search, but uses [scikit-learn](https://scikit-learn.org) and \n[numpy](https://numpy.org/) for better performance. It is literally hundreds \nof times faster.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frtmigo%2Fgifts_py","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frtmigo%2Fgifts_py","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frtmigo%2Fgifts_py/lists"}