Python Forum
sort search results by similarity of characters
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sort search results by similarity of characters
#1
i have a stock market app that im building. basically i have the following code that works as a feature that allows the user to search for official company name and its corresponding symbol in order to view news about the company or view the last few days's stock market value


    search_results=[data["name"] for data in DATA_DICT if search_term in data["name"].lower()]
i would like to sort these results by similarity so for instance if the input was spam and the company names were ham,spam, spammy it would sort the list as spam, spammy, ham
Reply
#2
You can have a look at sort and sorted.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
(Feb-16-2024, 08:48 PM)menator01 Wrote: You can have a look at sort and sorted.

won't that return sorted list alphabetically? i dont need it to be alphabetical but similarity to company name.. maybe i can add points for each matching character in the string and create a list of dictionaries with name and value of matching characters? but the how would i sort them based on those values? maybe some kind of sorcery using the insert method?
Reply
#4
Can you give an example of the data structure?
Pedroski55 likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#5
(Feb-16-2024, 09:04 PM)menator01 Wrote: Can you give an example of the data structure?
i have a DATA_DICT created at the top of my script which has the corresponding names and symbols of all the companies within a json file. i then use search list comprehension like


results=[data["name"] for data in DATA_DICT if search_term in data["name"].lower()]
i then want to sort the resulting list by similarity to company name and using a for loop and messageboxes from tkinter i pop up the search result one at a time and change the company_name Entry box test to the appropriate company name when the user clicks ok. i iterate through the list until i run out of search results
Reply
#6
I think sort is the wrong term. I think you want to filter your results based on similarity.

the easiest way to do this is only include values have a matching substring in name. From your first example
Quote: if the input was spam and the company names were ham,spam, spammy it would sort the list as spam, spammy, ham
Entering spam would match spam and spammy, but not ham. It would also match "I love spam" and "I am not a spammer".

Going from looking for names that contain a substring to similar words is a huge jump in complexity. You should start by looking at the built-in library difflib, in paticular the SequenceMatcher.

If difflib doesn't provide results you like, you might want to look at fuzzywuzzy thad does a different kind of processing.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I scrape profile information from Twitter People search results? asdad 0 737 Nov-29-2022, 10:25 AM
Last Post: asdad
Photo a.sort() == b.sort() all the time 3lnyn0 1 1,328 Apr-19-2022, 06:50 PM
Last Post: Gribouillis
  Search Results Web results Printing the number of days in a given month and year afefDXCTN 1 2,245 Aug-21-2020, 12:20 PM
Last Post: DeaD_EyE
  Remove escape characters / Unicode characters from string DreamingInsanity 5 13,774 May-15-2020, 01:37 PM
Last Post: snippsat
  Sort by the largest number of the same results (frequency) inlovewiththedj 3 2,226 Apr-01-2020, 07:29 AM
Last Post: DPaul
  How to append one function1 results to function2 results SriRajesh 5 3,177 Jan-02-2020, 12:11 PM
Last Post: Killertjuh
  Check fast similarity between gps coordinates dervast 0 1,600 Apr-08-2019, 07:27 PM
Last Post: dervast
  Unexpected Results for Selection Sort emerger 2 2,499 Jul-28-2018, 02:19 PM
Last Post: emerger
  Search and Sort Shapefiles babakkasraie 5 4,359 May-13-2017, 04:18 AM
Last Post: babakkasraie

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020