# Generating an ngram document term matrix with TextAnalysis.jl

**URL:** <https://discourse.julialang.org/t/generating-an-ngram-document-term-matrix-with-textanalysis-jl/55571>\
**Category:** New to Julia\
**Created:** [February 18, 2021, 7:21pm UTC](https://discourse.julialang.org/t/generating-an-ngram-document-term-matrix-with-textanalysis-jl/55571 "2021-02-18T19:21:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![PeterTolochko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petertolochko/32/22108_2.png) [@PeterTolochko](https://discourse.julialang.org/u/PeterTolochko)\
**Post date:** [February 18, 2021, 7:21pm UTC](https://discourse.julialang.org/t/generating-an-ngram-document-term-matrix-with-textanalysis-jl/55571/1 "2021-02-18T19:21:33Z")

</div>

Hello, I’m currently trying to use TextAnalysis.jl for the first time, and I can’t figure out this problem.

I can tokenize a string like so:

```julia
using TextAnalysis
test = "Hello, I like apples and oranges. I like ice-cream."
test_doc = TokenDocument(test)

```

And then, I can access unigrams as well as bigrams from the token object:

```julia
tokens(test_doc)
ngrams(test_doc, 2)

```

But what I can’t figure out is how to create a bigram (or ngram) document term matrix out of it? I know that first I have to convert it into a corpus object

```julia
crps = Corpus([test_doc])
update_lexicon!(crps)
m = DocumentTermMatrix(crps)
dtm(m, :dense)

```

But I can’t figure out from the TextAnalysis documentation, is whether I can choose the ngram to create the dtm? It seems reasonable that I could since the information is already there in the tokenized object.

---

<div class="post-metadata">

**Author:** ![joaomacalos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joaomacalos/32/26249_2.png) [@joaomacalos](https://discourse.julialang.org/u/joaomacalos)\
**Post date:** [June 19, 2021, 6:28am UTC](https://discourse.julialang.org/t/generating-an-ngram-document-term-matrix-with-textanalysis-jl/55571/2 "2021-06-19T06:28:30Z")

</div>

I recently wanted to do the same thing. I solved it by using `NGramDocument()` instead of `TokenDocument()`:

```julia
using TextAnalysis
test = "Hello, I like apples and oranges. I like ice-cream."
test_doc = NGramDocument(test, 2)

crps = Corpus([test_doc])
update_lexicon!(crps)
m = DocumentTermMatrix(crps)
dtm(m, :dense)

```

---

<div class="post-metadata">

**Author:** ![PeterTolochko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petertolochko/32/22108_2.png) [@PeterTolochko](https://discourse.julialang.org/u/PeterTolochko)\
**Post date:** [June 21, 2021, 12:49pm UTC](https://discourse.julialang.org/t/generating-an-ngram-document-term-matrix-with-textanalysis-jl/55571/3 "2021-06-21T12:49:06Z")

</div>

Hi, yep, later I figured it too, but did not reply back to my comment.  
Thanks for pointing out the solution!
