For-loop to determine the mood for the lyrics text files
kibetboniface opened this issue · 21 comments
Hm, have you checked that your folder 'F:/Music/project/lyrics'
contains the lyrics? Also what's the content of lyrics_fullpaths
?
The lyrics folder(F:/Music/project/lyrics) contains the lyrics of all songs in mp3s folder
Looks like it's trying to open the folder F:/Music/project/lyrics
as a file. It should be sth like
F:/Music/project/lyrics/mysong.txt
so there must be some mistake somewhere. What is the content of lyrics_fullpaths
if you do print(lyrics_fullpaths)
?
print(lyrics_fullpaths) gives me this
'F:/Music/project/mp3s\01 - Cake By The Ocean.txt', 'F:/Music/project/mp3s\01 Intro.txt', 'F:/Music/project/mp3s\01 Lit Like Bic.txt', 'F:/Music/project/mp3s\01 Ten Thousand Hours.txt', 'F:/Music/project/mp3s\01-macklemore_and_ryan_lewis-light_tunnels_(feat_mike_slap).txt', 'F:/Music/project/mp3s\01. Always In My Head.txt', 'F:/Music/project/mp3s\02 - Here I Am (Come and Take Me).txt', 'F:/Music/project/mp3s\02 - Pay My Rent.txt', "F:/Music/project/mp3s\02 Can't Hold Us (feat. Ray Dalton).txt", 'F:/Music/project/mp3s\02 January 28th.txt', 'F:/Music/project/mp3s\02 Unlock the Swag (feat. Jace of Two-9).txt', 'F:/Music/project/mp3s\02-macklemore_and_ryan_lewis-downtown_(feat_eric_nally_melle_mel_kool_moe_dee_and_grandmaster_caz).txt', 'F:/Music/project/mp3s\02. Cemeteries Of London.txt', 'F:/Music/project/mp3s\02. Magic.txt', "F:/Music/project/mp3s\03 - Bring Me Your Cup (7'' Version).txt", 'F:/Music/project/mp3s\03 - Toothbrush.txt', 'F:/Music/project/mp3s\03 No Flex Zone.txt', 'F:/Music/project/mp3s\03 One For the Road.txt', 'F:/Music/project/mp3s\03 Thrift Shop (feat. Wanz).txt', 'F:/Music/project/mp3s\03 Wet Dreamz.txt', 'F:/Music/project/mp3s\03-macklemore_and_ryan_lewis-brad_pitts_cousin_(feat_xp).txt', 'F:/Music/project/mp3s\03. Ink.txt', 'F:/Music/project/mp3s\03. Lost!.txt', 'F:/Music/project/mp3s\04 - Jinx.txt', 'F:/Music/project/mp3s\04 - One in Ten.txt', "F:/Music/project/mp3s\04 03' Adolescence.txt", 'F:/Music/project/mp3s\04 Arabella.txt', 'F:/Music/project/mp3s\04 My X.txt', 'F:/Music/project/mp3s\04 Thin Line (feat. Buffalo Madonna).txt', 'F:/Music/project/mp3s\04-macklemore_and_ryan_lewis-buckshot_(feat_krs_one_and_dj_premier).txt', 'F:/Music/project/mp3s\04. 42.txt', 'F:/Music/project/mp3s\04. True Love.txt', 'F:/Music/project/mp3s\05 - Red Red Wine.txt', 'F:/Music/project/mp3s\05 A Tale of 2 Citiez.txt', 'F:/Music/project/mp3s\05 I Want It All.txt', 'F:/Music/project/mp3s\05 Same Love (feat. Mary Lambert).txt', 'F:/Music/project/mp3s\05 This Could Be Us.txt', 'F:/Music/project/mp3s\05-macklemore_and_ryan_lewis-growing_up_(feat_ed_sheeran).txt', 'F:/Music/project/mp3s\05. Midnight.txt', 'F:/Music/project/mp3s\06 - Kingston Town.txt', 'F:/Music/project/mp3s\06 Come Get Her.txt', 'F:/Music/project/mp3s\06 Fire Squad.txt', 'F:/Music/project/mp3s\06 Make the Money.txt', 'F:/Music/project/mp3s\06 No. 1 Party Anthem.txt', 'F:/Music/project/mp3s\06-macklemore_and_ryan_lewis-kevin_(feat_leon_bridges).txt', "F:/Music/project/mp3s\06. Another's Arms.txt", 'F:/Music/project/mp3s\06. Yes.txt', 'F:/Music/project/mp3s\07 - If It Happens Again.txt', 'F:/Music/project/mp3s\07 Mad Sounds.txt', 'F:/Music/project/mp3s\07 Neon Cathedral (feat. Allen Stone.txt', 'F:/Music/project/mp3s\07 St. Tropez.txt', 'F:/Music/project/mp3s\07 Up Like Trump.txt', 'F:/Music/project/mp3s\07-macklemore_and_ryan_lewis-st_ides.txt', 'F:/Music/project/mp3s\07. Oceans.txt', 'F:/Music/project/mp3s\07. Viva La Vida.txt', "F:/Music/project/mp3s\08 - Don't Break My Heart.txt", 'F:/Music/project/mp3s\08 Fireside.txt', 'F:/Music/project/mp3s\08 G.O.M.D.txt', 'F:/Music/project/mp3s\08 Throw Sum Mo (feat. Nicki Minaj & Young Thug).txt']
Okay, I see that there's an 'mp3' in the paths e.g., 'F:/Music/project/mp3s\03-...'
Is that correct, or should it be e.g, 'F:/Music/project/lyrics\03-...'
?
If it should be the latter, you could change
lyrics_fullpaths = [f.replace('.mp3', '.txt') for f in mp3s_fullpaths]
into the following:
lyrics_fullpaths = [f.replace('.mp3', '.txt') for f in mp3s_fullpaths]
lyrics_fullpaths = [f.replace('mp3s', 'lyrics') for f in mp3s_fullpaths]
PS: Also, I am honestly not sure about the backslashes in windows -- i.e., if that's a problem in Python (never used Python on Windows).
After changing to
lyrics_fullpaths = [f.replace('.mp3', '.txt') for f in mp3s_fullpaths]
lyrics_fullpaths = [f.replace('mp3s', 'lyrics') for f in mp3s_fullpaths]
and print(lyrics_fullpaths) i get
'F:/Music/project/lyrics\01 - Cake By The Ocean.mp3', 'F:/Music/project/lyrics\01 Intro.mp3', 'F:/Music/project/lyrics\01 Lit Like Bic.mp3', 'F:/Music/project/lyrics\01 Ten Thousand Hours.mp3', 'F:/Music/project/lyrics\01-macklemore_and_ryan_lewis-light_tunnels_(feat_mike_slap).mp3', 'F:/Music/project/lyrics\01. Always In My Head.mp3', 'F:/Music/project/lyrics\02 - Here I Am (Come and Take Me).mp3', 'F:/Music/project/lyrics\02 - Pay My Rent.mp3', "F:/Music/project/lyrics\02 Can't Hold Us (feat. Ray Dalton).mp3", 'F:/Music/project/lyrics\02 January 28th.mp3', 'F:/Music/project/lyrics\02 Unlock the Swag (feat. Jace of Two-9).mp3', 'F:/Music/project/lyrics\02-macklemore_and_ryan_lewis-downtown_(feat_eric_nally_melle_mel_kool_moe_dee_and_grandmaster_caz).mp3', 'F:/Music/project/lyrics\02. Cemeteries Of London.mp3', 'F:/Music/project/lyrics\02. Magic.mp3', "F:/Music/project/lyrics\03 - Bring Me Your Cup (7'' Version).mp3", 'F:/Music/project/lyrics\03 - Toothbrush.mp3', 'F:/Music/project/lyrics\03 No Flex Zone.mp3', 'F:/Music/project/lyrics\03 One For the Road.mp3', 'F:/Music/project/lyrics\03 Thrift Shop (feat. Wanz).mp3', 'F:/Music/project/lyrics\03 Wet Dreamz.mp3', 'F:/Music/project/lyrics\03-macklemore_and_ryan_lewis-brad_pitts_cousin_(feat_xp).mp3', 'F:/Music/project/lyrics\03. Ink.mp3', 'F:/Music/project/lyrics\03. Lost!.mp3', 'F:/Music/project/lyrics\04 - Jinx.mp3', 'F:/Music/project/lyrics\04 - One in Ten.mp3', "F:/Music/project/lyrics\04 03' Adolescence.mp3", 'F:/Music/project/lyrics\04 Arabella.mp3', 'F:/Music/project/lyrics\04 My X.mp3', 'F:/Music/project/lyrics\04 Thin Line (feat. Buffalo Madonna).mp3', 'F:/Music/project/lyrics\04-macklemore_and_ryan_lewis-buckshot_(feat_krs_one_and_dj_premier).mp3', 'F:/Music/project/lyrics\04. 42.mp3', 'F:/Music/project/lyrics\04. True Love.mp3', 'F:/Music/project/lyrics\05 - Red Red Wine.mp3', 'F:/Music/project/lyrics\05 A Tale of 2 Citiez.mp3',.. , 'F:/Music/project/lyrics\18 Victory Lap.mp3']
and am still getting this error>> with open (lyrics, 'r') as f:
PermissionError: [Errno 13] Permission denied: 'F:/Music/project/lyrics'
Hm, that's weird. Can you maybe post the full script that you are using? I can try this on my machine with some example files to see what I get
import shutil
import os
mp3path='F:/Music/project/mp3s'
happy_mp3path='F:/Music/project/happy_mp3s'
sad_mp3path='F:/Music/project/sad_mp3s'
lyrics='F:/Music/project/lyrics'
mp3s= [f for f in os.listdir(mp3path) if f.endswith('.mp3')]
mp3s_fullpaths=[os.path.join(mp3path, f) for f in mp3s]
happymp3s_fullpaths=[os.path.join(happy_mp3path, f) for f in mp3s]
sadmp3s_fullpaths=[os.path.join(sad_mp3path, f) for f in mp3s]
lyrics_fullpaths = [f.replace('.mp3', '.txt') for f in mp3s_fullpaths]
lyrics_fullpaths = [f.replace('mp3s', 'lyrics') for f in mp3s_fullpaths]
for mp3,happy_mp3, sad_mp3, lyric in zip(mp3s_fullpaths, happymp3s_fullpaths,
sadmp3s_fullpaths, lyrics_fullpaths):
with open (lyrics, 'r') as f:
txt=f.read()
#prediction= None
#processing of txt
#classification of processed txt
if classify(txt) == 'happy':
shutil.move(mp3,happy_mp3)
else:
shutil.move(mp3,sad_mp3)
#print(lyrics_fullpaths)
Okay, I see you had a typo in the for loop, where you used the variable name lyrics
instead of lyric
. The following should work for reading in the lyrics:
import shutil
import os
mp3path='F:/Music/project/mp3s'
happy_mp3path='F:/Music/project/happy_mp3s'
sad_mp3path='F:/Music/project/sad_mp3s'
lyrics='F:/Music/project/lyrics'
mp3s= [f for f in os.listdir(mp3path) if f.endswith('.mp3')]
mp3s_fullpaths=[os.path.join(mp3path, f) for f in mp3s]
happymp3s_fullpaths=[os.path.join(happy_mp3path, f) for f in mp3s]
sadmp3s_fullpaths=[os.path.join(sad_mp3path, f) for f in mp3s]
lyrics_fullpaths = [os.path.join(lyrics,
os.path.basename(f).replace('.mp3', '.txt'))
for f in mp3s_fullpaths]
for mp3,happy_mp3, sad_mp3, lyric in zip(mp3s_fullpaths, happymp3s_fullpaths,
sadmp3s_fullpaths, lyrics_fullpaths):
with open (lyric, 'r') as f:
txt=f.read()
print(txt)
#prediction= None
#processing of txt
#classification of processed txt
#
#if classify(txt) == 'happy':
# shutil.move(mp3,happy_mp3)
#else:
# shutil.move(mp3,sad_mp3)
#print(lyrics_fullpaths)
am still getting this error >> with open (lyric, 'r') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'F:/Music/project/lyrics\01 - Cake By The Ocean.txt'
Not sure since I don't have much experience with Windows, but maybe replacing the forward slashes by backslashes for the filepaths solves the problem?
still the same error
not sure what it is then. Try open the file path (e.g. 'F:\Music\project\lyrics\01 - Cake By The Ocean.txt') from the terminal. If that doesn't work, than there's something about the file path ... maybe the spaces are an issue on win?
something like this
rt= open('F:\Music\project\lyrics\01 - Cake By The Ocean.txt','t')
print(rt)
error>>
ValueError: Must have exactly one of create/read/write/append mode and at most one plus
There should be an 'r' instead of the 't'. With terminal, I meant opening the command line terminal and using a command line text editor to open the file path. On Linux/MacOS simply doing a
cat F:\Music\project\lyrics\01 - Cake By The Ocean.txt
but I am not sure what the windows equivalent of that would be.
Typically, I also avoid whitespaces in all my files and folders, because that can cause some problems for certain tools. So, I don't know how to best deal with that, sry.
the equivalence of cat is type in windows
but i still get errors
type F:\Music\project\lyrics\01 - Cake By The Ocean.txt
Traceback (most recent call last):
File "", line 1, in
File "C:\Users\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
execfile(filename, namespace)
File "C:\Users\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/working.py", line 19, in
with open (lyric, 'r') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'F:/Music/project/lyrics\01 - Cake By The Ocean.txt'
Sorry to hear that. I guess I can not be much of help with that. But maybe search on Google or stackoverflow for something like "how to open a file with whitespaces in Python on Windows" ?
Finally got the solution to the problem. The lyric file has to have the same name with the songs file ...like Arctic Monkeys - Arabella.txt and Arctic Monkeys - Arabella.mp3 not like mine which was 04 Arabella.mp3
Is there a way in which you can solve this problem because the lyric files will not always match with the mp3 files name?
Glad that it works now in general. Finding lyrics files if they don't match the filename exactly sounds tricky. You could try to a fuzzy-string matching approach maybe. I.e., for each song, you would have to compute the fuzzy matching score for each lyric file, then you would the one with the highest similarity score, and then you would check if it's above a certain minimum threshold (say 80% similarity or sth like that). There's a Python library that could be useful for this: https://github.com/seatgeek/fuzzywuzzy