1
$\begingroup$

I am trying to analyze some text data for my thesis! How can I plot this data in Mathematica in a barplot based on the frequency of the words and combinations of them?

On the Abscissa (x) I would have "source", "target", and the word sequence "source and target", and on the Ordinate (y) the frequencies.

{"source", "source", "and", "target", "source", "and", "target", \
"target", "source", "and", "target", "target", "source", "and", \
"target", "target", "target", "target", "target", "source", "and", \
"target", "target", "source", "and", "target", "target", "target", \
"target", "target", "source", "and", "target", "source", "and", \
"target", "target", "target", "source", "and", "target", "target", \
"target", "target", "target", "target", "target", "target", "target", \
"target", "source", "and", "target", "target", "target", "target", \
"target", "target", "source", "and", "target", "source", "and", \
"target", "source", "and", "target", "target", "target", "source", \
"and", "target", "target", "target", "target", "target", "source", \
"and", "target", "target", "source", "and", "target", "source", \
"source", "and", "target", "target", "target", "source", "and", \
"target", "source", "and", "target", "source", "and", "target", \
"target", "source", "and", "target", "target", "target", "target", \
"source", "and", "target", "source", "and", "target", "target", \
"source", "and", "target", "source", "and", "target", "target", \
"target", "source", "and", "target", "target", "source", "target", \
"target", "source", "and", "target", "source", "and", "target", \
"source", "and", "target", "source", "and", "target", "source", \
"and", "target", "source", "target", "target", "source", "and", \
"target", "target", "target", "target", "target", "source", "and", \
"target", "target", "target", "source", "source", "and", "target", \
"target", "target", "target", "source", "and", "target", "target", \
"target", "target", "target", "target", "target", "target", "target", \
"target", "source", "and", "target", "source", "and", "target", \
"target", "source", "and", "target", "source", "and", "target"}

Those are the data I want to plot! Thanks a lot in advance!

$\endgroup$
4
  • 2
    $\begingroup$ Tally the rest is up to you $\endgroup$
    – Sektor
    Commented Oct 3, 2015 at 13:04
  • $\begingroup$ @Sektor I think it is asked to find the tallies for "source", "target", and {"source","and","target"}. Regardless if that is the case or not, the question is not phrased clearly. $\endgroup$ Commented Oct 3, 2015 at 13:28
  • $\begingroup$ Greetings @Ati! Make the most of Mma.SE and take the tour now. Help us to help you, write an excellent question. Edit if improvable, show due diligence, give brief context, include minimum working examples of code and data in formatted form. As you receive give back, vote and answer questions, keep the site useful, be kind, correct mistakes and share what you have learned. $\endgroup$
    – rhermans
    Commented Oct 3, 2015 at 13:29
  • $\begingroup$ Ati, you should clarify what you need. Is it what Anton Antonov says? $\endgroup$
    – rhermans
    Commented Oct 3, 2015 at 13:39

2 Answers 2

5
$\begingroup$

I assume it is asked to find the tallies for (1) "source", (2) "target", and (3) the sequence of words "source", "and", "target".

If that is the case this code does it.

words = {"source", "source", "and", "target", "source", "and", 
   "target", "target", "source", "and", "target", "target", "source", 
   "and", "target", "target", "target", "target", "target", "source", 
   "and", "target", "target", "source", "and", "target", "target", 
   "target", "target", "target", "source", "and", "target", "source", 
   "and", "target", "target", "target", "source", "and", "target", 
   "target", "target", "target", "target", "target", "target", 
   "target", "target",  "target", "source", "and", "target", "target",
    "target", "target", "target", "target", "source", "and", "target",
    "source", "and",  "target", "source", "and", "target", "target", 
   "target", "source", "and", "target", "target", "target", "target", 
   "target", "source", "and", "target", "target", "source", "and", 
   "target", "source", "source", "and", "target", "target", "target", 
   "source", "and", "target", "source", "and", "target", "source", 
   "and", "target", "target", "source", "and", "target", "target", 
   "target", "target", "source", "and", "target", "source", "and", 
   "target", "target", "source", "and", "target", "source", "and", 
   "target", "target", "target", "source", "and", "target", "target", 
   "source", "target", "target", "source", "and", "target", "source", 
   "and", "target", "source", "and", "target", "source", "and", 
   "target", "source", "and", "target", "source", "target", "target", 
   "source", "and", "target", "target", "target", "target", "target", 
   "source", "and", "target", "target", "target", "source", "source", 
   "and", "target", "target", "target", "target", "source", "and", 
   "target", "target", "target", "target", "target", "target", 
   "target", "target", "target", "target", "source", "and", "target", 
   "source", "and", "target", "target", "source", "and", "target", 
   "source", "and", "target"};

newWords = 
 words //. {start___, "source", "and", "target", end___} :> {start, 
    "source and target", end}

BarChart[Counts[newWords], ChartLabels -> Automatic]

enter image description here

Update Since my answer was accepted I updated it with the commands in the answer by Szabolcs.

$\endgroup$
2
  • $\begingroup$ Yes, awesome! This is exactly what I was asking! Sorry about the confused question!! Thank youuuuuu!!!!!!!! $\endgroup$
    – pollon
    Commented Oct 3, 2015 at 13:42
  • $\begingroup$ No problem. Good luck with the thesis! $\endgroup$ Commented Oct 3, 2015 at 14:12
2
$\begingroup$
BarChart[#2, ChartLabels -> #1] & @@ Transpose[Tally[data]]

Mathematica graphics

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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