-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
1437 lines (1314 loc) · 57.2 KB
/
Copy pathutils.py
File metadata and controls
1437 lines (1314 loc) · 57.2 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import streamlit as st
import base64
from io import BytesIO
import re
import mimetypes
import functools
from typing import Tuple
# Level-specific color scheme
def get_level_color(level_code):
"""
Get the color associated with a specific CEFR level
Parameters:
- level_code: The language level code (A1, A2, etc.)
Returns:
- Hex color code
"""
level_colors = {
"A1": "#4CAF50", # Green for beginner
"A2": "#8BC34A", # Light green for elementary
"B1": "#2196F3", # Blue for intermediate
"B2": "#3F51B5", # Indigo for upper intermediate
"C1": "#9C27B0" # Purple for advanced
}
return level_colors.get(level_code, "#0066FF")
# Function to create HTML level badge
def format_level_badge(level_code):
"""
Create an HTML-formatted level badge
Parameters:
- level_code: The language level code (A1, A2, etc.)
Returns:
- HTML string for the formatted badge
"""
color = get_level_color(level_code)
return f'<span class="level-badge {level_code}" style="background-color: {color};">{level_code}</span>'
# Function to process uploaded files (any type)
def process_uploaded_file(uploaded_file):
"""
Process an uploaded file of any type to prepare it for analysis by the LLM
"""
if uploaded_file is None:
return None
# Read the file
bytes_data = uploaded_file.getvalue()
# Convert to base64 for displaying or sending to API
base64_file = base64.b64encode(bytes_data).decode('utf-8')
# Attempt to detect file type if not provided
file_type = uploaded_file.type
if not file_type:
# Try to guess the MIME type based on filename
guessed_type, _ = mimetypes.guess_type(uploaded_file.name)
if guessed_type:
file_type = guessed_type
else:
# Default to binary if can't determine
file_type = "application/octet-stream"
# Determine if file is text-based for potential direct content extraction
is_text_file = file_type.startswith(('text/', 'application/json', 'application/xml'))
# For text files, try to extract content
text_content = None
if is_text_file:
try:
text_content = bytes_data.decode('utf-8')
except UnicodeDecodeError:
# If UTF-8 fails, try other common encodings
try:
text_content = bytes_data.decode('latin-1')
except:
# If still can't decode, leave as None
pass
# Include the current language level and language in the processed file data
# This ensures both are available when processing the file
current_level = st.session_state.selected_level
level_code = current_level.split()[0] # Extract just the level code (A1, A2, etc.)
# Get current language
language_code = st.session_state.selected_language if hasattr(st.session_state, 'selected_language') else "fin"
return {
"name": uploaded_file.name,
"data": bytes_data,
"base64": base64_file,
"type": file_type,
"is_text_file": is_text_file,
"text_content": text_content,
"language_level": current_level,
"level_code": level_code,
"language_code": language_code
}
# Legacy function for backward compatibility
def process_uploaded_image(uploaded_file):
"""
Legacy function that calls the more generic process_uploaded_file
Kept for backward compatibility
"""
return process_uploaded_file(uploaded_file)
# Function to format grammar tables with proper styling
def format_grammar_table(headers, rows, level_code=None):
"""
Create an HTML table with proper styling for grammar explanations
Parameters:
- headers: List of column headers
- rows: List of rows, where each row is a list of values
- level_code: Optional level code to style the table appropriately
Returns:
- HTML string for the formatted table
"""
# If level code is provided, use its color for the table header
header_color = get_level_color(level_code) if level_code else "#0066FF"
html = f'<table class="grammar-table" style="border-top: 3px solid {header_color};">'
# Add headers
html += f'<thead><tr style="background-color: {header_color};">'
for header in headers:
html += f'<th>{header}</th>'
html += '</tr></thead>'
# Add rows
html += '<tbody>'
for row in rows:
html += '<tr>'
for cell in row:
html += f'<td>{cell}</td>'
html += '</tr>'
html += '</tbody>'
html += '</table>'
return html
# Function to highlight important grammar points
def highlight_text(text, highlight_type="info", level_code=None):
"""
Create a highlighted text box for important information
Parameters:
- text: The text to highlight
- highlight_type: "info", "warning", or "success"
- level_code: Optional level code to style the highlight appropriately
Returns:
- HTML string for the highlighted text
"""
if level_code:
# Use level-specific color if level code is provided
level_color = get_level_color(level_code)
colors = {
"info": (f"rgba({','.join(str(int(c * 255)) for c in level_color.strip('#').lstrip('0')[::2])}, 0.1)", level_color),
"warning": ("#fff3e6", "#ff9500"),
"success": ("#e6fff0", "#00cc66")
}
else:
colors = {
"info": ("#e6f7ff", "#0066FF"), # Light blue background, blue border
"warning": ("#fff3e6", "#ff9500"), # Light orange background, orange border
"success": ("#e6fff0", "#00cc66") # Light green background, green border
}
bg_color, border_color = colors.get(highlight_type, colors["info"])
# Add level badge if level code is provided
level_badge = format_level_badge(level_code) + " " if level_code else ""
html = f"""
<div style="background-color: {bg_color};
border-left: 4px solid {border_color};
padding: 10px;
border-radius: 4px;
margin: 10px 0;">
{level_badge}{text}
</div>
"""
return html
# Function to format vocabulary lists in a visually appealing way
def format_vocabulary_list(vocab_pairs, level, language_name=None):
"""
Create an HTML formatted vocabulary list with level-appropriate styling
Parameters:
- vocab_pairs: List of tuples (target_word, english_translation)
- level: The language level (A1, A2, etc.) or full level string
- language_name: Optional language name to display
Returns:
- HTML string for the formatted vocabulary list
"""
# Extract level code if full level string is provided
if "(" in level:
level_code = level.split()[0]
else:
level_code = level
# Set color based on level
color = get_level_color(level_code)
# Default to 'Vocabulary' if no language name is provided
title = f"{language_name} Vocabulary List" if language_name else "Vocabulary List"
html = f'<div style="background-color: #f8f9fa; padding: 15px; border-radius: 8px; margin: 15px 0; border-top: 3px solid {color};">'
# Add level badge
level_badge = format_level_badge(level_code)
html += f'<h3 style="margin-top: 0; color: {color};">{level_badge} {title}</h3>'
# Add level-specific note
level_notes = {
"A1": "Basic, high-frequency words for beginners",
"A2": "Common everyday vocabulary",
"B1": "More varied vocabulary for familiar topics",
"B2": "Broader vocabulary including some specialized terms",
"C1": "Advanced vocabulary including idiomatic expressions"
}
note = level_notes.get(level_code, "")
if note:
html += f'<p style="font-style: italic; margin-bottom: 15px;">{note}</p>'
html += '<ul style="list-style-type: none; padding: 0;">'
for target_word, english in vocab_pairs:
html += f"""
<li style="padding: 8px 0; border-bottom: 1px solid #e0e0e0;">
<strong>{target_word}</strong> - {english}
</li>
"""
html += '</ul></div>'
return html
# Function to detect language of text using LLM with pattern-based fallback
def detect_language(text: str) -> str:
"""
Detect the language of a text using LLM with regex-based fallback
Parameters:
- text: Text to analyze
Returns:
- Most likely language code (three-letter ISO code)
"""
# If text is very short or empty, use traditional method
if not text or len(text.strip()) < 10:
return detect_language_traditional(text)
# First try using LLM for detection (preferred method)
try:
detected_lang = detect_language_llm(text)
# If detected language is in supported languages, return it
import app
if hasattr(app, 'SUPPORTED_LANGUAGES') and detected_lang in app.SUPPORTED_LANGUAGES:
return detected_lang
except Exception as e:
import logging
logging.warning(f"LLM language detection failed: {str(e)}. Falling back to traditional method.")
# Fallback to traditional method if LLM fails or returns unsupported language
return detect_language_traditional(text)
def detect_language_traditional(text: str) -> str:
"""
Traditional language detection using character patterns
(focused only on the supported languages)
"""
if not text:
return "eng" # Default to English for empty text
text_lower = text.lower()
# Check for Finnish-specific characters
if re.search(r'[äö]', text_lower) and re.search(r'\b(on|ei|ja|minä|sinä|hän|me|te|he)\b', text_lower):
return "fin"
# Check for Swedish-specific characters
if re.search(r'[åäö]', text_lower) and re.search(r'\b(och|att|det|är|jag|du|han|hon|vi)\b', text_lower):
return "swe"
# Check for Spanish-specific characters/patterns
if re.search(r'[áéíóúüñ¿¡]', text_lower) and re.search(r'\b(el|la|los|las|es|y|con|para|por)\b', text_lower):
return "spa"
# Check for French-specific characters/patterns
if re.search(r'[àâçéèêëîïôùûüÿœæ]', text_lower) and re.search(r'\b(le|la|les|un|une|est|et|avec|dans)\b', text_lower):
return "fra"
# Check for German-specific characters
if re.search(r'[äöüß]', text_lower) and re.search(r'\b(der|die|das|und|ist|ich|du|er|sie|wir)\b', text_lower):
return "deu"
# Check for Italian-specific patterns
if re.search(r'[àèéìíîòóùú]', text_lower) and re.search(r'\b(il|lo|la|i|gli|le|e|sono|è|un|una)\b', text_lower):
return "ita"
# Check for Russian Cyrillic
if re.search(r'[а-яА-Я]', text):
return "rus"
# Simpler checks for dominant character sets if the above failed
if re.search(r'[äö]', text_lower):
return "fin" # Could be Finnish
if re.search(r'[åäö]', text_lower):
return "swe" # Could be Swedish
if re.search(r'[áéíóúüñ]', text_lower):
return "spa" # Could be Spanish
if re.search(r'[àâçéèêëîïôùûüÿœæ]', text_lower):
return "fra" # Could be French
if re.search(r'[äöüß]', text_lower):
return "deu" # Could be German
if re.search(r'[àèéìíîòóùú]', text_lower):
return "ita" # Could be Italian
# Default to English if no specific patterns are found
return "eng"
def detect_language_traditional_with_confidence(text: str) -> Tuple[str, float]:
"""
Traditional language detection with confidence score
Returns:
- Tuple of (language_code, confidence_score)
"""
# Calculate pattern matches for each language
lang_patterns = {
"fin": r'[äöå]',
"spa": r'[áéíóúüñ¿¡]',
"fra": r'[àâçéèêëîïôùûüÿœæ]',
"deu": r'[äöüß]',
"rus": r'[а-яА-Я]',
"jpn": r'[\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff]',
"zho": r'[\u4e00-\u9fff]',
"kor": r'[\uac00-\ud7a3]',
"ara": r'[\u0600-\u06ff]'
}
best_match = {"lang": "eng", "confidence": 0.1} # Default to English with low confidence
# Calculate match density for each language
for lang, pattern in lang_patterns.items():
matches = re.findall(pattern, text.lower())
if matches:
# Calculate confidence based on match density
density = len(matches) / len(text)
confidence = min(0.95, density * 10) # Scale and cap at 0.95
# Special case for Chinese/Japanese to differentiate
if lang == "zho" and re.search(r'[\u3040-\u30ff]', text):
continue # Skip this match if Japanese characters are present
if confidence > best_match["confidence"]:
best_match = {"lang": lang, "confidence": confidence}
return best_match["lang"], best_match["confidence"]
# Use caching for LLM-based detection to improve performance
@functools.lru_cache(maxsize=100)
def detect_language_llm(text: str) -> str:
"""
Detect language using LLM with caching
"""
# Import required libraries
from langchain_openai import ChatOpenAI
import streamlit as st
# Get API key from Streamlit secrets
api_key = st.secrets.get("OPENAI_API_KEY", "")
if not api_key:
raise ValueError("OpenAI API key not configured in Streamlit secrets")
# Initialize the LLM
model_name = st.secrets.get("MODEL_NAME", "gpt-4.1-mini-2025-04-14")
chat = ChatOpenAI(
openai_api_key=api_key,
model=model_name,
max_tokens=50 # Small context since we just need the language code
)
# Get supported languages
supported_languages = {
"fin": "Finnish",
"spa": "Spanish",
"fra": "French",
"deu": "German",
"ita": "Italian",
"rus": "Russian",
"swe": "Swedish",
"eng": "English"
}
# Try to update with actual supported languages from app
try:
import app
if hasattr(app, 'SUPPORTED_LANGUAGES'):
supported_languages = {code: info["name"] for code, info in app.SUPPORTED_LANGUAGES.items()}
# Add English if not in the list
supported_languages.setdefault("eng", "English")
except ImportError:
pass
# Create a list of supported language codes and names
language_options = "\n".join([f"- {code}: {name}" for code, name in supported_languages.items()])
# Limit text length for the prompt (using just a sample to save tokens)
sample_text = text[:150].replace("\n", " ")
# Prepare the prompt
prompt = [
{
"role": "system",
"content": f"""You are a language detection system for a language learning application.
Identify the language of the provided text. Focus only on detecting the language.
The application supports these languages:
{language_options}
Respond ONLY with the appropriate three-letter language code from the list.
Your entire response should be just the language code (fin, spa, fra, deu, ita, rus, swe, or eng).
"""
},
{
"role": "user",
"content": f"Detect the language: \"{sample_text}\""
}
]
# Get the response from the LLM
response = chat.invoke(prompt)
# Extract the language code and clean it
language_code = response.content.strip().lower()
# Extract just the language code if the model didn't follow instructions
if ":" in language_code:
language_code = language_code.split(":", 1)[1].strip()
# Further cleaning to get just the 3-letter code
code_match = re.search(r'\b([a-z]{3})\b', language_code)
if code_match:
language_code = code_match.group(1)
else:
# Default to English if no valid code found
language_code = "eng"
# Validate that it's a supported language
if language_code not in supported_languages:
language_code = "eng"
return language_code
# Function to extract exercise-related parameters from user input
def extract_exercise_parameters(text):
"""
Extract exercise type and other parameters from user request using LLM
Parameters:
- text: User's request text
Returns:
- Dictionary with detected parameters
"""
# Initialize with default values
params = {
"exercise_type": None,
"language_direction": None,
"topic": None
}
# If text is too short, return empty params
if not text or len(text.strip()) < 10:
return params
try:
# Try to use LLM for parameter extraction
return extract_exercise_parameters_llm(text)
except Exception as e:
# Fallback to rule-based if LLM fails
import logging
logging.warning(f"LLM parameter extraction failed: {str(e)}. Using rule-based method.")
# Rule-based fallback (original implementation)
# Detect exercise type
if re.search(r'\b(reading|read)\b', text, re.IGNORECASE):
params["exercise_type"] = "reading"
elif re.search(r'\b(writing|write)\b', text, re.IGNORECASE):
params["exercise_type"] = "writing"
elif re.search(r'\b(vocabulary|vocab|words)\b', text, re.IGNORECASE):
params["exercise_type"] = "vocabulary"
elif re.search(r'\b(quiz|test|practice)\b', text, re.IGNORECASE):
params["exercise_type"] = "quiz"
# Detect language direction
from_to_match = re.search(r'\b(from|to)\s+(\w+)\b', text, re.IGNORECASE)
if from_to_match:
direction = from_to_match.group(1).lower()
language = from_to_match.group(2).lower()
if direction == "to" and language == "english":
params["language_direction"] = "target-to-english"
elif direction == "from" and language == "english":
params["language_direction"] = "english-to-target"
# Extract potential topic
topic_match = re.search(r'about\s+([a-zA-Z\s]+)', text, re.IGNORECASE)
if topic_match:
params["topic"] = topic_match.group(1).strip()
return params
@functools.lru_cache(maxsize=100)
def extract_exercise_parameters_llm(text):
"""
Extract exercise parameters using LLM with caching
"""
# Import required libraries
from langchain_openai import ChatOpenAI
import json
import streamlit as st
# Get API key from Streamlit secrets
api_key = st.secrets.get("OPENAI_API_KEY", "")
if not api_key:
raise ValueError("OpenAI API key not configured in Streamlit secrets")
# Initialize the LLM
model_name = st.secrets.get("MODEL_NAME", "gpt-4.1-mini-2025-04-14")
chat = ChatOpenAI(
openai_api_key=api_key,
model=model_name,
max_tokens=200 # Small context for parameter extraction
)
# Get current language if available
current_language = "unknown"
try:
if hasattr(st.session_state, 'selected_language'):
language_code = st.session_state.selected_language
import app
if hasattr(app, 'SUPPORTED_LANGUAGES') and language_code in app.SUPPORTED_LANGUAGES:
current_language = app.SUPPORTED_LANGUAGES[language_code]["name"]
except:
pass
# Prepare the prompt
prompt = [
{
"role": "system",
"content": f"""You are a parameter extraction system for a language learning application.
Extract key parameters from the user's exercise request.
The user is learning {current_language}.
Extract these parameters:
1. exercise_type: The type of exercise requested (reading, writing, vocabulary, quiz, or null if none specified)
2. language_direction: Direction of translation if applicable (target-to-english, english-to-target, or null)
3. topic: The topic or theme of the exercise if specified (e.g., "travel", "food", "work")
Respond with a valid JSON object containing these parameters.
Example: {{"exercise_type": "reading", "language_direction": null, "topic": "holidays"}}
"""
},
{
"role": "user",
"content": f"Extract exercise parameters from this request: \"{text}\""
}
]
# Get the response from the LLM
response = chat.invoke(prompt)
response_content = response.content.strip()
# Try to parse the response as JSON
try:
# Extract JSON object if embedded in text
json_match = re.search(r'\{.*\}', response_content, re.DOTALL)
if json_match:
params = json.loads(json_match.group(0))
else:
# Try parsing the whole response
params = json.loads(response_content)
# Ensure it's a dictionary with the expected keys
if not isinstance(params, dict):
params = {}
# Set default values for missing keys
params.setdefault("exercise_type", None)
params.setdefault("language_direction", None)
params.setdefault("topic", None)
except:
# If parsing fails, use empty parameters
params = {
"exercise_type": None,
"language_direction": None,
"topic": None
}
return params
# Function to get language-specific grammar features
def get_language_grammar_features(lang_code):
"""
Returns specific grammar features for a language
Parameters:
- lang_code: The language code (fin, spa, etc.)
Returns:
- Dictionary with grammar features
"""
grammar_features = {
"fin": {
"cases": ["nominative", "genitive", "partitive", "inessive", "elative", "illative",
"adessive", "ablative", "allative", "essive", "translative", "comitative", "instructive"],
"verb_types": ["Type 1", "Type 2", "Type 3", "Type 4", "Type 5", "Type 6"],
"special_features": ["consonant gradation", "vowel harmony", "partitive objects"]
},
"spa": {
"tenses": ["presente", "pretérito", "imperfecto", "futuro", "condicional", "perfecto", "pluscuamperfecto"],
"moods": ["indicativo", "subjuntivo", "imperativo", "condicional"],
"special_features": ["ser vs estar", "por vs para", "reflexive verbs"]
},
"fra": {
"tenses": ["présent", "passé composé", "imparfait", "futur simple", "conditionnel"],
"moods": ["indicatif", "subjonctif", "impératif", "conditionnel"],
"special_features": ["gender agreement", "partitive articles", "negation"]
},
"deu": {
"cases": ["nominativ", "akkusativ", "dativ", "genitiv"],
"tenses": ["präsens", "präteritum", "perfekt", "futur I", "futur II"],
"special_features": ["word order", "separable verbs", "modal verbs"]
},
"ita": {
"tenses": ["presente", "passato prossimo", "imperfetto", "futuro semplice", "condizionale"],
"moods": ["indicativo", "congiuntivo", "imperativo", "condizionale"],
"special_features": ["gender and number agreement", "articles", "prepositions"]
},
"rus": {
"cases": ["nominative", "genitive", "dative", "accusative", "instrumental", "prepositional"],
"aspects": ["perfective", "imperfective"],
"special_features": ["verbal aspects", "motion verbs", "hard/soft consonants"]
},
"swe": {
"articles": ["definite", "indefinite"],
"tenses": ["presens", "preteritum", "perfekt", "pluskvamperfekt", "futurum"],
"special_features": ["en/ett gender system", "word order", "verb conjugation"]
}
}
# Return language-specific features or empty dict if language not found
return grammar_features.get(lang_code, {})
def get_language_grammar_features(lang_code):
"""
Returns specific grammar features for a language
Parameters:
- lang_code: The language code (fin, spa, etc.)
Returns:
- Dictionary with grammar features
"""
grammar_features = {
"fin": {
"cases": ["nominative", "genitive", "partitive", "inessive", "elative", "illative",
"adessive", "ablative", "allative", "essive", "translative", "comitative", "instructive"],
"verb_types": ["Type 1", "Type 2", "Type 3", "Type 4", "Type 5", "Type 6"],
"special_features": ["consonant gradation", "vowel harmony", "partitive objects"]
},
"spa": {
"tenses": ["presente", "pretérito", "imperfecto", "futuro", "condicional", "perfecto", "pluscuamperfecto"],
"moods": ["indicativo", "subjuntivo", "imperativo", "condicional"],
"special_features": ["ser vs estar", "por vs para", "reflexive verbs"]
},
"fra": {
"tenses": ["présent", "passé composé", "imparfait", "futur simple", "conditionnel"],
"moods": ["indicatif", "subjonctif", "impératif", "conditionnel"],
"special_features": ["gender agreement", "partitive articles", "negation"]
},
"deu": {
"cases": ["nominativ", "akkusativ", "dativ", "genitiv"],
"tenses": ["präsens", "präteritum", "perfekt", "futur I", "futur II"],
"special_features": ["word order", "separable verbs", "modal verbs"]
},
"ita": {
"tenses": ["presente", "passato prossimo", "imperfetto", "futuro semplice", "condizionale"],
"moods": ["indicativo", "congiuntivo", "imperativo", "condizionale"],
"special_features": ["gender and number agreement", "articles", "prepositions"]
},
"rus": {
"cases": ["nominative", "genitive", "dative", "accusative", "instrumental", "prepositional"],
"aspects": ["perfective", "imperfective"],
"special_features": ["verbal aspects", "motion verbs", "hard/soft consonants"]
},
"swe": {
"articles": ["definite", "indefinite"],
"tenses": ["presens", "preteritum", "perfekt", "pluskvamperfekt", "futurum"],
"special_features": ["en/ett gender system", "word order", "verb conjugation"]
}
}
# Return language-specific features or empty dict if language not found
return grammar_features.get(lang_code, {})
# Function to get level-appropriate content for a specific language
def get_level_appropriate_content(level_code, language_code="fin"):
"""
Get vocabulary and grammar structures appropriate for each CEFR level and language
Parameters:
- level_code: The language level code (A1, A2, etc.)
- language_code: The language code (fin, spa, etc.)
Returns:
- Dictionary with level-appropriate content guidelines
"""
# Base content that's relatively language-agnostic
base_content = {
"A1": {
"grammar": [
"Basic present tense",
"Simple questions",
"Basic negation",
"Personal pronouns",
"Numbers 1-100",
"Basic prepositions"
],
"vocabulary": [
"Basic greetings and introductions",
"Family members",
"Numbers and time expressions",
"Food and drinks",
"Basic everyday items",
"Simple adjectives (good, bad, big, small)",
"Basic verbs (to be, to have, to go, to come)"
],
"example_sentences": [
"My name is...",
"I have a...",
"She/he goes to...",
"What are you doing?"
]
},
"A2": {
"grammar": [
"Past tense (simple)",
"More question forms",
"Possessives",
"Plural forms",
"Comparative forms",
"More prepositions"
],
"vocabulary": [
"Weather and seasons",
"Clothing",
"Parts of the body",
"Hobbies and free time",
"Traveling and transportation",
"Shopping and services",
"House and home"
],
"example_sentences": [
"I went to the store yesterday.",
"When did you arrive?",
"My house is bigger than yours.",
"In summer we go to the beach."
]
},
"B1": {
"grammar": [
"Perfect tenses",
"Future tense",
"Conditional forms",
"Passive voice (simple)",
"More complex sentence structures",
"Relative clauses"
],
"vocabulary": [
"Work and professional life",
"Education and studies",
"Media and current events",
"Health and wellbeing",
"Nature and environment",
"Emotions and feelings",
"Abstract concepts"
],
"example_sentences": [
"If I had more time, I would study more.",
"Have you already visited the new museum?",
"This book was written by a famous author.",
"Could you explain this again?"
]
},
"B2": {
"grammar": [
"All tenses",
"Complex verbal constructions",
"Reported speech",
"Advanced conditional forms",
"Expressing hypothesis",
"Complex modifiers"
],
"vocabulary": [
"Political and social issues",
"Science and technology",
"Economics and business",
"Arts and culture",
"Idiomatic expressions",
"Academic vocabulary",
"Specialized terminology"
],
"example_sentences": [
"Experts claim that climate change significantly affects our planet.",
"Without your help, I wouldn't have been able to solve this problem.",
"If only I had studied harder!",
"The matter will be announced later."
]
},
"C1": {
"grammar": [
"All grammatical structures",
"Complex constructions",
"Nuanced tense and mood usage",
"Literary and formal structures",
"Sophisticated syntax",
"Dialectal variations"
],
"vocabulary": [
"Specialized professional terminology",
"Literary and poetic language",
"Colloquial and dialectal expressions",
"Cultural references",
"Humor and wordplay",
"Philosophical concepts",
"Very specific domain knowledge"
],
"example_sentences": [
"Had the government approved the bill, we would have had to change our entire operating model.",
"The questions that emerged in the research will be addressed in more detail in future publications.",
"His/her works reflect the transition period of society in the post-war era.",
"Having said that, I realized I had made a mistake."
]
}
}
# Language-specific content
language_specific = {
"fin": {
"A1": {
"grammar": [
"Basic present tense verb conjugation",
"Simple noun cases: nominative, partitive, genitive",
"Personal pronouns",
"Simple questions with question words",
"Basic negative sentences",
"Numbers 1-100",
"Simple consonant gradation (kk-k, pp-p, tt-t)"
],
"vocabulary": [
"Basic greetings and introductions",
"Family members",
"Numbers and time expressions",
"Food and drinks",
"Basic everyday items",
"Simple adjectives (hyvä, paha, iso, pieni)",
"Basic verbs (olla, olla jollakin, mennä, tulla)"
],
"example_sentences": [
"Minä olen Anna. (I am Anna.)",
"Minulla on koira. (I have a dog.)",
"Hän menee kauppaan. (He/she goes to the store.)",
"Mitä sinä teet? (What are you doing?)"
]
},
"A2": {
"grammar": [
"All verb types in present tense",
"Past tense (imperfect)",
"Consonant gradation (more patterns)",
"Locative cases (inessive, elative, illative)",
"More question forms",
"Possessive suffixes (basic use)",
"Plural forms of nouns"
],
"vocabulary": [
"Weather and seasons",
"Clothing",
"Parts of the body",
"Hobbies and free time",
"Traveling and transportation",
"Shopping and services",
"House and home"
],
"example_sentences": [
"Minä kävin eilen kaupassa. (I went to the store yesterday.)",
"Milloin sinä tulit Suomeen? (When did you come to Finland?)",
"Minun autoni on sininen. (My car is blue.)",
"Kesällä me menemme mökille. (In summer we go to the cottage.)"
]
},
"B1": {
"grammar": [
"Perfect and pluperfect tenses",
"Conditional mood",
"All case forms in singular and plural",
"More complex sentence structures",
"Passive voice in present and past",
"Relative pronouns (joka, mikä)"
],
"vocabulary": [
"Work and employment",
"Education and learning",
"Health and wellbeing",
"Nature and environment",
"Emotions and feelings",
"Technology and media",
"Abstract concepts"
],
"example_sentences": [
"Oletko käynyt Helsingissä aikaisemmin? (Have you been to Helsinki before?)",
"Jos minulla olisi enemmän aikaa, opiskelisin suomea enemmän. (If I had more time, I would study Finnish more.)",
"Kirja, jonka luin viime viikolla, oli todella kiinnostava. (The book that I read last week was really interesting.)",
"Talo on rakennettu 1950-luvulla. (The house was built in the 1950s.)"
]
}
},
"spa": {
"A1": {
"grammar": [
"Present tense of regular -ar, -er, -ir verbs",
"Present tense of common irregular verbs (ser, estar, ir, tener)",
"Gender and number agreement",
"Definite and indefinite articles",
"Basic prepositions",
"Subject pronouns",
"Basic question words"
],
"vocabulary": [
"Greetings and farewells",
"Family and relationships",
"Numbers and time",
"Food and restaurants",
"Daily activities",
"Basic adjectives",
"Countries and nationalities"
],
"example_sentences": [
"Me llamo Juan. (My name is Juan.)",
"¿De dónde eres? (Where are you from?)",
"Tengo dos hermanos. (I have two siblings.)",
"Me gusta el café. (I like coffee.)"
]
},
"A2": {
"grammar": [
"Preterite tense of regular verbs",
"Preterite of common irregular verbs",
"Imperfect tense",
"Reflexive verbs",
"Direct and indirect object pronouns",
"Comparatives and superlatives",
"Simple commands (tú form)"
],
"vocabulary": [
"Shopping and clothing",
"Travel and transportation",
"House and furniture",
"Daily routines",
"Weather and seasons",
"Health and body parts",
"City and directions"
],
"example_sentences": [
"Ayer fui al cine. (Yesterday I went to the movies.)",
"Cuando era niño, jugaba al fútbol. (When I was a child, I used to play soccer.)",
"Me duele la cabeza. (My head hurts.)",
"¿Cómo llego al museo? (How do I get to the museum?)"
]
},
"B1": {