The previous notebook (Concept 09) introduced queues. Let's see how we can use them with real world data.
Download a corpus of text.
$ wget http://norvig.com/big.txt
Create a queue of filenames:
In [1]:
import tensorflow as tf
filename_queue = tf.train.string_input_producer(["big.txt"])
Define an op to dequeue a line from file:
In [2]:
reader = tf.TextLineReader()
key_op, value_op = reader.read(filename_queue)
Start all queue runners collected in the graph:
In [3]:
sess = tf.InteractiveSession()
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
Try reading lines from the file by dequeuing:
In [4]:
for i in range(100):
key, value = sess.run([key_op, value_op])
print(key, value)
b'big.txt:1' b'The Project Gutenberg EBook of The Adventures of Sherlock Holmes'
b'big.txt:2' b'by Sir Arthur Conan Doyle'
b'big.txt:3' b'(#15 in our series by Sir Arthur Conan Doyle)'
b'big.txt:4' b''
b'big.txt:5' b'Copyright laws are changing all over the world. Be sure to check the'
b'big.txt:6' b'copyright laws for your country before downloading or redistributing'
b'big.txt:7' b'this or any other Project Gutenberg eBook.'
b'big.txt:8' b''
b'big.txt:9' b'This header should be the first thing seen when viewing this Project'
b'big.txt:10' b'Gutenberg file. Please do not remove it. Do not change or edit the'
b'big.txt:11' b'header without written permission.'
b'big.txt:12' b''
b'big.txt:13' b'Please read the "legal small print," and other information about the'
b'big.txt:14' b'eBook and Project Gutenberg at the bottom of this file. Included is'
b'big.txt:15' b'important information about your specific rights and restrictions in'
b'big.txt:16' b'how the file may be used. You can also find out about how to make a'
b'big.txt:17' b'donation to Project Gutenberg, and how to get involved.'
b'big.txt:18' b''
b'big.txt:19' b''
b'big.txt:20' b'**Welcome To The World of Free Plain Vanilla Electronic Texts**'
b'big.txt:21' b''
b'big.txt:22' b'**eBooks Readable By Both Humans and By Computers, Since 1971**'
b'big.txt:23' b''
b'big.txt:24' b'*****These eBooks Were Prepared By Thousands of Volunteers!*****'
b'big.txt:25' b''
b'big.txt:26' b''
b'big.txt:27' b'Title: The Adventures of Sherlock Holmes'
b'big.txt:28' b''
b'big.txt:29' b'Author: Sir Arthur Conan Doyle'
b'big.txt:30' b''
b'big.txt:31' b'Release Date: March, 1999 [EBook #1661]'
b'big.txt:32' b'[Most recently updated: November 29, 2002]'
b'big.txt:33' b''
b'big.txt:34' b'Edition: 12'
b'big.txt:35' b''
b'big.txt:36' b'Language: English'
b'big.txt:37' b''
b'big.txt:38' b'Character set encoding: ASCII'
b'big.txt:39' b''
b'big.txt:40' b'*** START OF THE PROJECT GUTENBERG EBOOK, THE ADVENTURES OF SHERLOCK HOLMES ***'
b'big.txt:41' b''
b'big.txt:42' b''
b'big.txt:43' b''
b'big.txt:44' b''
b'big.txt:45' b'(Additional editing by Jose Menendez)'
b'big.txt:46' b''
b'big.txt:47' b''
b'big.txt:48' b''
b'big.txt:49' b'THE ADVENTURES OF'
b'big.txt:50' b'SHERLOCK HOLMES'
b'big.txt:51' b''
b'big.txt:52' b'BY'
b'big.txt:53' b''
b'big.txt:54' b'SIR ARTHUR CONAN DOYLE'
b'big.txt:55' b''
b'big.txt:56' b'CONTENTS'
b'big.txt:57' b''
b'big.txt:58' b'I.\tA Scandal in Bohemia'
b'big.txt:59' b'II.\tThe Red-Headed League'
b'big.txt:60' b'III.\tA Case of Identity'
b'big.txt:61' b'IV.\tThe Boscombe Valley Mystery'
b'big.txt:62' b'V.\tThe Five Orange Pips'
b'big.txt:63' b'VI.\tThe Man with the Twisted Lip'
b'big.txt:64' b'VII.\tThe Adventure of the Blue Carbuncle'
b'big.txt:65' b'VIII.\tThe Adventure of the Speckled Band'
b'big.txt:66' b"IX.\tThe Adventure of the Engineer's Thumb"
b'big.txt:67' b'X.\tThe Adventure of the Noble Bachelor'
b'big.txt:68' b'XI.\tThe Adventure of the Beryl Coronet'
b'big.txt:69' b'XII.\tThe Adventure of the Copper Beeches'
b'big.txt:70' b''
b'big.txt:71' b''
b'big.txt:72' b'ADVENTURE I. A SCANDAL IN BOHEMIA'
b'big.txt:73' b''
b'big.txt:74' b'I.'
b'big.txt:75' b''
b'big.txt:76' b''
b'big.txt:77' b"To Sherlock Holmes she is always the woman. I have seldom heard him mention her under any other name. In his eyes she eclipses and predominates the whole of her sex. It was not that he felt any emotion akin to love for Irene Adler. All emotions, and that one particularly, were abhorrent to his cold, precise but admirably balanced mind. He was, I take it, the most perfect reasoning and observing machine that the world has seen, but as a lover he would have placed himself in a false position. He never spoke of the softer passions, save with a gibe and a sneer. They were admirable things for the observer--excellent for drawing the veil from men's motives and actions. But for the trained reasoner to admit such intrusions into his own delicate and finely adjusted temperament was to introduce a distracting factor which might throw a doubt upon all his mental results. Grit in a sensitive instrument, or a crack in one of his own high-power lenses, would not be more disturbing than a strong emotion in a nature such as his. And yet there was but one woman to him, and that woman was the late Irene Adler, of dubious and questionable memory."
b'big.txt:78' b''
b'big.txt:79' b'I had seen little of Holmes lately. My marriage had drifted us away from each other. My own complete happiness, and the home-centred interests which rise up around the man who first finds himself master of his own establishment, were sufficient to absorb all my attention, while Holmes, who loathed every form of society with his whole Bohemian soul, remained in our lodgings in Baker Street, buried among his old books, and alternating from week to week between cocaine and ambition, the drowsiness of the drug, and the fierce energy of his own keen nature. He was still, as ever, deeply attracted by the study of crime, and occupied his immense faculties and extraordinary powers of observation in following out those clues, and clearing up those mysteries which had been abandoned as hopeless by the official police. From time to time I heard some vague account of his doings: of his summons to Odessa in the case of the Trepoff murder, of his clearing up of the singular tragedy of the Atkinson brothers at Trincomalee, and finally of the mission which he had accomplished so delicately and successfully for the reigning family of Holland. Beyond these signs of his activity, however, which I merely shared with all the readers of the daily press, I knew little of my former friend and companion.'
b'big.txt:80' b''
b'big.txt:81' b'One night--it was on the twentieth of March, 1888--I was returning from a journey to a patient (for I had now returned to civil practice), when my way led me through Baker Street. As I passed the well-remembered door, which must always be associated in my mind with my wooing, and with the dark incidents of the Study in Scarlet, I was seized with a keen desire to see Holmes again, and to know how he was employing his extraordinary powers. His rooms were brilliantly lit, and, even as I looked up, I saw his tall, spare figure pass twice in a dark silhouette against the blind. He was pacing the room swiftly, eagerly, with his head sunk upon his chest and his hands clasped behind him. To me, who knew his every mood and habit, his attitude and manner told their own story. He was at work again. He had risen out of his drug-created dreams and was hot upon the scent of some new problem. I rang the bell and was shown up to the chamber which had formerly been in part my own.'
b'big.txt:82' b''
b'big.txt:83' b'His manner was not effusive. It seldom was; but he was glad, I think, to see me. With hardly a word spoken, but with a kindly eye, he waved me to an armchair, threw across his case of cigars, and indicated a spirit case and a gasogene in the corner. Then he stood before the fire and looked me over in his singular introspective fashion.'
b'big.txt:84' b''
b'big.txt:85' b'"Wedlock suits you," he remarked. "I think, Watson, that you have put on seven and a half pounds since I saw you."'
b'big.txt:86' b''
b'big.txt:87' b'"Seven!" I answered.'
b'big.txt:88' b''
b'big.txt:89' b'"Indeed, I should have thought a little more. Just a trifle more, I fancy, Watson. And in practice again, I observe. You did not tell me that you intended to go into harness."'
b'big.txt:90' b''
b'big.txt:91' b'"Then, how do you know?"'
b'big.txt:92' b''
b'big.txt:93' b'"I see it, I deduce it. How do I know that you have been getting yourself very wet lately, and that you have a most clumsy and careless servant girl?"'
b'big.txt:94' b''
b'big.txt:95' b'"My dear Holmes," said I, "this is too much. You would certainly have been burned, had you lived a few centuries ago. It is true that I had a country walk on Thursday and came home in a dreadful mess, but as I have changed my clothes I can\'t imagine how you deduce it. As to Mary Jane, she is incorrigible, and my wife has given her notice, but there, again, I fail to see how you work it out."'
b'big.txt:96' b''
b'big.txt:97' b'He chuckled to himself and rubbed his long, nervous hands together.'
b'big.txt:98' b''
b'big.txt:99' b'"It is simplicity itself," said he; "my eyes tell me that on the inside of your left shoe, just where the firelight strikes it, the leather is scored by six almost parallel cuts. Obviously they have been caused by someone who has very carelessly scraped round the edges of the sole in order to remove crusted mud from it. Hence, you see, my double deduction that you had been out in vile weather, and that you had a particularly malignant boot-slitting specimen of the London slavey. As to your practice, if a gentleman walks into my rooms smelling of iodoform, with a black mark of nitrate of silver upon his right forefinger, and a bulge on the right side of his top-hat to show where he has secreted his stethoscope, I must be dull, indeed, if I do not pronounce him to be an active member of the medical profession."'
b'big.txt:100' b''
Content source: BinRoot/TensorFlow-Book
Similar notebooks: