Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Initial commit
  • Loading branch information
Klaus Thoden committed Oct 9, 2017
0 parents commit 9b9a718
Show file tree
Hide file tree
Showing 3 changed files with 574 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
auto/
doc/
__pycache__/
93 changes: 93 additions & 0 deletions bibtex_search.py
@@ -0,0 +1,93 @@
#!/usr/bin/env python3
# -*- coding: utf-8; mode: python -*-

__version__ = "1.0"
__date__ = "20171006"
__author__ = "kthoden@mpiwg-berlin.mpg.de"

import re
import bibtexparser

bibtexfile = "data/15_maennig.bib"

# find references in XML file
# look up reference in database (Author and Year)

def parse_bibtex(bibfile):
"""Parse the bibtex file, return a dict"""

with open(bibfile) as btf:
btb = bibtexparser.load(btf)
tmp_dict = btb.entries_dict

return tmp_dict
# def parse_bibtex ends here

def search_bibtex(search_string, bibdict):
"""Find the right entry in database
Try different methods. A standard way is to encode author and year
in the citekey. That's a cheap method for a first try.
"""

import difflib

normalized_string = re.sub(r'\s+', '', search_string)

citekeys = bibdict.keys()

for entries in bibdict:
print(entries)

# or use a similarity measure
candidates = difflib.get_close_matches(search_string, citekeys)
print(candidates)

for key in citekeys:
if key == normalized_string:
bibtex_entry = key

return bibtex_entry
# def search_bibtex ends here

def get_local_lyrics_fuzzy(cached_lyrics, song):
"""Look for similar titles."""
import difflib

other_songs, devnull = get_all_files(ROOT_DIR + "/" + song.artist)
candidates = difflib.get_close_matches(song.title + SUFFIX, other_songs)
# old_lyrics_filename = song.title + SUFFIX
count = 1
if candidates:
sys.stdout.write(_("Found \n"))
for i in candidates:
sys.stdout.write(_("%s. %s\n") % (count, i.replace(SUFFIX,"")))
count += 1
answer = input(_("""Does one of those (1 - %s) fit?\nTo abort, just press Enter: """) % (count - 1))
if answer:
alternate_lyrics = "%s/%s/%s" % (ROOT_DIR, song.artist, candidates[int(answer) - 1 ])
logging.info(_("Getting lyrics for %s (%s by %s) from %s\n") %
(song.filename, song.title, song.artist, alternate_lyrics))
file_string = open(alternate_lyrics,"r")
else:
file_string = ""
else:
file_string = ""

return file_string
# def get_local_lyrics_fuzzy ends here

def main():
"""The main bit"""

bibdict = parse_bibtex(bibtexfile)

result = search_bibtex("Daston 2005", bibdict)

print(result)
# def main ends here

if __name__ == '__main__':
main()
# finis

0 comments on commit 9b9a718

Please sign in to comment.