-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
63 lines (53 loc) · 2.49 KB
/
Copy pathmain.py
File metadata and controls
63 lines (53 loc) · 2.49 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
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.preprocessing.text import Tokenizer
import numpy as np
import warnings
warnings.filterwarnings("ignore")
model = load_model("model.h5")
print("==========================================================")
print("==========================================================")
print("==========================================================")
print("==========================================================")
print("==========================================================")
seed_text = str(input("Enter a seed text: "))
print("==========================================================")
print("==========================================================")
print("==========================================================")
next_words = int(input("Enter the number of words you want to generate: "))
print("==========================================================")
print("==========================================================")
print("==========================================================")
print("==========================================================")
print("==========================================================")
tokenizer = Tokenizer()
data = open('linkedin.txt').read()
corpus = data.lower().split("\n")
tokenizer.fit_on_texts(corpus)
total_words = len(tokenizer.word_index) + 1
input_sequences = []
for line in corpus:
token_list = tokenizer.texts_to_sequences([line])[0]
for i in range(1, len(token_list)):
n_gram_sequence = token_list[:i+1]
input_sequences.append(n_gram_sequence)
max_sequence_len = max([len(x) for x in input_sequences])
special_words = ["object", "profession", "subject", "institution", "person"]
for _ in range(next_words):
token_list = tokenizer.texts_to_sequences([seed_text])[0]
token_list = pad_sequences([token_list], maxlen=max_sequence_len-1, padding="pre")
predicted = model.predict(token_list, verbose=0)
predicted = np.argmax(predicted[0], axis=-1)
output_word = ""
for word, index in tokenizer.word_index.items():
if index == predicted:
output_word = word
break
seed_text += " " + output_word
# uppercasing any word in seed_text if the word is in special words
for word in special_words:
if word in seed_text:
seed_text = seed_text.replace(word, word.upper())
print(seed_text)