Guest User

Untitled

a guest
Jan 21st, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. public class KWSentiment_Mapper extends Mapper<LongWritable, Text, Text, IntWritable> {
  2. ArrayList<String> keywordsList = new ArrayList<String>();
  3. ArrayList<String> posWordsList = new ArrayList<String>();
  4. ArrayList<String> tokensList = new ArrayList<String>();
  5. int e;
  6.  
  7. @Override
  8. protected void setup(Context context) throws IOException, InterruptedException {
  9. try {
  10. FileReader keywordsFile = new FileReader(keywordsPath);
  11. Scanner keywords = new Scanner(keywordsFile);
  12. FileReader posWordsFile = new FileReader(posWordsPath);
  13. Scanner posWords = new Scanner(posWordsFile);
  14.  
  15. while (keywords.hasNextLine()) {
  16. keywordsList.add(keywords.nextLine());
  17. }
  18. while (posWords.hasNextLine()) {
  19. posWordsList.add(posWords.nextLine());
  20. }
  21. keywords.close();
  22. posWords.close();
  23. } catch (FileNotFoundException e) {
  24. e.printStackTrace();
  25. }
  26.  
  27. }
  28.  
  29. @Override
  30. public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
  31.  
  32. String[] line = value.toString().split("t");
  33. String Review = line[4].replaceAll("[\-\+\\)\.\("\{\$\^:,]", "").toLowerCase();
  34.  
  35. StringTokenizer tokenizer = new StringTokenizer(Review);
  36.  
  37. while (tokenizer.hasMoreTokens()) {
  38. // 1- first read the review line and store the tokens in an arraylist, 2-
  39. // iterate through review to check for KW if found
  40. // 3-check if there's PosWord near (upto +3 and -2)
  41. // 4- setWord & context.write 5- null the review line arraylist
  42. String CompareString = tokenizer.nextToken();
  43.  
  44. tokensList.add(CompareString);
  45. }
  46. {
  47. for (int i = 0; i < tokensList.size(); i++)
  48.  
  49. {
  50.  
  51. for (int j = 0; j < keywordsList.size(); j++) {
  52. boolean flag = false;
  53. System.out.println("Checking");
  54. System.out.println(tokensList.get(i));
  55. System.out.println(keywordsList.get(j));
  56.  
  57. if (tokensList.get(i).startsWith(keywordsList.get(j)) == true) {
  58. System.out.println("KW Match found");
  59.  
  60. for (int e = Math.max(0, i - 2); e < Math.min(tokensList.size(), i + 4); e++) {
  61.  
  62. if (posWordsList.contains(tokensList.get(e))) {
  63. System.out.println("Near \+ve Word found");
  64. word.set(keywordsList.get(j));
  65. context.write(word, one);
  66. flag = true;
  67.  
  68. break; // breaks out of e loop }}
  69. }
  70. }
  71. }
  72. if (flag)
  73. break;
  74. }
  75. }
  76. tokensList.clear();
  77. }
  78.  
  79. InputStream inputStream = null;
  80. Reader reader = null;
  81. try
  82. {
  83. inputStream = new FileInputStream("file.txt");
  84. reader = new InputStreamReader(inputStream);
  85. CSVReader csvReader = new CSVReader(reader, 't', '"', '\', 0);
  86.  
  87. String [] nextLine;
  88. while ((nextLine = csvReader.readNext()) != null) {
  89. // nextLine[] is an array of values from the line
  90. System.out.println(nextLine[0] + nextLine[1] + "etc...");
  91. }
  92. }
  93. finally
  94. {
  95. if(reader != null)
  96. reader.close();
  97. if(inputStream != null)
  98. inputStream.close();
  99. }
Add Comment
Please, Sign In to add comment