Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import re
|
| 3 |
+
|
| 4 |
+
def sentence_to_odk_fieldname(sentence, answer_type):
|
| 5 |
+
# Dictionary for specific abbreviations
|
| 6 |
+
abbreviations = {
|
| 7 |
+
'%': 'percentage',
|
| 8 |
+
'internally displaced peoples': 'idps',
|
| 9 |
+
'minors under the age of 18': 'minors',
|
| 10 |
+
'minors under 18 years': 'minors',
|
| 11 |
+
'key informant':'ki',
|
| 12 |
+
'households': 'hh',
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
# Process text within parentheses
|
| 16 |
+
def include_or_exclude(match):
|
| 17 |
+
text = match.group(0)
|
| 18 |
+
char_count = len(re.sub(r'[^\w\s]', '', text)) # Count characters excluding punctuation
|
| 19 |
+
return text if char_count <= 20 else ''
|
| 20 |
+
|
| 21 |
+
# Handle '/' character within words
|
| 22 |
+
sentence = re.sub(r'\b/\b', '_', sentence)
|
| 23 |
+
|
| 24 |
+
# Apply the logic for text within parentheses
|
| 25 |
+
sentence = re.sub(r'\([^)]*\)', include_or_exclude, sentence)
|
| 26 |
+
|
| 27 |
+
general_words_to_remove = {'do', 'you', 'to', 'your', 'a', 'the', 'is', 'are', 'of', 'in', 'this'}
|
| 28 |
+
measurement_words = {'average', 'total', 'sum', 'count', 'maximum', 'minimum'}
|
| 29 |
+
|
| 30 |
+
if answer_type in ['select_one', 'select_multiple']:
|
| 31 |
+
additional_words_to_remove = {'what', 'which', 'who', 'how', 'why', 'did'}
|
| 32 |
+
else:
|
| 33 |
+
additional_words_to_remove = set()
|
| 34 |
+
|
| 35 |
+
words_to_remove = general_words_to_remove.union(additional_words_to_remove)
|
| 36 |
+
sentence = sentence.lower()
|
| 37 |
+
|
| 38 |
+
for key, value in abbreviations.items():
|
| 39 |
+
sentence = sentence.replace(key, value)
|
| 40 |
+
|
| 41 |
+
words = ''.join(char for char in sentence if char.isalnum() or char.isspace()).split()
|
| 42 |
+
filtered_words = [word for word in words if word not in words_to_remove]
|
| 43 |
+
|
| 44 |
+
if answer_type in ['decimal', 'integer']:
|
| 45 |
+
field_name_parts = [word for word in filtered_words if word in measurement_words or word not in measurement_words]
|
| 46 |
+
else:
|
| 47 |
+
entity, context = None, []
|
| 48 |
+
for word in filtered_words:
|
| 49 |
+
if not entity:
|
| 50 |
+
entity = word
|
| 51 |
+
else:
|
| 52 |
+
context.append(word)
|
| 53 |
+
field_name_parts = [entity] if entity else []
|
| 54 |
+
if context:
|
| 55 |
+
field_name_parts.append('_'.join(context))
|
| 56 |
+
|
| 57 |
+
field_name = '_'.join(field_name_parts)
|
| 58 |
+
field_name = re.sub('_+', '_', field_name)
|
| 59 |
+
field_name = field_name[:128]
|
| 60 |
+
|
| 61 |
+
return field_name
|
| 62 |
+
|
| 63 |
+
def main(sentence, answer_type):
|
| 64 |
+
field_name = sentence_to_odk_fieldname(sentence, answer_type)
|
| 65 |
+
character_count = len(field_name)
|
| 66 |
+
return field_name, character_count
|
| 67 |
+
|
| 68 |
+
answer_types = ["select_one", "select_multiple", "decimal", "integer", "text"]
|
| 69 |
+
iface = gr.Interface(
|
| 70 |
+
fn=main,
|
| 71 |
+
inputs=[
|
| 72 |
+
gr.Textbox(label="Enter Sentence"),
|
| 73 |
+
gr.Dropdown(choices=answer_types, label="Answer Type")
|
| 74 |
+
],
|
| 75 |
+
outputs=[
|
| 76 |
+
gr.Textbox(label="Field Name"),
|
| 77 |
+
gr.Textbox(label="Character Count")
|
| 78 |
+
]
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
iface.launch()
|