CoolFace
Apppublic

CTRLMYBGM/SimplifyBib

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
app.py97 linesDownload Raw Back to root
1import gradio as gr2import bibtexparser3import re4import os5 6custom_config = bibtexparser.bparser.BibTexParser(common_strings=True)7custom_config.ignore_nonstandard_types = False8 9LOCATION_ABBREVIATIONS = {10    # Countries and regions11    "USA", "UK", "UAE", "EU", "NZ", "SA", "RSA", "USSR", "PRC",12 13    # US states14    "AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA",15    "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD",16    "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ",17    "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC",18    "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY",19 20    # Canadian provinces21    "AB", "BC", "MB", "NB", "NL", "NS", "NT", "NU", "ON", "PE", "QC", "SK", "YT",22 23    # Major cities and their common abbreviations24    "NYC", "LA", "SF", "CHI", "DC", "LDN",  # New York City, Los Angeles, San Francisco, Chicago, D.C., London25 26    # International organizations and bodies27    "UN", "NATO", "ASEAN", "OPEC",28 29    # Continents and major regions30    "NA", "SA", "EU", "AS", "AF", "OC", "AN",  # North America, South America, Europe, Asia, Africa, Oceania, Antarctica31 32    # Some major cities around the world33    "HK", "TPE", "TOK", "SYD",  # Hong Kong, Taipei, Tokyo, Sydney34}35 36 37def is_location_abbreviation(segment: str) -> bool:38    """39    Check if the segment contains a location abbreviation.40    """41    matches = re.findall(r"\{(\w+)\}", segment)42    for match in matches:43        if match in LOCATION_ABBREVIATIONS:44            return True45    return False46 47 48def simplify_booktitle(booktitle: str, year: str = None) -> str:49    """50    Simplifies the booktitle by:51    1. Keeping only the first segment (before the comma).52    2. If any later segment contains '{}', it is retained unless it's a location abbreviation.53    3. If a year is provided, removing that year substring from any segment.54    """55    segments = booktitle.split(',')56 57    # Keeping the first segment58    simplified_title = [segments[0].strip()]59 60    # Checking the remaining segments61    for segment in segments[1:]:62        if '{' in segment and '}' in segment and not is_location_abbreviation(segment):63            simplified_title.append(segment.strip())64 65    # If year is provided, remove it from any segment66    if year:67        simplified_title = [segment.replace(year, '').strip() for segment in simplified_title]68 69    return ', '.join(simplified_title)70 71def simplify_bibtex(bibtex_str: str) -> str:72    bib_database = bibtexparser.loads(bibtex_str, parser=custom_config)73    for entry in bib_database.entries:74        if entry['ENTRYTYPE'] not in ['book', 'inproceedings', 'article', 'misc', 'incollection']:75            continue76        if 'booktitle' in entry:77            year_value = entry.get('year', None)78            entry['booktitle'] = simplify_booktitle(entry['booktitle'], year_value)79        desired_fields = ['author', 'title', 'journal', 'booktitle', 'volume', 'pages', 'year', 'ENTRYTYPE', 'ID']80        keys_to_remove = [key for key in entry if key not in desired_fields]81        for key in keys_to_remove:82            del entry[key]83    return bibtexparser.dumps(bib_database)84 85def gradio_wrapper(bibtex_str: str) -> str:86    result = simplify_bibtex(bibtex_str)87    return result88 89# Define the Gradio interface90interface = gr.Interface(91    fn=gradio_wrapper,92    inputs=gr.inputs.Textbox(lines=20, placeholder="Enter your BibTeX here..."),93    outputs=gr.outputs.Textbox(label="Simplified BibTeX")94)95 96 97interface.launch()