File size: 2,329 Bytes
04722ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/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()