File size: 4,658 Bytes
3615d9b
 
41020d8
3615d9b
 
 
41020d8
3615d9b
 
 
 
 
7c2eafe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
title: Semantrix
emoji: 🔠
colorFrom: indigo
colorTo: pink
sdk: gradio
sdk_version: 5.1.0
app_file: app.py
pinned: false
license: other
---

# Semantrix Game

This repository contains the implementation of the Semantrix game, a word guessing game using word embeddings. The game supports multiple languages (Spanish and English) and can be configured to use either a Word2Vec model or a SentenceTransformer model for word embeddings.

## Modules



### app.py

This module defines a Gradio-based web application for the Semantrix game. The application allows users to play the game in either Spanish or English, using different embedding models for word similarity.

#### Functions

- `convert_to_markdown_centered(text)`: Converts text to a centered markdown format for displaying game history and last attempt.
    - **Parameters**:
        - `text (str)`: The text to be converted.
    - **Returns**: `str`: The centered markdown formatted text.

- `reset(difficulty, lang, model)`: Resets the game state based on the selected difficulty, language, and model.
    - **Parameters**:
        - `difficulty`: The selected difficulty level.
        - `lang`: The selected language.
        - `model`: The selected embedding model.
    - **Returns**: `list`: A list of initial output components for the UI.

- `change(state, inp)`: Changes the game state by incrementing the state variable.
    - **Parameters**:
        - `state`: The current game state.
        - `inp`: The user input.
    - **Returns**: `list`: A list containing the updated state and input component.

- `update(state, radio, inp, hint)`: Updates the game state and UI components based on the current state and user inputs.
    - **Parameters**:
        - `state`: The current game state.
        - `radio`: The radio input component.
        - `inp`: The user input.
        - `hint`: The hint state.
    - **Returns**: `list`: A list of updated output components for the UI.

### game.py

This module defines the Semantrix class, which implements a word guessing game using word embeddings. The game can be configured to use either a Word2Vec model or a SentenceTransformer model for word embeddings. The game supports multiple languages and difficulty levels.

#### Classes

- `Semantrix`: A class that implements the Semantrix word guessing game.
    - **Methods**:
        - `__init__(self, lang=0, model_type="SentenceTransformer")`: Initializes the Semantrix game with the specified language and model type.
        - `prepare_game(self, difficulty)`: Prepares the game with the selected difficulty level.
        - `gen_rank(self, repeated)`: Generates the ranking file based on the scores.
        - `play_game(self, word)`: Plays the game with the selected word and returns feedback.
        - `curiosity(self)`: Generates a curiosity hint about the secret word once the game is over.

### hints.py

This module provides functions to generate dynamic hints and curiosities about a secret word using language models (LLMs).

#### Functions

- `hint(secret, n, model, last_hint, lang, Config)`: Generates a dynamic hint based on the secret word and the number of hints given.
    - **Parameters**:
        - `secret (str)`: The secret word.
        - `n (int)`: The number of hints already given.
        - `model`: The sentence transformer model used for encoding.
        - `last_hint (int)`: The index of the last hint given.
        - `lang (int)`: The language code (0 for Spanish, 1 for English).
        - `Config`: Configuration object containing hint templates.
    - **Returns**: `tuple`: A tuple containing the generated hint (str), the updated number of hints (int), and the index of the last hint given (int).

- `curiosity(secret, Config)`: Generates a curiosity about the secret word.
    - **Parameters**:
        - `secret (str)`: The secret word.
        - `Config`: Configuration object containing the curiosity template.
    - **Returns**: `str`: The generated curiosity.

- `ireplace(old, new, text)`: Replaces all occurrences of a substring in a string, case-insensitively.
    - **Parameters**:
        - `old (str)`: The substring to be replaced.
        - `new (str)`: The substring to replace with.
        - `text (str)`: The original string.
    - **Returns**: `str`: The modified string with all occurrences of the old substring replaced by the new substring.

    
## How to Run

1. Clone the repository.
2. Install the required dependencies.
3. Run the `app.py` script to launch the Gradio web application.

## Dependencies

- Gradio
- OpenAI
- SentenceTransformers
- Gensim
- NumPy

## License

This project is licensed under the MIT License.