Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """ | |
| Test script for app.py place name cleaning | |
| """ | |
| import re | |
| def clean_place_name(place_name): | |
| if not place_name or not isinstance(place_name, str): | |
| return place_name | |
| if place_name.endswith('.1'): | |
| return place_name[:-2] | |
| return place_name | |
| def clean_place_names_in_result(result): | |
| if not result or not isinstance(result, dict): | |
| return result | |
| # Clean message field | |
| if 'message' in result and result['message']: | |
| message = result['message'] | |
| pattern = r'\b(\w+)\.1\b' | |
| result['message'] = re.sub(pattern, r'\1', message) | |
| # Clean data field | |
| if 'data' in result and isinstance(result['data'], list): | |
| for item in result['data']: | |
| if isinstance(item, dict): | |
| if 'from_place' in item: | |
| item['from_place'] = clean_place_name(item['from_place']) | |
| if 'to_place' in item: | |
| item['to_place'] = clean_place_name(item['to_place']) | |
| # Clean summary field | |
| if 'summary' in result and isinstance(result['summary'], dict): | |
| summary = result['summary'] | |
| if 'from_place' in summary: | |
| summary['from_place'] = clean_place_name(summary['from_place']) | |
| if 'to_place' in summary: | |
| summary['to_place'] = clean_place_name(summary['to_place']) | |
| return result | |
| def test_cleaning(): | |
| # Test the cleaning function | |
| test_result = { | |
| 'success': True, | |
| 'message': 'The fare from Colombo to Kandy.1 is Rs. 745.0', | |
| 'data': [ | |
| {'from_place': 'Colombo', 'to_place': 'Kandy.1', 'fare': 745.0} | |
| ], | |
| 'summary': { | |
| 'from_place': 'Colombo', | |
| 'to_place': 'Kandy.1', | |
| 'fare': 745.0 | |
| } | |
| } | |
| print('Before cleaning:') | |
| print(f'Message: {test_result["message"]}') | |
| print(f'Data: {test_result["data"]}') | |
| print(f'Summary: {test_result["summary"]}') | |
| cleaned_result = clean_place_names_in_result(test_result) | |
| print('\nAfter cleaning:') | |
| print(f'Message: {cleaned_result["message"]}') | |
| print(f'Data: {cleaned_result["data"]}') | |
| print(f'Summary: {cleaned_result["summary"]}') | |
| if __name__ == '__main__': | |
| test_cleaning() | |