Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.12 KB | None | 0 0
  1. /**
  2. * Creates a graph of RAT objects based on information from a TSV.
  3. *
  4. * @author Kathryn Swint
  5. * @version 12/05/2019
  6. */
  7.  
  8. import javafoundations.*;
  9. import java.util.LinkedList;
  10. import java.util.Scanner;
  11. import java.util.Hashtable;
  12. import java.io.File;
  13. import java.io.FileNotFoundException;
  14.  
  15. public class RATgraph
  16. {
  17. // instance variables
  18. protected AdjListsGraph<String> graph; //how we'll graph the RATs and stories
  19.  
  20. protected Hashtable<String,RAT> accounts; //a hashtable of RATs.
  21. //key = username, value = RAT object
  22. protected Hashtable<String,Integer> stories; //a hashtable of stories. key = story,
  23. //value = # of times the story was tweeted about
  24. protected String[] usernames; //array of RAT usernames
  25. protected String[] userIDs; //array of RAT userIDs
  26. protected long[] tweetCounts; //array of RAT tweetCounts
  27. protected long[] storyCounts; //array of RAT storyCounts
  28.  
  29. protected int usernamesSize; //number of usernames in the usernames array
  30. protected int userIDsSize; //number of userIDs in the userIDs array
  31. protected int tweetCountsSize; //number of tweetCounts in the tweetCounts array
  32. protected int storyCountsSize; //number of storyCounts in the storyCounts array
  33.  
  34. /**
  35. * Constructor for objects of class RATgraph
  36. *
  37. * @param fileName the name of the file to read from
  38. */
  39. public RATgraph(String fileName)
  40. {
  41. // initialise instance variables
  42. accounts = new Hashtable<String,RAT>();
  43. stories = new Hashtable<String,Integer>();
  44.  
  45. usernames = new String[300]; //assumes there are <=300 RATs
  46. userIDs = new String[300];
  47. tweetCounts = new long[300];
  48. storyCounts = new long[300];
  49.  
  50. usernamesSize = 0;
  51. userIDsSize = 0;
  52. tweetCountsSize = 0;
  53. storyCountsSize = 0;
  54.  
  55. readFromFile(fileName); //reads RATs from file
  56.  
  57. graph = new AdjListsGraph<String>();
  58. createGraph(fileName); //creates the graph of RATs
  59. }
  60.  
  61. /**
  62. * Reads from a TSV and creates a new RAT object with the information on each
  63. * line.
  64. *
  65. * @param fileName the file to read from
  66. */
  67. public void readFromFile(String fileName)
  68. {
  69. try{
  70. Scanner scan = new Scanner(new File(fileName));
  71. String header = scan.nextLine(); //takes care of file header
  72.  
  73. while (scan.hasNext()) {
  74. String[] input = scan.nextLine().split("\t"); //splits @ tabs since file is a TSV
  75.  
  76. String user = input[0];
  77. String accountNum = input[1];
  78. long tweetCount = Long.parseLong(input[2]);
  79. long storyCount = Long.parseLong(input[3]);
  80.  
  81. addUsername(user); //adds username to the username array
  82. addUserID(accountNum); //adds userID to the userID array
  83.  
  84. RAT current = new RAT(user, accountNum, tweetCount, storyCount);
  85. String[] currentStories = input[4].split(","); //splits the stories at commas
  86.  
  87. for (String s : currentStories) {
  88. current.addStory(s); //adds the story to the current RAT
  89. if (!stories.containsKey(s)) {
  90. stories.put(s, 1); //if the story is not in the stories Hashtable,
  91. //adds it with a "tweeted about" count of 1
  92. } else {
  93. stories.replace(s, (1 + stories.get(s))); //increases "tweeted about" count
  94. }
  95. }
  96.  
  97. accounts.put(user, current); //adds this RAT to the hashtable of RATs
  98. }
  99.  
  100. scan.close();
  101. } catch (FileNotFoundException ex) {
  102. System.out.println("File " + fileName + " was not found.");
  103. } catch (NumberFormatException ex) {
  104. System.out.println(ex);
  105. }
  106. }
  107.  
  108. /**
  109. * Adds a username to the usernames array.
  110. *
  111. * @param u the username to add
  112. */
  113. private void addUsername(String u) {
  114. if (usernamesSize == usernames.length) {
  115. String[] temporary = new String[usernames.length * 2];
  116.  
  117. for (int i = 0; i < usernames.length; i++) {
  118. temporary[i] = usernames[i];
  119. }
  120.  
  121. usernames = temporary;
  122. }
  123.  
  124. usernames[usernamesSize++] = u; //adds the username to the array & increases usernamesSize
  125. }
  126.  
  127. /**
  128. * Adds a userID to the userIDs array
  129. *
  130. * @param u the userID to add
  131. */
  132. private void addUserID(String u) {
  133. if (userIDsSize == userIDs.length) {
  134. String[] temporary = new String[userIDs.length * 2];
  135.  
  136. for (int i = 0; i < userIDs.length; i++) {
  137. temporary[i] = userIDs[i];
  138. }
  139.  
  140. userIDs = temporary;
  141. }
  142.  
  143. userIDs[userIDsSize++] = u; //adds the userID to the array & increases userIDsSize
  144. }
  145.  
  146. /**
  147. * Adds a RAT's tweet count to the tweetCounts array
  148. *
  149. * @param u the tweet count to add
  150. */
  151. private void addTweetCounts(long u) {
  152. if (tweetCountsSize == tweetCounts.length) {
  153. long[] temporary = new long[tweetCounts.length * 2];
  154.  
  155. for (int i = 0; i < tweetCounts.length; i++) {
  156. temporary[i] = tweetCounts[i];
  157. }
  158.  
  159. tweetCounts = temporary;
  160. }
  161.  
  162. tweetCounts[tweetCountsSize++] = u; //adds the tweetCount to the array & increases tweetCountsSize
  163. }
  164.  
  165. /**
  166. * Adds a RAT's story count to the storyCounts array
  167. *
  168. * @param u the story count to add
  169. */
  170. private void addStoryCounts(long u) {
  171. if (storyCountsSize == storyCounts.length) {
  172. long[] temporary = new long[storyCounts.length * 2];
  173.  
  174. for (int i = 0; i < storyCounts.length; i++) {
  175. temporary[i] = storyCounts[i];
  176. }
  177.  
  178. storyCounts = temporary;
  179. }
  180.  
  181. storyCounts[storyCountsSize++] = u; //adds the storyCount to the array & increases storyCountsSize
  182. }
  183.  
  184. /**
  185. * Creates a graph from the accounts and stories Vertex structures.
  186. *
  187. * @param fileName the fileName you read from previously (used here
  188. * for the exception).
  189. */
  190. protected void createGraph(String fileName)
  191. {
  192. try {
  193. String currentUser;
  194. LinkedList<String> currentStories;
  195. RAT currentRAT;
  196. String currentU;
  197.  
  198. for (int i = 0; i < usernamesSize; i++) {
  199. currentU = usernames[i]; //gets username from usernames array
  200. currentRAT = accounts.get(currentU); //gets RAT from accounts using username as key
  201.  
  202. graph.addVertex(currentU); //adds username as a vertex to the graph
  203. for (String story : currentRAT.stories) {
  204. graph.addVertex(story); //adds the story to the graph
  205.  
  206. //used arcs instead of edges here to ensure that yEd correctly displayed
  207. //our bipartite graph. We're unsure why edges created a bug in yEd.
  208. graph.addArc(currentU, story); //creates an arc between the user and story
  209. graph.addArc(story, currentU); //creates an arc between the story and use
  210. }
  211. }
  212. } catch (NullPointerException ex) {
  213. System.out.println(ex + " for file " + fileName);
  214. }
  215. }
  216.  
  217. public static void main(String[] args) {
  218. RATgraph r = new RATgraph("All_Russian-Accounts-in-TT-stories.csv.tsv");
  219. //RATgraph r = new RATgraph("TestFile1.txt");
  220.  
  221. System.out.println(r.accounts);
  222. System.out.println(r.stories);
  223. System.out.println(r.accounts.size());
  224.  
  225. System.out.println(r.graph);
  226. r.graph.saveToTGF("RATgraph.tgf");
  227.  
  228. System.out.println("Testing DFTraversal on 155892608: " + r.graph.DFtraversal("155892608"));
  229. }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement