This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import spacy | |
| nlp = spacy.load('en_core_web_sm') | |
| # Define the text to be processed | |
| text = "This is a sample sentence! It contains some punctuation marks." | |
| # Create a Doc object | |
| doc = nlp(text) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def generate_text(seed_text, num_words): | |
| for _ in range(num_words): | |
| token_list = tokenizer.texts_to_sequences([seed_text])[0] | |
| token_list = tf.keras.preprocessing.sequence.pad_sequences([token_list], maxlen=max_length) | |
| predicted_index = model.predict(token_list)[0].argmax() | |
| predicted_word = tokenizer.index_word[predicted_index] | |
| seed_text += " " + predicted_word | |
| return seed_text | |
| # Generate a story |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Tokenize text and create word-to-index mapping | |
| tokenizer = tf.keras.preprocessing.text.Tokenizer() | |
| tokenizer.fit_on_texts(data["preprocessed_text"]) | |
| sequences = tokenizer.texts_to_sequences(data["preprocessed_text"]) | |
| padded_sequences = tf.keras.preprocessing.sequence.pad_sequences(sequences, maxlen=max_length) | |
| # Create training data | |
| X_train, X_val, y_train, y_val = train_test_split(padded_sequences, padded_sequences, test_size=0.2, random_state=42) | |
| # Train the model |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import tensorflow as tf | |
| from tensorflow.keras.layers import Embedding, LSTM, Dense, Dropout | |
| from tensorflow.keras.models import Sequential | |
| # Define model architecture | |
| model = Sequential([ | |
| Embedding(input_dim=vocab_size, output_dim=embedding_dim), | |
| LSTM(units=256, return_sequences=True), | |
| Dropout(0.2), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Preprocess text | |
| def preprocess_text(text): | |
| # Remove stop words stop_words = set(stopwords.words("english")) | |
| words = word_tokenize(text.lower()) | |
| filtered_words = [word for word in words if word not in stop_words] | |
| return " ".join(filtered_words) | |
| data["preprocessed_text"] = data["text"].apply(preprocess_text) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Load data (assuming a CSV file) | |
| data = pd.read_csv("stories.csv") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pandas as pd | |
| import nltk | |
| import tensorflow as tf |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import xgboost as xgb | |
| from sklearn.datasets import load_boston | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.metrics import mean_squared_error | |
| # load the Boston housing dataset | |
| boston = load_boston() | |
| X, y = boston.data, boston.target | |
| # split the data into training and testing sets |
NewerOlder