You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
posted question here on stackoverflow without answer yet
Is there a way to access a dfm using the n-1 precedessor types as indexes values?
[abstract pasted from link above] "For next word prediction using ngrams I would need to find all the ngrams (and their frequencies) given n-1 predecessor words.
In dfm I could not see any way to do that, so started implementing it manually on texstat_frequency (data.frame). ...
(Implicitly maybe wrongly excluding using regexes, that I normally love, becauses of prejudice that running them on hundred thousands strings might be too slow/heavy)
I thought this can be done by using asymmetric window values in fcm() but it raises error:
> require(quanteda)
>txt<- c("a b c", "b d e")
> fcm(tokens(txt), "window", window= c(0, 1), tri=FALSE,)
Errorin try(if (window<2) stop("The window size is too small.")) :Thewindowsizeistoosmall.Inaddition:Warningmessage:Inif (window<2) stop("The window size is too small.") :theconditionhaslength>1andonlythefirstelementwillbeusedErrorin qatd_cpp_fcm(x, length(types), count, window, weights, ordered, :Expectingasinglevalue: [extent=2].
By looking at the code, I remembered that fcm() only takes single value for window unlike the window argument of tokens_select(). It makes sense to me to add of such a capability to the function.
Yes that was my first line of attack too. In a test branch I commented out the condition about window needing to be >2, and it produced some results I could not quite explain. It's something we need to dig deeper into.
The other angle of attack would be:
form bigrams with space concatenator
coerce those tokens to character
tokenize them again, coerce to a list
coerce to a data.frame
use some tidy-fu to get a transition matrix of features in rows and the features that followed them in columns
Divide by rowSums() to get a transition matrix
Then 6. could be used as a bigram generator, similar to the one in the NLTK book.
Alternatively, the size of the window could be only an integer, but we could record words before/after in upper/lower triangle when a new argument asymmetric = TRUE.
That’s what ordered = TRUE already does: shows in rows a term and then in columns, the counts of the words that occur following the row word, within the (one-sided/asymmetric) window size.
or at least I think that is what it does. When I ran some tests I found some results I could not explain. That’s why I think this needs to be checked again very thoroughly, and matched to results we would count by hand.
Yes, that's what I found. If this did work, and we removed thewindow >=2 condition, then it could easily serve as a generator for a bigram transition matrix.
We need better substantive tests on this, clearly - should start a new issue to fix this.
@koheiw I installed today 20th August from github, I am still unable to see/find the a way.
Have updated question on SO to include my unsuccessful/wrong way and a simple text to be used if you reply with some sample code to illustrate how to do it
@koheiw
I see exactly that, but that is not what I asked (or meant to ask) except when n-1 is 1.
My question is not "given an ngram find subsequent ngrams and their frequencies (as continuations of the given ngram)"
my question is
"given an (n-1)gram get the next words and their frequencies as successors, (possibly avoiding searches that imply regex matching as I guess that they might be slow, happy to be informed/corrected on that).
So, using the text from the updated SO questionI quoted before:
"a b 1 2 3 a b 2 3 4 a b 3 4 5"
using "a","b" as the given (n-1)gram I would lik a way to get something like:
next word = "1" frequency (as successor "a","b") = 1
next word = "2" frequency (as successor "a","b") = 1
next word = "3" frequency (as successor "a","b") = 1
Activity
koheiw commentedon Aug 13, 2018
I thought this can be done by using asymmetric
windowvalues infcm()but it raises error:By looking at the code, I remembered that
fcm()only takes single value forwindowunlike thewindowargument oftokens_select(). It makes sense to me to add of such a capability to the function.[-]Beginner/basic question, select ngrams given n-1 predecessors[/-][+]Allow asymmetic window sizes in fcm()[/+]kbenoit commentedon Aug 13, 2018
Yes that was my first line of attack too. In a test branch I commented out the condition about window needing to be >2, and it produced some results I could not quite explain. It's something we need to dig deeper into.
The other angle of attack would be:
Then 6. could be used as a bigram generator, similar to the one in the NLTK book.
koheiw commentedon Aug 15, 2018
Alternatively, the size of the window could be only an integer, but we could record words before/after in upper/lower triangle when a new argument
asymmetric = TRUE.kbenoit commentedon Aug 15, 2018
That’s what
ordered = TRUEalready does: shows in rows a term and then in columns, the counts of the words that occur following the row word, within the (one-sided/asymmetric) window size.or at least I think that is what it does. When I ran some tests I found some results I could not explain. That’s why I think this needs to be checked again very thoroughly, and matched to results we would count by hand.
koheiw commentedon Aug 15, 2018
You are right, it is there but produces what I do not expect:
kbenoit commentedon Aug 15, 2018
Yes, that's what I found. If this did work, and we removed the
window >=2condition, then it could easily serve as a generator for a bigram transition matrix.We need better substantive tests on this, clearly - should start a new issue to fix this.
Fix counting when ordered = TRUE to solve #1413
Update NEWS and version for #1413 fix
enrico200165 commentedon Aug 20, 2018
@koheiw I installed today 20th August from github, I am still unable to see/find the a way.
Have updated question on SO to include my unsuccessful/wrong way and a simple text to be used if you reply with some sample code to illustrate how to do it
koheiw commentedon Aug 21, 2018
Don't you see something like this?
enrico200165 commentedon Aug 21, 2018
@koheiw
I see exactly that, but that is not what I asked (or meant to ask) except when n-1 is 1.
My question is not "given an ngram find subsequent ngrams and their frequencies (as continuations of the given ngram)"
my question is
"given an (n-1)gram get the next words and their frequencies as successors, (possibly avoiding searches that imply regex matching as I guess that they might be slow, happy to be informed/corrected on that).
So, using the text from the updated SO questionI quoted before:
"a b 1 2 3 a b 2 3 4 a b 3 4 5"
using "a","b" as the given (n-1)gram I would lik a way to get something like:
next word = "1" frequency (as successor "a","b") = 1
next word = "2" frequency (as successor "a","b") = 1
next word = "3" frequency (as successor "a","b") = 1
The goal is next word prediction.
Link to the SO question where I enclosed that text and code similar to yours.
koheiw commentedon Aug 21, 2018
You should not run this on a large corpus, but it does the job:
enrico200165 commentedon Aug 22, 2018
Thank you, very kind and very useful. I posted a simplified version of your code on SO so other people can also benefit from it