title: How to build a Term-Document-Matrix from a set of texts and a specific set of terms (tags)?
body: <p>I have two sets of data:</p>
  
  <ul>
  <li><p>a set of tags (single words like <code>php</code>, <code>html</code>, etc)</p></li>
  <li><p>a set of texts</p></li>
  </ul>
  
  <p>I wish now to build a Term-Document-Matrix representing the number occurrences of the <code>tags</code>  element in the <code>text</code> element.</p>
  
  <p>I have looked into <a href="http://cran.r-project.org/web/packages/tm/tm.pdf" rel="noreferrer">R library tm</a>, and the <code>TermDocumentMatrix</code> function, but I do not see the possibility to specify the tags as input.</p>
  
  <p>Is there a way to do that?</p>
  
  <p>I am open to any tool (R, Python, other), although using R would be great.</p>
  
  <hr>
  
  <p>Let's set the data as:</p>
  
  <pre><code>TagSet &lt;- data.frame(c("c","java","php","javascript","android"))
  colnames(TagSet)[1] &lt;- "tag"
  
  TextSet &lt;- data.frame(c("How to check if a java file is a javascript script java blah","blah blah php"))
  colnames(TextSet)[1] &lt;- "text"
  </code></pre>
  
  <p>now I'd like to have the TermDocumentMatrix of TextSet according to TagSet.</p>
  
  <p>I tried this:</p>
  
  <pre><code>myCorpus &lt;- Corpus(VectorSource(TextSet$text))
  tdm &lt;- TermDocumentMatrix(myCorpus, control = list(removePunctuation = TRUE, stopwords=TRUE))
  
  
  &gt;inspect(tdm)
  A term-document matrix (7 terms, 2 documents)
  
  Non-/sparse entries: 8/6
  Sparsity           : 43%
  Maximal term length: 10 
  Weighting          : term frequency (tf)
  
              Docs
  Terms        1 2
    blah       1 2
    check      1 0
    file       1 0
    java       2 0
    javascript 1 0
    php        0 1
    script     1 0
  </code></pre>
  
  <p>but that's checking the text against the words of the text, whereas I want to check presence of already defined tags.</p>