title: Extract words from tree
body: <p>I am working with Recursive Neural Networks and have to use pre-trained GLOVE word embeddings. As my dataset is small I don't want to use all embeddings, only the ones in my dataset. The glove text file looks like:</p>
  <blockquote>
  <p>throughout 0.099415 0.063893 -0.65517 -0.31035 -0.54867 -0.31378 -0.75248 0.11362 0.029626 -0.23832 0.054125 -0.6937 0.019753 -0.25373 0.12972 -0.16954 -0.40668 -0.25018 -0.36012 -0.32704 0.15759 0.041711 0.39897 0.53711 0.058105 -0.8139 -0.39827 0.077777 0.2354 0.27001 3.6635 0.35466 0.81482 -1.1884 -0.29611 0.5244 -0.45087 0.13699 -0.93812 0.4332 -0.45985 0.13347 -0.069847 0.097409 -0.073005 0.55471 -0.13301 -0.47559 -1.057 -0.60987</p>
  <p>established 0.083877 0.14873 -0.80328 -0.24779 -0.10944 -0.11647 -0.87871 -0.43654 0.015808 -0.61117 0.83698 0.39583 -0.1244 -0.36991 -0.43331 0.23538 0.14534 0.2862 0.00098344 0.74532 0.7003 -0.34758 -0.29388 0.17333 -0.51236 -1.2029 -0.35278 -1.1695 -0.61088 0.18471 3.0157 -0.67365 -0.45001 -1.0056 0.034002 -0.026611 0.0097914 0.55304 -0.19343 0.44712 -0.33502 -0.91209 0.19758 -0.035535 -0.77284 0.28167 -0.6852 -0.62592 -0.36489 -0.22197</p>
  <p>mission 1.9144 0.40923 -0.31422 -0.6443 0.55513 -0.81588 -0.766 0.18716 0.94453 -1.0715 0.52092 0.20347 -0.36264 0.5649 -0.30487 -0.13143 0.080574 0.16483 -0.25986 0.45458 -0.40467 0.7947 -0.66128 -0.49532 0.31973 -1.1424 0.097268 -0.48496 -0.17357 0.29176 2.7318 -0.073546 -1.7509 -0.86062 -0.0093403 0.021347 -0.006449 -0.18159 -0.27718 0.10547 -0.30741 -0.48023 0.40922 -1.2402 -0.476 0.30602 0.32634 0.62645 -0.91152 0.42901</p>
  <p>associated 0.23032 1.0919 -0.029497 0.749 -0.17768 0.22918 -0.38591 -0.15044 0.25437 -0.35935 0.42707 0.056076 -0.3842 -0.35789 0.35419 0.45716 -0.15932 -1.0055 0.25288 0.0078635 -0.031715 0.16926 0.90977 0.36871 0.04203 -0.81182 -0.81018 -0.40931 -0.2312 0.50252 2.6289 -0.57009 0.87378 -1.6698 -0.13101 0.28056 0.040286 -0.12814 0.23653 1.2403 0.29779 0.94279 -0.057406 -0.17155 0.96692 -0.055877 -0.48754 0.25528 0.12889 0.10638</p>
  </blockquote>
  <p>The input data (text file) is in the form of tree as follows :</p>
  <blockquote>
  <p>(3 (2 (2 The) (2 Rock)) (4 (3 (2 is) (4 (2 destined) (2 (2 (2 (2 (2 to) (2 (2 be) (2 (2 the) (2 (2 21st) (2 (2 (2 Century) (2 's)) (2 (3 new) (2 (2 ``) (2 Conan)))))))) (2 '')) (2 and)) (3 (2 that) (3 (2 he) (3 (2 's) (3 (2 going) (3 (2 to) (4 (3 (2 make) (3 (3 (2 a) (3 splash)) (2 (2 even) (3 greater)))) (2 (2 than) (2 (2 (2 (2 (1 (2 Arnold) (2 Schwarzenegger)) (2 ,)) (2 (2 Jean-Claud) (2 (2 Van) (2 Damme)))) (2 or)) (2 (2 Steven) (2 Segal))))))))))))) (2 .)))</p>
  <p>(4 (4 (4 (2 The) (4 (3 gorgeously) (3 (2 elaborate) (2 continuation)))) (2 (2 (2 of) (2 ``)) (2 (2 The) (2 (2 (2 Lord) (2 (2 of) (2 (2 the) (2 Rings)))) (2 (2 '') (2 trilogy)))))) (2 (3 (2 (2 is) (2 (2 so) (2 huge))) (2 (2 that) (3 (2 (2 (2 a) (2 column)) (2 (2 of) (2 words))) (2 (2 (2 (2 can) (1 not)) (3 adequately)) (2 (2 describe) (2 (3 (2 (2 co-writer/director) (2 (2 Peter) (3 (2 Jackson) (2 's)))) (3 (2 expanded) (2 vision))) (2 (2 of) (2 (2 (2 J.R.R.) (2 (2 Tolkien) (2 's))) (2 Middle-earth))))))))) (2 .)))</p>
  </blockquote>
  <p>What I want as output is to have a new text file which contains embeddings only for the words in my input text file. That means, extract the words from the input file, match them in glove file and only store those lines of glove file in a new file which match the words.</p>
  <p>I have tried doing this using awk, grep, python but am unable to arrive at a unified solution.
  Any help is appreciated. This is the first time i'm asking any question, please correct me if there is any mistake.</p>
  <p>What I tried :
  If I get list of words from the tree file then I can use</p>
  <pre><code>'grep -F -x -f word_list.txt glove.6B.50d.txt &gt; final.txt'
  </code></pre>
  <p>To get list of words I tried the following with python code:</p>
  <pre><code>import re
  words = re.compile(r'([A-Za-z])')
  file = open('tree.txt','r')
  text = file.read()
  a = [x for x in text if x in set(words)]
  file.close()
  </code></pre>
  <p>But it throws the error</p>
  <pre><code>Traceback (most recent call last):
  File &quot;get_words.py&quot;, line 7, in &lt;module&gt;
  a = [x for x in text if x in set(words)]
  TypeError: '_sre.SRE_Pattern' object is not iterable
  </code></pre>
  <p>Also tried the extraction part using awk by looking online but cannot understand anything.</p>