one
#1
by
ProCreations
- opened
tg.py
CHANGED
@@ -1,36 +1,30 @@
|
|
1 |
-
import
|
2 |
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
Machine learning is the study of algorithms that can learn from data.
|
7 |
-
Natural language processing is a subfield of artificial intelligence concerned with the interactions between computers and human language.
|
8 |
-
Deep learning is a class of machine learning algorithms that use multiple layers of artificial neural networks to learn from data.
|
9 |
-
"""
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
#
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
# Generate text starting with "The"
|
31 |
-
prompt_vector = tf.one_hot(tf.constant([tokens.index("The")]), vocab_size)
|
32 |
-
for i in range(10):
|
33 |
-
prediction = model.predict(tf.expand_dims(prompt_vector, axis=0))
|
34 |
-
predicted_index = tf.argmax(prediction, axis=1).numpy()[0]
|
35 |
-
prompt_vector = tf.concat([prompt_vector, tf.one_hot([predicted_index], vocab_size)], axis=0)
|
36 |
-
print(tokens[predicted_index], end=" ")
|
|
|
1 |
+
import random
|
2 |
|
3 |
+
def generate_text(corpus, start_word, max_length=100):
|
4 |
+
"""
|
5 |
+
Generates text using a bigram language model.
|
|
|
|
|
|
|
|
|
6 |
|
7 |
+
Args:
|
8 |
+
corpus: A list of words from the training text.
|
9 |
+
start_word: The word to start the generation.
|
10 |
+
max_length: The maximum length of the generated text.
|
11 |
|
12 |
+
Returns:
|
13 |
+
A string of generated text.
|
14 |
+
"""
|
15 |
+
text = start_word
|
16 |
+
prev_word = start_word
|
17 |
+
for _ in range(max_length):
|
18 |
+
# Get all words that follow the previous word in the corpus
|
19 |
+
next_word_candidates = [word for word in corpus if word[0] == prev_word[-1]]
|
20 |
+
# Randomly choose the next word based on their frequency
|
21 |
+
next_word = random.choices(next_word_candidates, weights=[corpus.count(w) for w in next_word_candidates])[0]
|
22 |
+
text += " " + next_word
|
23 |
+
prev_word = next_word
|
24 |
+
return text
|
25 |
|
26 |
+
# Example usage
|
27 |
+
corpus = ["hello", "world", "how", "are", "you", "today", "feeling", "great", "is", "a", "beautiful", "day"]
|
28 |
+
start_word = "hello"
|
29 |
+
generated_text = generate_text(corpus, start_word)
|
30 |
+
print(generated_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|