Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class KWSentiment_Mapper extends Mapper<LongWritable, Text, Text, IntWritable> {
- ArrayList<String> keywordsList = new ArrayList<String>();
- ArrayList<String> posWordsList = new ArrayList<String>();
- ArrayList<String> tokensList = new ArrayList<String>();
- int e;
- @Override
- protected void setup(Context context) throws IOException, InterruptedException {
- try {
- FileReader keywordsFile = new FileReader(keywordsPath);
- Scanner keywords = new Scanner(keywordsFile);
- FileReader posWordsFile = new FileReader(posWordsPath);
- Scanner posWords = new Scanner(posWordsFile);
- while (keywords.hasNextLine()) {
- keywordsList.add(keywords.nextLine());
- }
- while (posWords.hasNextLine()) {
- posWordsList.add(posWords.nextLine());
- }
- keywords.close();
- posWords.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- @Override
- public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
- String[] line = value.toString().split("t");
- String Review = line[4].replaceAll("[\-\+\\)\.\("\{\$\^:,]", "").toLowerCase();
- StringTokenizer tokenizer = new StringTokenizer(Review);
- while (tokenizer.hasMoreTokens()) {
- // 1- first read the review line and store the tokens in an arraylist, 2-
- // iterate through review to check for KW if found
- // 3-check if there's PosWord near (upto +3 and -2)
- // 4- setWord & context.write 5- null the review line arraylist
- String CompareString = tokenizer.nextToken();
- tokensList.add(CompareString);
- }
- {
- for (int i = 0; i < tokensList.size(); i++)
- {
- for (int j = 0; j < keywordsList.size(); j++) {
- boolean flag = false;
- System.out.println("Checking");
- System.out.println(tokensList.get(i));
- System.out.println(keywordsList.get(j));
- if (tokensList.get(i).startsWith(keywordsList.get(j)) == true) {
- System.out.println("KW Match found");
- for (int e = Math.max(0, i - 2); e < Math.min(tokensList.size(), i + 4); e++) {
- if (posWordsList.contains(tokensList.get(e))) {
- System.out.println("Near \+ve Word found");
- word.set(keywordsList.get(j));
- context.write(word, one);
- flag = true;
- break; // breaks out of e loop }}
- }
- }
- }
- if (flag)
- break;
- }
- }
- tokensList.clear();
- }
- InputStream inputStream = null;
- Reader reader = null;
- try
- {
- inputStream = new FileInputStream("file.txt");
- reader = new InputStreamReader(inputStream);
- CSVReader csvReader = new CSVReader(reader, 't', ''', '\', 0);
- String [] nextLine;
- while ((nextLine = csvReader.readNext()) != null) {
- // nextLine[] is an array of values from the line
- System.out.println(nextLine[0] + nextLine[1] + "etc...");
- }
- }
- catch(Exception e)
- {
- if(reader != null)
- reader.close();
- if(inputStream != null)
- inputStream.close();
- }
Add Comment
Please, Sign In to add comment