I have a dataset under this form : sentence = {sentence1, sentence2, sentence3,...}

and a list of label : label = {1,2,3,4,5,6,7...} both have same length !

So I want to make an association between my first and second list as sentence1 = label 1

So I tried this code

ruleData = Table[Rule[sentence,label], {i, 1, Length[sentence]}]

so as when I recall ruleData[[7,1]], it should give me : sentence7 (as label7 is 7)

but my code is not working... Any hints ?


closed as off-topic by m_goldberg, happy fish, garej, LCarvalho, LouisB May 21 at 10:19

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – m_goldberg, happy fish, garej, LCarvalho, LouisB
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ Shouldn't you have sentence[[i]] etc in your table? p.s. check AssociationThread[label, sentence] $\endgroup$ – Kuba Apr 8 at 13:23
  • $\begingroup$ Thank you ! It works ! I want now to apply TFIDF to this variable ruleData and hence use this code : TFIDF = FeatureExtraction[ Join[First /@ Keys@ruleData[[All]], Last /@ Keys@ruleData[[All]]], "TFIDF"] but the output is telling me that there is nonatomic expression. As I wanted to apply the tf-idf for each sentence and for the total of sentences ... Any ideas of how to solve it ? $\endgroup$ – Tom Peterson Apr 8 at 13:37
  • $\begingroup$ You're pulling the Keys out of your association and then mapping First over it. Keys are usually atomic expressions (numbers, strings, etc.) and you cannot apply First and/or Last to atoms. It's difficult to understand what you're trying to do from a comment like this. $\endgroup$ – Sjoerd Smit Apr 8 at 13:45
  • $\begingroup$ I want to apply TF-IDF: puting the number of times that a word appears in a sentence (term frequency) in relation to the number of times that that word appears in all other sentences (document frequency) or in other words counting the times a word appears on a given sentence but reducing its importance if it appears on many other sentences $\endgroup$ – Tom Peterson Apr 8 at 13:59
  • $\begingroup$ To make an association, use ruleData = AssociationThread[{sentence1, sentence2, sentence3}, {1, 2, 3}]. You may want to flip those around so can access ruleData[3] to return sentence3. To generate the TF-IDF, you can use GroupBy's to group documents, and words once they are tokenized/normalized. Please ask as a separate question with a minimal dataset. $\endgroup$ – alancalvitti Apr 8 at 14:06

Your code can be fixed by supplying indexes to the variables bound to the lists. Like so:

Contrived data

With[{n = 4},
  sentence = 
    StringJoin[#1, " ", #2] & @@@ Transpose@{RandomWord["Verb", n], RandomWord["Noun", n]};
  label = Range[n]];


ruleData = Table[Rule[sentence[[i]], label[[i]]], {i, 1, Length[sentence]}]


{"beget weevil" -> 1, "closet clanswoman" -> 2, 
 "panel seconder" -> 3,  "cauterize prominence" -> 4}


ruleData[[4, 1]]


"cauterize prominence"

as expected.

But there is a much better way. Mathematica has associations built-in a hash table objects. They are very efficient and make working with key, value associations very easy. Here is how you would set up and use an association for the contrived data.

 assocData = AssociationThread[label -> sentence]

You can retrieve the sentence that has 4 as it key by simply writing


"cauterize prominence"


say your sentence as lst your lable as lbl


lst = {s1, s2, s3, s4, s5, s6, s7};
lbl = {1, 2, 3, 4, 5, 6, 7};

to combine or associate two list, then use

data=Flatten /@ Transpose[{lst, lbl}]

output (* {{s1, 1}, {s2, 2}, {s3, 3}, {s4, 4}, {s5, 5}, {s6, 6}, {s7, 7}}*)

To use Table to get specific list define

Table[data[[i]], {i, 1, Length[lst]}]
output (* {{s1, 1}, {s2, 2}, {s3, 3}, {s4, 4}, {s5, 5}, {s6, 6}, {s7, 7}}*)

To get list from 6 to 7 == Length[lst]==Length[lbl]

Table[data[[i]], {i, 6, Length[lst]}]

output(* {{s6, 6}, {s7, 7}} *)

To get specific list you can use above table or just use


output (* {s3, 3} *)

Not the answer you're looking for? Browse other questions tagged or ask your own question.