0

I am trying to create a script that searches for the file in directory and if the file exists then play it. but I ran into a problem. Suppose when I enter the song name "car" but the file name is "caar, new punjabi song 2022.mp3", then it does not do anything. Is there any way to play similar files. The code is something like this:-

song_name = input("Please enter the song name: ")
for file in os.listdir(music_path):
    if song_name in file:
        print("Here you go!")
        speak("Here you go")
        print("Playing: ", song_name)
        speak("Playing")
        speak(song_name)
        finalfile = os.path.join(music_path, file)
        os.startfile(finalfile)
        break
CodeWithYash
  • 223
  • 1
  • 15
  • You could try fuzzy string matching, e.g., with [fuzzywuzzy](https://pypi.org/project/fuzzywuzzy/) – fsimonjetz Feb 26 '22 at 18:03
  • 2
    Does this answer your question? [Fuzzy String Comparison](https://stackoverflow.com/questions/10383044/fuzzy-string-comparison) – Davide Fiocco Feb 26 '22 at 18:12
  • I tried but the problem is the first string contains only the song name and the second string contains the singer name plus the song name plus other info about song. So, basically the first string is too short them second string.And the ratio is same with all the files. – CodeWithYash Feb 27 '22 at 04:31
  • @CodeWithYash In that case you can use `partial_ratio` (see my answer below). For a more accurate approach, we'd need more details about what you're trying to achieve, what the file names look like, etc. – fsimonjetz Feb 27 '22 at 12:05
  • You can use RegEx – user541396 Feb 27 '22 at 16:37

2 Answers2

0

I've done a little bit of research and I think this may be helpful to you in some way. There is a built in library that can check the similarity between two strings. This is not my code

from difflib import SequenceMatcher

def similar(a, b):
    return SequenceMatcher(None, a, b).ratio()

This creates a function that will return the percentage a string matches another string, Eg.

>>> similar("Apple","Appel")
0.8
>>> similar("Apple","Mango")
0.0

Original comment: https://stackoverflow.com/a/17388505/10462963

Dharman
  • 30,962
  • 25
  • 85
  • 135
Lewis
  • 139
  • 1
  • 7
0

FuzzyWuzzy is worth a try. In particular, partial_ratio would give a rather high value for the example you posted.

>>> from fuzzywuzzy import fuzz
>>> fuzz.partial_ratio("car", "caar, new punjabi song 2022.mp3")
67

To get the most similar file giving a list of files and a search string, you can do something like this:

def get_most_similar_file(query, file_list):
    return max(file_list, key=lambda x:fuzz.partial_ratio(query, x))

files = ["caar, new punjabi song 2022.mp3", "some other song .mp3"]

# returns "caar, new punjabi song 2022.mp3"
get_most_similar_file("car", files)
fsimonjetz
  • 5,644
  • 3
  • 5
  • 21
  • It works almost fine, but i have many files in that folder and it prints the ratio for all the files. Is there any way I can only get the highest ratio. – CodeWithYash Feb 27 '22 at 14:26
  • @CodeWithYash that's a different question, but I added a quick function to get the most similar match. – fsimonjetz Feb 27 '22 at 14:59
  • Thanks it is working fine, but one last think. The function returns the file name but I want it to return the ratio. – CodeWithYash Feb 27 '22 at 15:13