How to make my code insensitive to acronyms

70 views Asked by At

I'm creating a website that shows available carparks in each college campus. How do I make the code insensitive to acronyms, e.g.g if the name of the campus is DCU Alpha?

carparks.html

<h2>
{% if campus %}
{{ campus }}
{% else %}
No such campus
{% endif %}
</h2>
{% if campus %}
{% if carparks %}
<ul>
{% for carpark in carparks %}
<li>{{carpark.name}}: {{carpark.spaces}} spaces, {{carpark.disabled_spaces}} spaces for people with disabilities
  <br>Spaces available:  {{ carpark.spaces_available }}<br><br>
</li>
{% endfor %}
</ul>
{% else %}
<p>No carparks found</p>
{% endif %}
{% endif %}

views.py

def carparks(request):
    context = {}
    Base_URL = 'http://jfoster.pythonanywhere.com/carparks/'
    campus_name = request.GET['campus']
    try:
       campus = Campus.objects.get(name__iexact=campus_name)
    except Campus.DoesNotExist:
       return render(request,"parkatdcu/carparks.html",context)
    carparks = Carpark.objects.filter(campus_id=campus)
    carpark_info = []
    for carpark in carparks:
        URL = Base_URL + carpark.name
        r = requests.get(URL).json()
        if 'spaces_available' in r:
            spaces_available = r['spaces_available']
        else:
            spaces_available = 'not available'
        carpark_info.append({
                         'name': carpark.name,
                         'spaces': carpark.spaces,
                         'disabled_spaces': carpark.disabled_spaces,
                         'spaces_available': spaces_available
                         }
                        )
        context['campus'] = campus_name.title()
        context['carparks'] = carpark_info
        return render(request,"parkatdcu/carparks.html",context)
1

There are 1 answers

6
Daniel On BEST ANSWER

Original Answer: String Manipulation

You can force uppercase strings in python / javascript like so:

python

# becomes DCU ALPHA
'dcu alpha'.upper()
 
# becomes Dcu Alpha
'dcu alpha'.capitalize()

# becomes DCU Alpha: 
'dcu alpha'.split(' ')[0].upper() + ' ' + 'dcu alpha'.split(' ')[1].captialize()

javascript

// becomes DCU ALPHA:
'dcu alpha'.toUpperCase()

// becomes DCU Alpha:
'dcu alpha'.slice(0, 5).toUpperCase() + 'dcu alpha'.slice(5,)

EDIT: Conditional Formatting:

Let's assume we have campus names like so:

campuses = ['dcu alpha', 'northridge', 'lsu univiersity', 'princeton', ...]

We can add some logic to check for campuses's with acrononyms:

views.py

# check if a campus contains two words:
if len(campus_name.split(' ')) > 1: 
    # campus name has at least two words
else: 
    # campus name has only one word

We can also create two lists, one with campus names that have acronyms and one with campus names that do not:

# set two lists:
campuses_with_acronyms = ['dcu alpha', 'lsu univiersity', ...]
campuses_without_acronyms = ['northride', 'princeton', ...]

# check if the campus name is in either list:
if campus_name in campuses_with_acronyms:
    # campus name has an acronym
else:
    # campus name does not have an acronym