File size: 2,776 Bytes
d489882
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
74
75
76
77
78
79
80
81
import gradio as gr
import re

def sentence_to_odk_fieldname(sentence, answer_type):
    # Dictionary for specific abbreviations
    abbreviations = {
        '%': 'percentage',
        'internally displaced peoples': 'idps',
        'minors under the age of 18': 'minors',
        'minors under 18 years': 'minors',
        'key informant':'ki',
        'households': 'hh',
    }

    # Process text within parentheses
    def include_or_exclude(match):
        text = match.group(0)
        char_count = len(re.sub(r'[^\w\s]', '', text))  # Count characters excluding punctuation
        return text if char_count <= 20 else ''

    # Handle '/' character within words
    sentence = re.sub(r'\b/\b', '_', sentence)

    # Apply the logic for text within parentheses
    sentence = re.sub(r'\([^)]*\)', include_or_exclude, sentence)

    general_words_to_remove = {'do', 'you', 'to', 'your', 'a', 'the', 'is', 'are', 'of', 'in', 'this'}
    measurement_words = {'average', 'total', 'sum', 'count', 'maximum', 'minimum'}

    if answer_type in ['select_one', 'select_multiple']:
        additional_words_to_remove = {'what', 'which', 'who', 'how', 'why', 'did'}
    else:
        additional_words_to_remove = set()

    words_to_remove = general_words_to_remove.union(additional_words_to_remove)
    sentence = sentence.lower()

    for key, value in abbreviations.items():
        sentence = sentence.replace(key, value)

    words = ''.join(char for char in sentence if char.isalnum() or char.isspace()).split()
    filtered_words = [word for word in words if word not in words_to_remove]

    if answer_type in ['decimal', 'integer']:
        field_name_parts = [word for word in filtered_words if word in measurement_words or word not in measurement_words]
    else:
        entity, context = None, []
        for word in filtered_words:
            if not entity:
                entity = word
            else:
                context.append(word)
        field_name_parts = [entity] if entity else []
        if context:
            field_name_parts.append('_'.join(context))

    field_name = '_'.join(field_name_parts)
    field_name = re.sub('_+', '_', field_name)
    field_name = field_name[:128]

    return field_name

def main(sentence, answer_type):
    field_name = sentence_to_odk_fieldname(sentence, answer_type)
    character_count = len(field_name)
    return field_name, character_count

answer_types = ["select_one", "select_multiple", "decimal", "integer", "text"]
iface = gr.Interface(
    fn=main, 
    inputs=[
        gr.Textbox(label="Enter Sentence"), 
        gr.Dropdown(choices=answer_types, label="Answer Type")
    ], 
    outputs=[
        gr.Textbox(label="Field Name"),
        gr.Textbox(label="Character Count")
    ]
)

iface.launch()