Is there a way to find associated words from a term document matrix , other than using findAssoc() in r. My objective is to find all words with decided frequency (lets say if i want to find all wrods with freq more than 200)and then find words which were appearing with these words.
findAssoc() has a terms argument. E.g. feed it with the words, which have a frequency greater than 200. You can find those words by using findFreqTerms().
Related
I have been following this example and was wondering if it is possible to draw the figure 4.4 for combinations of words that were within 10 words of the keyword instead of words that are right next to each other. For example, let's say I wanted to know which words were commonly within 10 words of "sir"?
Sorry, my company has disabled copying/pasting text on your website so I can't post the code.
Don't know about the 10 words difference. But one option may be to calculate co-occurrences on sentence-level, for example with the udpipe::cooccurrence function.
I would like to be able to give the texts in a very large corpus a score based on the number of words they contain from either 'Dictionary 1' (positive score), or 'Dictionary 2' (negative score). Can anybody help me to figure out how to do this?
I have read up on the use of Quanteda for sentiment analysis https://tutorials.quanteda.io/advanced-operations/targeted-dictionary-analysis/ and it seems like that is essentially just scoring texts based on whether they contain words from lists of 'positive' and 'negative' words.
I would like to use Quanteda for this, as I am familiar with the package.
How can I make modified dictionary which doesn't contain two lists of positive and negative words, but two list of other kinds of words e.g. 'right wing' and 'left wing' for example? I already know the words I wish to include in each list.
I am using the tf - IDF to rank terms in a document. When terms are arranged in descending order of the tf - IDF, top 'n' terms are most relevant to that document.
When we choose a document, top 'n' terms of that document has to be displayed.
My question is how to decide the value of 'n'?
For example: for a document terms arranged in descending order of the tf - IDF is as follows:
Document 1
president
Obama
Barak
speech
inauguration
come
the
look
again
took
Now when I want to show topics for document 1, I need only top 5 terms, since all others are not relevant or not topics for the document.
How do I decide this breaking point of terms in a document?
Thanks in advance
In relation to your sample data, there seems to be a problem because 6 to 10 are non-informative stop-words, some of them even stop-words, such as 'the'.
So, a first step that you should try is to remove stop-words.
Coming back to your question, there is no best practice for choosing the value of K in a top-K keyword extraction. This varies from one document to another because some documents are more informative (often multi-topical) than others, which means that these documents should have a higher value of K.
A way to decide on a stopping point is to check the relative differences between the tfidf values between consecutive terms and then stop at a point where this relative difference becomes higher than a threshold, which indicates that there is a big fall in the amount of key informtion that you are outputting.
Assuming that you have computed a tfidf score for each term and have sorted them in descending order of their values, compute the following
before adding every new term
If the above expression is true, where delta is a pre-defined threshold, add the new term... because the new term's informativeness measure is close-enough to the ones already in the list. Otherwise stop if the expression is false, i.e. the difference is higher than delta.
A note: You can play around with different term scoring functions... not just tfidf.
I'd like to use the GloVe word embedding implemented in text2vec to perform supervised regression/classification. I read the helpful tutorial on the text2vec homepage on how to generate the word vectors. However, I'm having trouble grasping how to proceed further, namely apply or transform these word vectors and attach them to each document in such a way that each document is represented by a vector (derived from its component words' vectors I assume), to be used as input in a classifier. I've run into some quick fixes online for short documents, but my documents are rather lengthy (movie subtitles) and there doesn't seem to be any guidance on how to proceed with such documents - or at least guidance matching my comprehension level; I have experience working with n-grams, dictionaries, and topic models, but word embeddings puzzle me.
Thank you!
If your goal is to classify documents - I doubt any doc2vec approach will beat bag-of-words/ngrams. If you still want to try - common simple strategy short documents (< 20 words) is to represent document as weighted sum/average of word vectors.
You can obtain it by something like:
common_terms = intersect(colnames(dtm), rownames(word_vectors) )
dtm_averaged = normalize(dtm[, common_terms], "l1")
# you can re-weight dtm above with tf-idf instead of "l1" norm
sentence_vectors = dtm_averaged %*% word_vectors[common_terms, ]
I'm not aware of any universal established methods to obtain good document vectors for long documents.
I have been working on a project about sentence similarity. I know it has been asked many times in SO, but I just want to know if my problem can be accomplished by the method I use by the way that I am doing it, or I should change my approach to the problem. Roughly speaking, the system is supposed to split all sentences of an article and find similar sentences among other articles that are fed to the system.
I am using cosine similarity with tf-idf weights and that is how I did it.
1- First, I split all the articles into sentences, then I generate trigrams for each sentence and sort them(should I?).
2- I compute the tf-idf weights of trigrams and create vectors for all sentences.
3- I calculate the dot product and magnitude of original sentence and of the sentence to be compared. Then calculate the cosine similarity.
However, the system does not work as I expected. Here, I have some questions in my mind.
As far as I have read about tf-idf weights, I guess they are more useful for finding similar "documents". Since I am working on sentences, I modified the algorithm a little by changing some variables of the formula of tf and idf definitions(instead of document I tried to come up with sentence based definition).
tf = number of occurrences of trigram in sentence / number of all trigrams in sentence
idf = number of all sentences in all articles / number of sentences where trigram appears
Do you think it is ok to use such a definition for this problem?
Another one is that I saw the normalization is mentioned many times when calculating the cosine similarity. I am guessing that this is important because the trigrams vectors might not be the same size(which they rarely are in my case). If a trigram vector is size of x and the other is x+1, then I treat the first vector as it was the size of x+1 with the last value is being 0. Is this what it is meant by normalization? If not, how do I do the normalization?
Besides these, if I have chosen the wrong algorithm what else can be used for such problem(preferably with n-gram approach)?
Thank you in advance.
I am not sure why you are sorting the trigrams for every sentence. All you need to care about when computing cosine similarity is that whether the same trigram occurred in the two sentences or not and with what frequencies. Conceptually speaking you define a fixed and common order among all possible trigrams. Remember the order has to be the same for all sentences. If the number of possible trigrams is N, then for each sentence you obtain a vector of dimensionality N. If a certain trigram does not occur, you set the corresponding value in the vector to zero. You dont really need to store the zeros, but have to take care of them when you define the dot product.
Having said that, trigrams are not a good choice as chances of a match are a lot sparser. For high k you will have better results from bags of k consecutive words, rather than k-grams. Note that the ordering does not matter inside a bag, its a set. You are using k=3 k-grams, but that seems to be on the high side, especially for sentences. Either drop down to bi-grams or use bags of different lengths, starting from 1. Preferably use both.
I am sure you have noticed that sentences that do not use the exact trigram has 0 similarity in your method. K-bag of words
will alleviate the situation somewhat but not solve it completely. Because now you need sentences to share actual words. Two sentences may be similar without using the same words. There are a couple of ways to fix this. Either use LSI(latent Semantic Indexing) or clustering of the words and use the cluster labels to define your cosine similarity.
In order to compute the cosine similarity between vectors x and y you compute the dot product and divide by the norms of x and y.
The 2-norm of the vector x can be computed as square root of the sum of the components squared. However you should also try your algorithm out without any normalization to compare. Usually it works fine, because you are already taking care of the relative sizes of the sentences when you compute the term frequencies (tf).
Hope this helps.