Facebook
From Em, 1 Year ago, written in Java.
Embed
Download Paste or View Raw
Hits: 73
  1.  
  2. /*Counts of headline results for Myspace, Facebook, and Twitter respectively
  3. starting from July 2005, when Myspace was first mentioned, until Dec 2021
  4. when my dataset ends*/
  5. int[][] raw_data = {{0, 0, 0},{0, 0, 0},{0, 0, 0},{0, 0, 0},{0, 0, 0},{0, 0, 0},
  6.               {1, 0, 0},{0, 0, 0},{0, 0, 0},{0, 0, 0},{0, 0, 0},{0, 0, 0},
  7.               {0, 0, 0},{0, 0, 0},{0, 0, 0},{0, 0, 0},{0, 0, 0},{0, 0, 0},
  8.               {0, 0, 0},{1, 0, 0},{0, 0, 0},{0, 0, 0},{0, 0, 0},{1, 0, 0},
  9.               {1, 0, 0},{0, 0, 0},{0, 0, 0},{3, 0, 0},{1, 0, 0},{1, 1, 0},
  10.               {3, 2, 0},{1, 2, 0},{0, 5, 0},{2, 3, 0},{1, 1, 0},{0, 3, 0},
  11.               {1, 1, 0},{1, 2, 0},{0, 1, 0},{1, 0, 0},{0, 2, 0},{0, 2, 0},
  12.               {1, 3, 0},{2, 4, 0},{0, 1, 0},{0, 2, 0},{1, 0, 0},{0, 5, 1},
  13.               {0, 1, 1},{1, 7, 0},{0, 3, 4},{0, 4, 2},{0, 3, 2},{0, 6, 5},
  14.               {0, 5, 5},{0, 0, 3},{0, 11, 0},{1, 8, 6},{0, 7, 2},{0, 5, 2},
  15.               {0, 6, 1},{0, 8, 0},{0, 7, 0},{0, 6, 2},{0, 13, 4},{0, 6, 3},
  16.               {0, 15, 4},{0, 5, 5},{0, 4, 7},{0, 6, 3},{0, 4, 1},{0, 8, 4},
  17.               {0, 14, 5},{0, 2, 3},{0, 9, 3},{0, 6, 1},{0, 7, 6},{0, 10, 2},
  18.               {0, 4, 6},{0, 4, 3},{0, 3, 4},{0, 2, 4},{0, 5, 7},{0, 6, 6},
  19.               {0, 6, 4},{0, 9, 2},{0, 6, 6},{0, 9, 3},{0, 24, 2},{0, 4, 2},
  20.               {0, 5, 4},{0, 7, 3},{1, 1, 3},{0, 11, 4},{0, 5, 3},{0, 5, 7},
  21.               {0, 2, 3},{0, 2, 4},{0, 3, 2},{0, 5, 5},{0, 6, 9},{0, 4, 4},
  22.               {0, 3, 3},{0, 4, 5},{0, 2, 8},{0, 8, 9},{0, 4, 8},{0, 6, 7},
  23.               {0, 7, 3},{0, 7, 3},{0, 5, 4},{0, 6, 4},{0, 4, 5},{0, 5, 1},
  24.               {0, 4, 0},{0, 3, 3},{0, 4, 3},{0, 7, 3},{0, 4, 2},{0, 5, 1},
  25.               {0, 1, 3},{0, 1, 6},{0, 2, 3},{0, 5, 5},{0, 13, 2},{0, 3, 1},
  26.               {0, 5, 7},{0, 5, 1},{0, 11, 7},{0, 10, 4},{0, 7, 5},{0, 11, 5},
  27.               {0, 13, 8},{0, 9, 7},{0, 12, 4},{0, 13, 0},{0, 14, 4},{1, 14, 5},
  28.               {0, 13, 6},{0, 10, 4},{0, 10, 1},{0, 7, 5},{0, 14, 3},{0, 7, 4},
  29.               {0, 7, 4},{0, 8, 6},{0, 7, 5},{0, 11, 1},{0, 14, 3},{0, 4, 1},
  30.               {0, 9, 2},{0, 7, 5},{0, 11, 9},{0, 11, 8},{0, 19, 9},{0, 10, 3},
  31.               {0, 11, 5},{1, 15, 2},{0, 35, 7},{0, 31, 3},{0, 17, 6},{0, 8, 3},
  32.               {0, 18, 2},{0, 11, 2},{0, 9, 7},{0, 8, 2},{0, 8, 5},{0, 8, 4},
  33.               {0, 6, 2},{0, 15, 3},{1, 16, 3},{0, 8, 3},{0, 14, 4},{0, 11, 0},
  34.               {0, 7, 3},{0, 3, 1},{0, 2, 3},{0, 6, 1},{0, 9, 3},{0, 7, 0},
  35.               {0, 5, 0},{0, 7, 1},{0, 1, 1},{0, 6, 1},{0, 7, 5},{0, 9, 5},
  36.               {0, 3, 7},{0, 7, 5},{0, 15, 5},{0, 8, 6},{0, 7, 5},{0, 9, 5},
  37.               {0, 12, 9},{0, 68, 5},{0, 6, 2},{0, 3, 0},{0, 0, 0},{0, 0, 0},
  38.               {0, 1, 0},{0, 0, 0},{0, 0, 0},{0, 9, 0},{0, 0, 0},{0, 0, 0}};
  39.  
  40.  
  41. //takes a 2d array and removes all the leading empty lists
  42. int[][] sanitize (int[][] data) {
  43.  
  44.   int i = 0;
  45.   while ((i < data.length && data[i][0] == 0 && data[i][1] == 0 && data[i][2] == 0)) {
  46.     i += 1;
  47.   }
  48.   return(sublist(data, i, data.length-i));
  49.  
  50. }
  51.  
  52. //splits a 2d array into a 1d array using one element of each list
  53. int[] splitList(int[][] data, int index) {
  54.   int[] list = new int[data.length];
  55.   for(int i = 0; i < data.length; i++) {
  56.     list[i] = data[i][index];
  57.   }
  58.   return list;
  59. }
  60. //returns just the part of a 2d array starting with a given value up to a given length
  61. int[][] sublist(int[][] data, int min, int sublength) {
  62.   int[][] sublist = new int[sublength][3];
  63.   int index = 0;
  64.   for(int i = min; i < min+sublength; i++) {
  65.     sublist[index] = data[i];
  66.     index++;
  67.   }
  68.   return sublist;
  69. }
  70.  
  71. //Converts data index (starting with July 2005) to month/year
  72. String getDate(int index) {
  73.   int year = (index+6)/12+5;
  74.   int month = (index+7)%12;
  75.   if (month == 0) {
  76.     month = 12;
  77.   }
  78.   String date = "";
  79.  
  80.   //accounts for date formatting
  81.   if(month < 10) {
  82.     date += "0";
  83.   }
  84.   date += month;
  85.   date += "/";
  86.   if(year < 10) {
  87.     date += "0";
  88.   }
  89.   date += year;
  90.   return date;
  91. }
  92.  
  93. //returns the LAST index at which a particular value is found
  94. int searchArrayReverse(int[] array, int value) {
  95.   for(int i = array.length-1; i >= 0; i--) {
  96.     if(array[i] == value) {
  97.       return i;
  98.     }
  99.   }
  100.   return -1;
  101. }
  102. //splits 3d year array into 1d array of each year for a given company
  103. int[] yearArray(int[][][] years, int index) {
  104.   int[] perYear = new int[years.length];
  105.   int months;
  106.   for(int i = 0; i < years.length; i++) {
  107.     months = 0;
  108.     for(int j = 0; j < years[0].length; j++) {
  109.       months += years[i][j][index];
  110.     }
  111.     perYear[i] = months;
  112.   }
  113.   return perYear;
  114. }
  115.  
  116. //Finds the sum of an array
  117. int sum(int[] list) {
  118.   int sum = 0;
  119.   for(int i = 0; i < list.length; i++) {
  120.     sum += list[i];
  121.   }
  122.   return sum;
  123. }
  124.  
  125. //Finds the mean of an array
  126. float mean(int[] list)
  127. {
  128.   return float(sum(list))/list.length;
  129. }
  130.  
  131. //Finds median of an array
  132. //(The way I learned to do it, where an even length returns the mean of the medians)
  133. int median(int[] list) {
  134.   if(list.length % 2 == 1) {
  135.     return list[(list.length-1)/2];
  136.   }
  137.   return (list[list.length/2] + list[list.length/2 - 1])/2;
  138. }
  139.  
  140. //Finds standard deviation of an array
  141. float stDev(int[] list) {
  142.  float mean = mean(list);
  143.  float sumSqDev = 0;
  144.  for(int i = 0; i < list.length; i++) {
  145.    sumSqDev += (list[i] - mean) * (list[i] - mean);
  146.  }
  147.  return sqrt(sumSqDev/list.length);
  148. }
  149.  
  150.  
  151. void setup() {
  152.   fullScreen();
  153.   background(255);
  154.  
  155.  
  156.   /*cuts off the months before our companies appear in headlines
  157.   for the first time*/
  158.   int[][] mft = sanitize(raw_data);
  159.  
  160.   //splits the unsanitized data by year
  161.   int[][][] years = new int[raw_data.length/12][12][mft[0].length];
  162.   int index = 0;
  163.   for(int i = 0; i < years.length; i++) {
  164.     for(int j = 0; j < 12; j++) {
  165.       years[i][j] = raw_data[index];
  166.       index++;
  167.     }
  168.   }
  169.    
  170.   //splits the full dataset by company
  171.   int[] myspace = splitList(mft, 0);
  172.   int[] facebook = splitList(mft, 1);
  173.   int[] twitter = splitList(mft, 2);
  174.  
  175.   int xaxis = height*8/10;
  176.   int yaxis = width * 4/10;
  177.   int horizBound = width * 9/10;
  178.   int vertBound = height/10;
  179.   float xinterval = float((horizBound-yaxis))/raw_data.length;
  180.  
  181.   //draw and label axes
  182.   line(yaxis, xaxis, yaxis, vertBound-height/50);
  183.   line(yaxis, xaxis, horizBound, xaxis);
  184.   float tick = yaxis;
  185.   int label = 2006;
  186.   textAlign(CENTER);
  187.   fill(0);
  188.   //Label x-axis
  189.   for(int i = 0; i < years.length; i++) {
  190.     tick += xinterval*12;
  191.     line(tick, xaxis-5, tick, xaxis+5);
  192.     text(str(label).substring(2), tick, xaxis+20);
  193.     label++;
  194.   }
  195.   tick = xaxis;
  196.   int ticksize;
  197.   int max = max(max(myspace), max(twitter), max(facebook));
  198.   //scale graph reasonably
  199.   if(max > 100) {
  200.     ticksize = 10;
  201.   }
  202.   else if(max > 20) {
  203.     ticksize = 5;
  204.   }
  205.   else {
  206.     ticksize = 1;
  207.   }
  208.   //label y-axis  
  209.   for(int i = 0; i < max+ticksize; i += ticksize) {
  210.     line(yaxis-5, tick, yaxis+5, tick);
  211.     text(i, yaxis-15, tick+2);
  212.     tick += (vertBound - xaxis) * ticksize / max;
  213.   }
  214.  
  215.   //Title graph
  216.   float sizeBound = min(height,width);
  217.   textSize(sizeBound/20);
  218.   textAlign(CENTER);
  219.   text("Social Media headlines over time", (yaxis+horizBound)/2, sizeBound/20);
  220.  
  221.  
  222.   //Create facebook graph
  223.   stroke(#D4C651);
  224.   noFill();
  225.   strokeWeight(2);
  226.   beginShape();
  227.   float x = yaxis+7*xinterval;
  228.   for (int i = 0; i < facebook.length; i++)
  229.   {
  230.     vertex(x, xaxis + facebook[i]*(vertBound-xaxis)/max);
  231.     x += xinterval;
  232.   }
  233.   endShape();
  234.  
  235.  
  236.   //Create Twitter graph
  237.   stroke(#1D9BF0);
  238.   beginShape();
  239.   x = yaxis+7*xinterval;
  240.   for (int i = 0; i < twitter.length; i++)
  241.   {
  242.     vertex(x, xaxis + twitter[i]*(vertBound-xaxis)/max);
  243.     x += xinterval;
  244.   }
  245.   endShape();
  246.  
  247.   //Create myspace graph
  248.   stroke(#003399);
  249.   beginShape();
  250.   x = yaxis+7*xinterval;
  251.   for (int i = 0; i < myspace.length; i++)
  252.   {
  253.     vertex(x, xaxis + myspace[i]*(vertBound-xaxis)/max);
  254.     x += xinterval;
  255.   }
  256.   endShape();
  257.  
  258.   //Create legend for graph
  259.   textSize(height/60);
  260.   textAlign(LEFT);
  261.   stroke(255);
  262.   int boxSize = height/60;
  263.   x = width/2;
  264.   int y = vertBound;
  265.   fill(#003399);
  266.   rect(x,y,boxSize, boxSize);
  267.   y += boxSize * 2;
  268.   fill(#D4C651);
  269.   rect(x, y, boxSize, boxSize);
  270.   y += boxSize * 2;
  271.   fill(#1D9BF0);
  272.   rect(x, y, boxSize, boxSize);
  273.  
  274.   fill(0);
  275.   x += boxSize * 1.5;
  276.   y = vertBound + boxSize;
  277.   text("Myspace", x, y);
  278.   y += boxSize * 2;
  279.   text("Facebook", x, y);
  280.   y += boxSize * 2;
  281.   text("Twitter", x, y);
  282.  
  283.  
  284.  
  285.   textSize(sizeBound/15);
  286.   text("Statistics:",sizeBound/15, sizeBound/15);
  287.   textSize(sizeBound/50);
  288.   y = height/10;
  289.   int myX = width/12 + width/20;
  290.   int faX = 2*width/12+width/20;
  291.   int twX = 3*width/12+width/20;
  292.  
  293.   text("Myspace", myX, y);
  294.   text("Facebook", faX, y);
  295.   text("Twitter", twX, y);
  296.  
  297.   stroke(0);
  298.   strokeWeight(1);
  299.  
  300.  
  301.   textSize(max(sizeBound/50, 12));
  302.   int tableRight = yaxis-24;
  303.   int tableTop = y+5;
  304.   int tableLeft = myX - 5;
  305.   line(tableLeft, tableTop, tableRight, tableTop);
  306.   y += height/20;
  307.  
  308.   //Finds most mentions in a month
  309.   text("Max/month", 10, y);
  310.   text(max(myspace), myX, y);
  311.   text(max(facebook), faX, y);
  312.   text(max(twitter), twX, y);
  313.   line(tableLeft, y+5, tableRight, y+5);
  314.   y += height/20;
  315.  
  316.   //Finds LAST month with max mentions
  317.   text("Best month", 10, y);
  318.   text(getDate(searchArrayReverse(myspace, max(myspace))), myX, y);
  319.   text(getDate(searchArrayReverse(facebook, max(facebook))), faX, y);
  320.   text(getDate(searchArrayReverse(twitter, max(twitter))), twX, y);
  321.   line(tableLeft, y+5, tableRight, y+5);
  322.   y += height/20;
  323.  
  324.   //Creates an array of years by company
  325.   int[] myYears = yearArray(years, 0);
  326.   int[] faYears = yearArray(years, 1);
  327.   int[] twYears = yearArray(years, 2);
  328.  
  329.   //Finds most mentions per year
  330.   text("Max/year", 10, y);
  331.   text(max(myYears), myX, y);
  332.   text(max(faYears), faX, y);
  333.   text(max(twYears), twX, y);
  334.   line(tableLeft, y+5, tableRight, y+5);
  335.   y += height/20;
  336.  
  337.   //Finds LAST month with max mentions
  338.   text("Best year", 10, y);
  339.   text(searchArrayReverse(myYears, max(myYears)) + 2005, myX, y);
  340.   text(searchArrayReverse(faYears, max(faYears)) + 2005, faX, y);
  341.   text(searchArrayReverse(twYears, max(twYears)) + 2005, twX, y);
  342.   line(tableLeft, y+5, tableRight, y+5);
  343.   y += height/20;
  344.  
  345.   //Finds the mean mentions per year
  346.   text("Mean/year", 10, y);
  347.   text(mean(myYears), myX, y);
  348.   text(mean(faYears), faX, y);
  349.   text(mean(twYears), twX, y);
  350.   line(tableLeft, y+5, tableRight, y+5);
  351.   y += height/20;
  352.  
  353.   //Finds the standard deviation of mentions per year
  354.   text("StDev/year", 10, y);
  355.   text(stDev(myYears), myX, y);
  356.   text(stDev(faYears), faX, y);
  357.   text(stDev(twYears), twX, y);
  358.   line(tableLeft, y+5, tableRight, y+5);
  359.   y+= height/20;
  360.  
  361.   //Finds the median mentions per year
  362.   text("Median/year", 10, y);
  363.   text(median(myYears), myX, y);
  364.   text(median(faYears), faX, y);
  365.   text(median(twYears), twX, y);
  366.   line(tableLeft, y+5, tableRight, y+5);
  367.  
  368.   //Fill in table grid
  369.   line(tableLeft, tableTop, tableRight, tableTop);
  370.   line(myX-5, tableTop, myX-5, y+5);
  371.   line(faX-5, tableTop, faX-5, y+5);
  372.   line(twX-5, tableTop, twX-5, y+5);
  373.   line(tableRight, tableTop, tableRight, y+5);
  374.  
  375.   //Display some notes!
  376.   String notes = "Notes: Mode not included, since it would have been 0 across the board. \n"
  377.   + "The minima are all likewise 0.\n"
  378.   + "Although the data source included headlines starting from 2003, the first relevant headline appeared in 2005.\n"
  379.   + "The data end in 2021, but it is plausible that Twitter might have overtaken Facebook's headlines "
  380.   + "in 2022, when Elon Musk bought the company.\n"
  381.   + "Interestingly, the spike for Facebook in February of 2021 might have been region-specific, "
  382.   + "when Facebook restricted access to news content in Australia.";
  383.   text(notes, 10, y+20, tableRight-10, height-20);
  384.  
  385.   //Display data source
  386.   String source = "Source: Rohit Kulkarni, \"A Million News Headlines\"\n "
  387.   + "(19 years of Australian Broadcasting Corporation headlines)\n"
  388.   + "https://www.kaggle.com/datasets/therohk/million-headlines";
  389.   text(source, yaxis, xaxis+30, width-20, height);
  390.   print(horizBound);
  391. }