Skip to content

Instantly share code, notes, and snippets.

@Tdual
Created May 4, 2018 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tdual/39f38d62d67422826a061abd8104b046 to your computer and use it in GitHub Desktop.
Save Tdual/39f38d62d67422826a061abd8104b046 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"from tensorflow.contrib.learn import preprocessing"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[' T h i s i s a p e n . ', ' I a m a c a t . ']\n"
]
}
],
"source": [
"docs = [\"This is a pen.\", \"I am a cat.\"]\n",
"\n",
"docs = [s.replace(\" \", \"\").replace(\"\", \" \") for s in docs]\n",
"print(docs)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"max length: 11\n"
]
}
],
"source": [
"max_length = max([len(x.replace(\" \", \"\")) for x in docs])\n",
"print(\"max length: \", max_length)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"vocab_processor = preprocessing.VocabularyProcessor(max_length)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[array([1, 2, 3, 4, 3, 4, 5, 6, 7, 8, 0]), array([ 9, 5, 10, 5, 11, 5, 12, 0, 0, 0, 0])]\n"
]
}
],
"source": [
"print(list(vocab_processor.fit_transform(docs)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Environment (conda_py3.6)",
"language": "python",
"name": "conda_py3.6"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment