task_type
stringclasses
4 values
code_task
stringclasses
15 values
start_line
int64
4
1.79k
end_line
int64
4
1.8k
before
stringlengths
79
76.1k
between
stringlengths
17
806
after
stringlengths
2
72.6k
reason_categories_output
stringlengths
2
2.24k
horizon_categories_output
stringlengths
83
3.99k
reason_freq_analysis
stringclasses
150 values
horizon_freq_analysis
stringlengths
23
185
infilling_java
CounterTester
76
77
['import junit.framework.TestCase;', 'import java.io.*;', 'import java.util.HashMap;', 'import java.util.TreeMap;', 'import java.util.ArrayList;', '', '/**', ' * This silly little class wraps String, int pairs so they can be sorted.', ' * Used by the WordCounter class.', ' */', 'class WordWrapper implements Comparable <WordWrapper> {', '', ' private String myWord;', ' private int myCount;', ' private int mySlot;', '', ' /**', ' * Creates a new WordWrapper with string "XXX" and count zero.', ' */', ' public WordWrapper (String myWordIn, int myCountIn, int mySlotIn) {', ' myWord = myWordIn;', ' myCount = myCountIn;', ' mySlot = mySlotIn;', ' }', ' /**', ' * Returns a copy of the string in the WordWrapper.', ' *', ' * @return The word contained in this object. Is copied out: no aliasing.', ' */', ' public String extractWord () {', ' return new String (myWord);', ' }', '', ' /**', ' * Returns the count in this WordWrapper.', ' *', ' * @return The count contained in the object.', ' */', ' public int extractCount () {', ' return myCount;', ' }', '', ' public void incCount () {', ' myCount++;', ' }', '', ' public int getCount () {', ' return myCount;', ' }', '', ' public int getPos () {', ' return mySlot;', ' }', '', ' public void setPos (int toWhat) {', ' ', ' mySlot = toWhat;', ' }', '', ' public String toString () {', ' return myWord + " " + mySlot;', ' }', '', ' /**', ' * Comparison operator so we implement the Comparable interface.', ' *', ' * @param w The word wrapper to compare to', ' * @return a positive, zero, or negative value depending upon whether this', ' * word wrapper is less then, equal to, or greater than param w. The', ' * relationship is determied by looking at the counts.', ' */', ' public int compareTo (WordWrapper w) {', " // Check if the count of the current word (myCount) is equal to the count of the word 'w'", ' // If the counts are equal, compare the words lexicographically', ' if (w.myCount == myCount) {']
[' return myWord.compareTo(w.myWord);', ' }']
[" // If the counts are not equal, subtract the count of the current word from the count of the word 'w'", ' return w.myCount - myCount;', ' }', '}', '', '', '/**', ' * This class is used to count occurences of words (strings) in a corpus. Used by', ' * the IndexedDocumentCollection class to find the most frequent words in the corpus.', ' */', '', 'class WordCounter {', ' ', " // this is the map that holds (for each word) the number of times we've seen it", ' // note that Integer is a wrapper for the buitin Java int type', ' private HashMap <String, WordWrapper> wordsIHaveSeen = new HashMap <String, WordWrapper> ();', ' private TreeMap <String, String> ones = new TreeMap <String, String> ();', ' ', ' // the set of words that have been extracted', ' private ArrayList<WordWrapper> extractedWords = new ArrayList <WordWrapper> ();', '', ' /**', ' * Accepts a word and increments the count of the number of times we have seen it.', ' * Note that a deep copy of the param is done (no aliasing).', ' *', ' * @param addMe The string to count', ' */', ' public void insert (String addMe) {', ' if (ones.containsKey (addMe)) {', ' ones.remove (addMe);', ' WordWrapper temp = new WordWrapper (addMe, 1, extractedWords.size ());', ' wordsIHaveSeen.put(addMe, temp);', ' extractedWords.add (temp);', ' }', '', ' // first check to see if the word is alredy in wordsIHaveSeen', ' if (wordsIHaveSeen.containsKey (addMe)) {', ' // if it is, then remove and increment its count', ' WordWrapper temp = wordsIHaveSeen.get (addMe);', ' temp.incCount ();', '', ' // find the slot that we go to in the extractedWords list', " // by sorting the 'extractedWords' list in ascending order", ' for (int i = temp.getPos () - 1; i >= 0 && ', ' extractedWords.get (i).compareTo (extractedWords.get (i + 1)) > 0; i--) {', ' temp = extractedWords.get (i + 1);', ' temp.setPos (i);', ' ', ' WordWrapper temp2 = extractedWords.get (i);', ' temp2.setPos (i + 1);', ' ', ' extractedWords.set (i + 1, temp2);', ' extractedWords.set (i, temp);', ' }', '', ' // in this case it is not there, so just add it', ' } else {', ' ones.put (addMe, addMe);', ' }', ' }', '', ' /**', ' * Returns the kth most frequent word in the corpus so far. A deep copy is returned,', ' * so no aliasing is possible. Returns null if there are not enough words.', ' *', ' * @param k Note that the most frequent word is at k = 0', ' * @return The kth most frequent word in the corpus to date', ' */', ' public String getKthMostFrequent (int k) {', '', ' // if we got here, then the array is all set up, so just return the kth most frequent word', ' if (k >= extractedWords.size ()) {', ' int which = extractedWords.size ();', ' for (String s : ones.navigableKeySet ()) {', ' if (which == k)', ' return s;', ' which++;', ' }', ' return null;', ' } else {', ' // If k is less than the size of the extractedWords list,', ' // return the kth most frequent word from extractedWords', ' return extractedWords.get (k).extractWord ();', ' }', ' }', '}', '/**', ' * A JUnit test case class.', ' * Every method starting with the word "test" will be called when running', ' * the test with JUnit.', ' */', 'public class CounterTester extends TestCase {', '', ' /**', ' * File object to read words from, with buffering.', ' */', ' private FileReader file = null;', ' private BufferedReader reader = null;', '', ' /**', ' * Close file readers.', ' */', ' private void closeFiles() {', ' try {', ' if (file != null) {', ' file.close();', ' file = null;', ' }', ' if (reader != null) {', ' reader.close();', ' reader = null;', ' }', ' } catch (Exception e) {', ' System.err.println("Problem closing file");', ' System.err.println(e);', ' e.printStackTrace();', ' }', ' }', '', ' /**', ' * Close files no matter what happens in the test.', ' */', ' protected void tearDown () {', ' closeFiles();', ' }', '', ' /**', ' * Open file and set up readers.', ' *', ' * @param fileName name of file to open', ' * */', ' private void openFile(String fileName) {', ' try {', ' file = new FileReader(fileName);', ' reader = new BufferedReader(file);', ' } catch (Exception e) {', ' System.err.format("Problem opening %s file\\n", fileName);', ' System.err.println(e);', ' e.printStackTrace();', ' fail();', ' }', ' }', '', ' /**', ' * Read the next numWords from the file.', ' *', ' * @param counter word counter to update', ' * @param numWords number of words to read.', ' */', ' private void readWords(WordCounter counter, int numWords) {', ' try {', ' for (int i=0; i<numWords; i++) {', ' if (i % 100000 == 0)', ' System.out.print (".");', ' String word = reader.readLine();', ' if (word == null) {', ' return;', ' }', " // Insert 'word' into the counter.", ' counter.insert(word);', ' }', ' } catch (Exception e) {', ' System.err.println("Problem reading file");', ' System.err.println(e);', ' e.printStackTrace();', ' fail();', ' }', ' }', '', ' /**', ' * Read the next numWords from the file, mixing with queries', ' *', ' * @param counter word counter to update', ' * @param numWords number of words to read.', ' */', ' private void readWordsMixed (WordCounter counter, int numWords) {', ' try {', ' int j = 0;', ' for (int i=0; i<numWords; i++) {', ' String word = reader.readLine();', " // If 'word' is null, it means we've reached the end of the file. So, return from the method.", ' if (word == null) {', ' return;', ' }', ' counter.insert(word);', '', ' // If the current iteration number is a multiple of 10 and greater than 100,000, perform a query of the kth most frequent word.', ' if (i % 10 == 0 && i > 100000) {', ' String myStr = counter.getKthMostFrequent(j++);', ' }', '', ' // rest j once we get to 100', ' if (j == 100)', ' j = 0;', ' }', ' } catch (Exception e) {', ' System.err.println("Problem reading file");', ' System.err.println(e);', ' e.printStackTrace();', ' fail();', ' }', ' }', '', ' /**', ' * Check that a sequence of words starts at the "start"th most', ' * frequent word.', ' *', ' * @param counter word counter to lookup', ' * @param start frequency index to start checking at', ' * @param expected array of expected words that start at that frequency', ' */', ' private void checkExpected(WordCounter counter, int start, String [] expected) {', ' for (int i = 0; i<expected.length; i++) {', ' String actual = counter.getKthMostFrequent(start);', ' System.out.format("k: %d, expected: %s, actual: %s\\n",', ' start, expected[i], actual);', ' assertEquals(expected[i], actual);', ' start++;', ' }', ' }', '', ' /**', ' * A test method.', ' * (Replace "X" with a name describing the test. You may write as', ' * many "testSomething" methods in this class as you wish, and each', ' * one will be called when running JUnit over this class.)', ' */', ' public void testSimple() {', ' System.out.println("\\nChecking insert");', ' WordCounter counter = new WordCounter();', ' counter.insert("pizzaz");', ' // Insert the word "pizza" into the counter twice', ' counter.insert("pizza");', ' counter.insert("pizza");', ' String [] expected = {"pizza"};', ' checkExpected(counter, 0, expected);', ' }', '', ' public void testTie() {', ' System.out.println("\\nChecking tie for 2nd place");', ' WordCounter counter = new WordCounter();', ' counter.insert("panache");', ' counter.insert("pizzaz");', ' counter.insert("pizza");', ' counter.insert("zebra");', ' counter.insert("pizza");', ' counter.insert("lion");', ' counter.insert("pizzaz");', ' counter.insert("panache");', ' counter.insert("panache");', ' counter.insert("camel");', ' // order is important here', ' String [] expected = {"pizza", "pizzaz"};', ' checkExpected(counter, 1, expected);', ' }', '', '', ' public void testPastTheEnd() {', ' System.out.println("\\nChecking past the end");', ' WordCounter counter = new WordCounter();', ' counter.insert("hi");', ' counter.insert("hello");', ' counter.insert("greetings");', ' counter.insert("salutations");', ' counter.insert("hi");', ' counter.insert("welcome");', ' counter.insert("goodbye");', ' counter.insert("later");', ' counter.insert("hello");', ' counter.insert("when");', ' counter.insert("hi");', ' assertNull(counter.getKthMostFrequent(8));', ' }', '', ' public void test100Top5() {', ' System.out.println("\\nChecking top 5 of 100 words");', ' WordCounter counter = new WordCounter();', ' // Open the file "allWordsBig"', ' openFile("allWordsBig");', ' // Read the next 100 words', ' readWords(counter, 100);', ' String [] expected = {"edu", "comp", "cs", "windows", "cmu"};', ' checkExpected(counter, 0, expected);', ' }', '', ' public void test300Top5() {', ' System.out.println("\\nChecking top 5 of first 100 words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter, 100);', ' String [] expected1 = {"edu", "comp", "cs", "windows", "cmu"};', ' checkExpected(counter, 0, expected1);', '', ' System.out.println("Adding 100 more words and rechecking top 5");', ' readWords(counter, 100);', ' String [] expected2 = {"edu", "cmu", "comp", "cs", "state"};', ' checkExpected(counter, 0, expected2);', '', ' System.out.println("Adding 100 more words and rechecking top 5");', ' readWords(counter, 100);', ' String [] expected3 = {"edu", "cmu", "comp", "ohio", "state"};', ' checkExpected(counter, 0, expected3);', ' }', '', ' public void test300Words14Thru19() {', ' System.out.println("\\nChecking rank 14 through 19 of 300 words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' // Read the next 300 words', ' readWords(counter, 300);', ' String [] expected = {"cantaloupe", "from", "ksu", "okstate", "on", "srv"};', ' checkExpected(counter, 14, expected);', ' }', '', ' public void test300CorrectNumber() {', ' System.out.println("\\nChecking correct number of unique words in 300 words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter, 300);', ' /* check the 122th, 123th, and 124th most frequent word in the corpus so far ', ' * and check if the result is not null.', ' */', ' assertNotNull(counter.getKthMostFrequent(122));', ' assertNotNull(counter.getKthMostFrequent(123));', ' assertNotNull(counter.getKthMostFrequent(124));', ' // check the 125th most frequent word in the corpus so far', ' // and check if the result is null.', ' assertNull(counter.getKthMostFrequent(125));', ' }', '', ' public void test300and100() {', ' System.out.println("\\nChecking top 5 of 100 and 300 words with two counters");', ' WordCounter counter1 = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter1, 300);', ' closeFiles();', '', ' WordCounter counter2 = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter2, 100);', '', ' String [] expected1 = {"edu", "cmu", "comp", "ohio", "state"};', ' checkExpected(counter1, 0, expected1);', '', ' String [] expected2 = {"edu", "comp", "cs", "windows", "cmu"};', ' checkExpected(counter2, 0, expected2);', ' }', '', ' public void testAllTop15() {', ' System.out.println("\\nChecking top 15 of all words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' // Read the next 6000000 words', ' readWords(counter, 6000000);', ' String [] expected = {"the", "edu", "to", "of", "and",', ' "in", "is", "ax", "that", "it",', ' "cmu", "for", "com", "you", "cs"};', ' checkExpected(counter, 0, expected);', ' }', '', ' public void testAllTop10000() {', ' System.out.println("\\nChecking time to get top 10000 of all words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter, 6000000);', '', ' for (int i=0; i<10000; i++) {', ' counter.getKthMostFrequent(i);', ' }', ' }', '', ' public void testSpeed() {', ' System.out.println("\\nMixing adding data with finding top k");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWordsMixed (counter, 6000000);', ' String [] expected = {"the", "edu", "to", "of", "and",', ' "in", "is", "ax", "that", "it",', ' "cmu", "for", "com", "you", "cs"};', ' checkExpected(counter, 0, expected);', ' }', '', '}']
[{'reason_category': 'If Body', 'usage_line': 76}, {'reason_category': 'If Body', 'usage_line': 77}]
Global_Variable 'myWord' used at line 76 is defined at line 13 and has a Long-Range dependency. Variable 'w' used at line 76 is defined at line 72 and has a Short-Range dependency.
{'If Body': 2}
{'Global_Variable Long-Range': 1, 'Variable Short-Range': 1}
infilling_java
CounterTester
79
80
['import junit.framework.TestCase;', 'import java.io.*;', 'import java.util.HashMap;', 'import java.util.TreeMap;', 'import java.util.ArrayList;', '', '/**', ' * This silly little class wraps String, int pairs so they can be sorted.', ' * Used by the WordCounter class.', ' */', 'class WordWrapper implements Comparable <WordWrapper> {', '', ' private String myWord;', ' private int myCount;', ' private int mySlot;', '', ' /**', ' * Creates a new WordWrapper with string "XXX" and count zero.', ' */', ' public WordWrapper (String myWordIn, int myCountIn, int mySlotIn) {', ' myWord = myWordIn;', ' myCount = myCountIn;', ' mySlot = mySlotIn;', ' }', ' /**', ' * Returns a copy of the string in the WordWrapper.', ' *', ' * @return The word contained in this object. Is copied out: no aliasing.', ' */', ' public String extractWord () {', ' return new String (myWord);', ' }', '', ' /**', ' * Returns the count in this WordWrapper.', ' *', ' * @return The count contained in the object.', ' */', ' public int extractCount () {', ' return myCount;', ' }', '', ' public void incCount () {', ' myCount++;', ' }', '', ' public int getCount () {', ' return myCount;', ' }', '', ' public int getPos () {', ' return mySlot;', ' }', '', ' public void setPos (int toWhat) {', ' ', ' mySlot = toWhat;', ' }', '', ' public String toString () {', ' return myWord + " " + mySlot;', ' }', '', ' /**', ' * Comparison operator so we implement the Comparable interface.', ' *', ' * @param w The word wrapper to compare to', ' * @return a positive, zero, or negative value depending upon whether this', ' * word wrapper is less then, equal to, or greater than param w. The', ' * relationship is determied by looking at the counts.', ' */', ' public int compareTo (WordWrapper w) {', " // Check if the count of the current word (myCount) is equal to the count of the word 'w'", ' // If the counts are equal, compare the words lexicographically', ' if (w.myCount == myCount) {', ' return myWord.compareTo(w.myWord);', ' }', " // If the counts are not equal, subtract the count of the current word from the count of the word 'w'"]
[' return w.myCount - myCount;', ' }']
['}', '', '', '/**', ' * This class is used to count occurences of words (strings) in a corpus. Used by', ' * the IndexedDocumentCollection class to find the most frequent words in the corpus.', ' */', '', 'class WordCounter {', ' ', " // this is the map that holds (for each word) the number of times we've seen it", ' // note that Integer is a wrapper for the buitin Java int type', ' private HashMap <String, WordWrapper> wordsIHaveSeen = new HashMap <String, WordWrapper> ();', ' private TreeMap <String, String> ones = new TreeMap <String, String> ();', ' ', ' // the set of words that have been extracted', ' private ArrayList<WordWrapper> extractedWords = new ArrayList <WordWrapper> ();', '', ' /**', ' * Accepts a word and increments the count of the number of times we have seen it.', ' * Note that a deep copy of the param is done (no aliasing).', ' *', ' * @param addMe The string to count', ' */', ' public void insert (String addMe) {', ' if (ones.containsKey (addMe)) {', ' ones.remove (addMe);', ' WordWrapper temp = new WordWrapper (addMe, 1, extractedWords.size ());', ' wordsIHaveSeen.put(addMe, temp);', ' extractedWords.add (temp);', ' }', '', ' // first check to see if the word is alredy in wordsIHaveSeen', ' if (wordsIHaveSeen.containsKey (addMe)) {', ' // if it is, then remove and increment its count', ' WordWrapper temp = wordsIHaveSeen.get (addMe);', ' temp.incCount ();', '', ' // find the slot that we go to in the extractedWords list', " // by sorting the 'extractedWords' list in ascending order", ' for (int i = temp.getPos () - 1; i >= 0 && ', ' extractedWords.get (i).compareTo (extractedWords.get (i + 1)) > 0; i--) {', ' temp = extractedWords.get (i + 1);', ' temp.setPos (i);', ' ', ' WordWrapper temp2 = extractedWords.get (i);', ' temp2.setPos (i + 1);', ' ', ' extractedWords.set (i + 1, temp2);', ' extractedWords.set (i, temp);', ' }', '', ' // in this case it is not there, so just add it', ' } else {', ' ones.put (addMe, addMe);', ' }', ' }', '', ' /**', ' * Returns the kth most frequent word in the corpus so far. A deep copy is returned,', ' * so no aliasing is possible. Returns null if there are not enough words.', ' *', ' * @param k Note that the most frequent word is at k = 0', ' * @return The kth most frequent word in the corpus to date', ' */', ' public String getKthMostFrequent (int k) {', '', ' // if we got here, then the array is all set up, so just return the kth most frequent word', ' if (k >= extractedWords.size ()) {', ' int which = extractedWords.size ();', ' for (String s : ones.navigableKeySet ()) {', ' if (which == k)', ' return s;', ' which++;', ' }', ' return null;', ' } else {', ' // If k is less than the size of the extractedWords list,', ' // return the kth most frequent word from extractedWords', ' return extractedWords.get (k).extractWord ();', ' }', ' }', '}', '/**', ' * A JUnit test case class.', ' * Every method starting with the word "test" will be called when running', ' * the test with JUnit.', ' */', 'public class CounterTester extends TestCase {', '', ' /**', ' * File object to read words from, with buffering.', ' */', ' private FileReader file = null;', ' private BufferedReader reader = null;', '', ' /**', ' * Close file readers.', ' */', ' private void closeFiles() {', ' try {', ' if (file != null) {', ' file.close();', ' file = null;', ' }', ' if (reader != null) {', ' reader.close();', ' reader = null;', ' }', ' } catch (Exception e) {', ' System.err.println("Problem closing file");', ' System.err.println(e);', ' e.printStackTrace();', ' }', ' }', '', ' /**', ' * Close files no matter what happens in the test.', ' */', ' protected void tearDown () {', ' closeFiles();', ' }', '', ' /**', ' * Open file and set up readers.', ' *', ' * @param fileName name of file to open', ' * */', ' private void openFile(String fileName) {', ' try {', ' file = new FileReader(fileName);', ' reader = new BufferedReader(file);', ' } catch (Exception e) {', ' System.err.format("Problem opening %s file\\n", fileName);', ' System.err.println(e);', ' e.printStackTrace();', ' fail();', ' }', ' }', '', ' /**', ' * Read the next numWords from the file.', ' *', ' * @param counter word counter to update', ' * @param numWords number of words to read.', ' */', ' private void readWords(WordCounter counter, int numWords) {', ' try {', ' for (int i=0; i<numWords; i++) {', ' if (i % 100000 == 0)', ' System.out.print (".");', ' String word = reader.readLine();', ' if (word == null) {', ' return;', ' }', " // Insert 'word' into the counter.", ' counter.insert(word);', ' }', ' } catch (Exception e) {', ' System.err.println("Problem reading file");', ' System.err.println(e);', ' e.printStackTrace();', ' fail();', ' }', ' }', '', ' /**', ' * Read the next numWords from the file, mixing with queries', ' *', ' * @param counter word counter to update', ' * @param numWords number of words to read.', ' */', ' private void readWordsMixed (WordCounter counter, int numWords) {', ' try {', ' int j = 0;', ' for (int i=0; i<numWords; i++) {', ' String word = reader.readLine();', " // If 'word' is null, it means we've reached the end of the file. So, return from the method.", ' if (word == null) {', ' return;', ' }', ' counter.insert(word);', '', ' // If the current iteration number is a multiple of 10 and greater than 100,000, perform a query of the kth most frequent word.', ' if (i % 10 == 0 && i > 100000) {', ' String myStr = counter.getKthMostFrequent(j++);', ' }', '', ' // rest j once we get to 100', ' if (j == 100)', ' j = 0;', ' }', ' } catch (Exception e) {', ' System.err.println("Problem reading file");', ' System.err.println(e);', ' e.printStackTrace();', ' fail();', ' }', ' }', '', ' /**', ' * Check that a sequence of words starts at the "start"th most', ' * frequent word.', ' *', ' * @param counter word counter to lookup', ' * @param start frequency index to start checking at', ' * @param expected array of expected words that start at that frequency', ' */', ' private void checkExpected(WordCounter counter, int start, String [] expected) {', ' for (int i = 0; i<expected.length; i++) {', ' String actual = counter.getKthMostFrequent(start);', ' System.out.format("k: %d, expected: %s, actual: %s\\n",', ' start, expected[i], actual);', ' assertEquals(expected[i], actual);', ' start++;', ' }', ' }', '', ' /**', ' * A test method.', ' * (Replace "X" with a name describing the test. You may write as', ' * many "testSomething" methods in this class as you wish, and each', ' * one will be called when running JUnit over this class.)', ' */', ' public void testSimple() {', ' System.out.println("\\nChecking insert");', ' WordCounter counter = new WordCounter();', ' counter.insert("pizzaz");', ' // Insert the word "pizza" into the counter twice', ' counter.insert("pizza");', ' counter.insert("pizza");', ' String [] expected = {"pizza"};', ' checkExpected(counter, 0, expected);', ' }', '', ' public void testTie() {', ' System.out.println("\\nChecking tie for 2nd place");', ' WordCounter counter = new WordCounter();', ' counter.insert("panache");', ' counter.insert("pizzaz");', ' counter.insert("pizza");', ' counter.insert("zebra");', ' counter.insert("pizza");', ' counter.insert("lion");', ' counter.insert("pizzaz");', ' counter.insert("panache");', ' counter.insert("panache");', ' counter.insert("camel");', ' // order is important here', ' String [] expected = {"pizza", "pizzaz"};', ' checkExpected(counter, 1, expected);', ' }', '', '', ' public void testPastTheEnd() {', ' System.out.println("\\nChecking past the end");', ' WordCounter counter = new WordCounter();', ' counter.insert("hi");', ' counter.insert("hello");', ' counter.insert("greetings");', ' counter.insert("salutations");', ' counter.insert("hi");', ' counter.insert("welcome");', ' counter.insert("goodbye");', ' counter.insert("later");', ' counter.insert("hello");', ' counter.insert("when");', ' counter.insert("hi");', ' assertNull(counter.getKthMostFrequent(8));', ' }', '', ' public void test100Top5() {', ' System.out.println("\\nChecking top 5 of 100 words");', ' WordCounter counter = new WordCounter();', ' // Open the file "allWordsBig"', ' openFile("allWordsBig");', ' // Read the next 100 words', ' readWords(counter, 100);', ' String [] expected = {"edu", "comp", "cs", "windows", "cmu"};', ' checkExpected(counter, 0, expected);', ' }', '', ' public void test300Top5() {', ' System.out.println("\\nChecking top 5 of first 100 words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter, 100);', ' String [] expected1 = {"edu", "comp", "cs", "windows", "cmu"};', ' checkExpected(counter, 0, expected1);', '', ' System.out.println("Adding 100 more words and rechecking top 5");', ' readWords(counter, 100);', ' String [] expected2 = {"edu", "cmu", "comp", "cs", "state"};', ' checkExpected(counter, 0, expected2);', '', ' System.out.println("Adding 100 more words and rechecking top 5");', ' readWords(counter, 100);', ' String [] expected3 = {"edu", "cmu", "comp", "ohio", "state"};', ' checkExpected(counter, 0, expected3);', ' }', '', ' public void test300Words14Thru19() {', ' System.out.println("\\nChecking rank 14 through 19 of 300 words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' // Read the next 300 words', ' readWords(counter, 300);', ' String [] expected = {"cantaloupe", "from", "ksu", "okstate", "on", "srv"};', ' checkExpected(counter, 14, expected);', ' }', '', ' public void test300CorrectNumber() {', ' System.out.println("\\nChecking correct number of unique words in 300 words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter, 300);', ' /* check the 122th, 123th, and 124th most frequent word in the corpus so far ', ' * and check if the result is not null.', ' */', ' assertNotNull(counter.getKthMostFrequent(122));', ' assertNotNull(counter.getKthMostFrequent(123));', ' assertNotNull(counter.getKthMostFrequent(124));', ' // check the 125th most frequent word in the corpus so far', ' // and check if the result is null.', ' assertNull(counter.getKthMostFrequent(125));', ' }', '', ' public void test300and100() {', ' System.out.println("\\nChecking top 5 of 100 and 300 words with two counters");', ' WordCounter counter1 = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter1, 300);', ' closeFiles();', '', ' WordCounter counter2 = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter2, 100);', '', ' String [] expected1 = {"edu", "cmu", "comp", "ohio", "state"};', ' checkExpected(counter1, 0, expected1);', '', ' String [] expected2 = {"edu", "comp", "cs", "windows", "cmu"};', ' checkExpected(counter2, 0, expected2);', ' }', '', ' public void testAllTop15() {', ' System.out.println("\\nChecking top 15 of all words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' // Read the next 6000000 words', ' readWords(counter, 6000000);', ' String [] expected = {"the", "edu", "to", "of", "and",', ' "in", "is", "ax", "that", "it",', ' "cmu", "for", "com", "you", "cs"};', ' checkExpected(counter, 0, expected);', ' }', '', ' public void testAllTop10000() {', ' System.out.println("\\nChecking time to get top 10000 of all words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter, 6000000);', '', ' for (int i=0; i<10000; i++) {', ' counter.getKthMostFrequent(i);', ' }', ' }', '', ' public void testSpeed() {', ' System.out.println("\\nMixing adding data with finding top k");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWordsMixed (counter, 6000000);', ' String [] expected = {"the", "edu", "to", "of", "and",', ' "in", "is", "ax", "that", "it",', ' "cmu", "for", "com", "you", "cs"};', ' checkExpected(counter, 0, expected);', ' }', '', '}']
[]
Variable 'w' used at line 79 is defined at line 72 and has a Short-Range dependency. Global_Variable 'myCount' used at line 79 is defined at line 14 and has a Long-Range dependency.
{}
{'Variable Short-Range': 1, 'Global_Variable Long-Range': 1}
infilling_java
CounterTester
107
111
['import junit.framework.TestCase;', 'import java.io.*;', 'import java.util.HashMap;', 'import java.util.TreeMap;', 'import java.util.ArrayList;', '', '/**', ' * This silly little class wraps String, int pairs so they can be sorted.', ' * Used by the WordCounter class.', ' */', 'class WordWrapper implements Comparable <WordWrapper> {', '', ' private String myWord;', ' private int myCount;', ' private int mySlot;', '', ' /**', ' * Creates a new WordWrapper with string "XXX" and count zero.', ' */', ' public WordWrapper (String myWordIn, int myCountIn, int mySlotIn) {', ' myWord = myWordIn;', ' myCount = myCountIn;', ' mySlot = mySlotIn;', ' }', ' /**', ' * Returns a copy of the string in the WordWrapper.', ' *', ' * @return The word contained in this object. Is copied out: no aliasing.', ' */', ' public String extractWord () {', ' return new String (myWord);', ' }', '', ' /**', ' * Returns the count in this WordWrapper.', ' *', ' * @return The count contained in the object.', ' */', ' public int extractCount () {', ' return myCount;', ' }', '', ' public void incCount () {', ' myCount++;', ' }', '', ' public int getCount () {', ' return myCount;', ' }', '', ' public int getPos () {', ' return mySlot;', ' }', '', ' public void setPos (int toWhat) {', ' ', ' mySlot = toWhat;', ' }', '', ' public String toString () {', ' return myWord + " " + mySlot;', ' }', '', ' /**', ' * Comparison operator so we implement the Comparable interface.', ' *', ' * @param w The word wrapper to compare to', ' * @return a positive, zero, or negative value depending upon whether this', ' * word wrapper is less then, equal to, or greater than param w. The', ' * relationship is determied by looking at the counts.', ' */', ' public int compareTo (WordWrapper w) {', " // Check if the count of the current word (myCount) is equal to the count of the word 'w'", ' // If the counts are equal, compare the words lexicographically', ' if (w.myCount == myCount) {', ' return myWord.compareTo(w.myWord);', ' }', " // If the counts are not equal, subtract the count of the current word from the count of the word 'w'", ' return w.myCount - myCount;', ' }', '}', '', '', '/**', ' * This class is used to count occurences of words (strings) in a corpus. Used by', ' * the IndexedDocumentCollection class to find the most frequent words in the corpus.', ' */', '', 'class WordCounter {', ' ', " // this is the map that holds (for each word) the number of times we've seen it", ' // note that Integer is a wrapper for the buitin Java int type', ' private HashMap <String, WordWrapper> wordsIHaveSeen = new HashMap <String, WordWrapper> ();', ' private TreeMap <String, String> ones = new TreeMap <String, String> ();', ' ', ' // the set of words that have been extracted', ' private ArrayList<WordWrapper> extractedWords = new ArrayList <WordWrapper> ();', '', ' /**', ' * Accepts a word and increments the count of the number of times we have seen it.', ' * Note that a deep copy of the param is done (no aliasing).', ' *', ' * @param addMe The string to count', ' */', ' public void insert (String addMe) {', ' if (ones.containsKey (addMe)) {']
[' ones.remove (addMe);', ' WordWrapper temp = new WordWrapper (addMe, 1, extractedWords.size ());', ' wordsIHaveSeen.put(addMe, temp);', ' extractedWords.add (temp);', ' }']
['', ' // first check to see if the word is alredy in wordsIHaveSeen', ' if (wordsIHaveSeen.containsKey (addMe)) {', ' // if it is, then remove and increment its count', ' WordWrapper temp = wordsIHaveSeen.get (addMe);', ' temp.incCount ();', '', ' // find the slot that we go to in the extractedWords list', " // by sorting the 'extractedWords' list in ascending order", ' for (int i = temp.getPos () - 1; i >= 0 && ', ' extractedWords.get (i).compareTo (extractedWords.get (i + 1)) > 0; i--) {', ' temp = extractedWords.get (i + 1);', ' temp.setPos (i);', ' ', ' WordWrapper temp2 = extractedWords.get (i);', ' temp2.setPos (i + 1);', ' ', ' extractedWords.set (i + 1, temp2);', ' extractedWords.set (i, temp);', ' }', '', ' // in this case it is not there, so just add it', ' } else {', ' ones.put (addMe, addMe);', ' }', ' }', '', ' /**', ' * Returns the kth most frequent word in the corpus so far. A deep copy is returned,', ' * so no aliasing is possible. Returns null if there are not enough words.', ' *', ' * @param k Note that the most frequent word is at k = 0', ' * @return The kth most frequent word in the corpus to date', ' */', ' public String getKthMostFrequent (int k) {', '', ' // if we got here, then the array is all set up, so just return the kth most frequent word', ' if (k >= extractedWords.size ()) {', ' int which = extractedWords.size ();', ' for (String s : ones.navigableKeySet ()) {', ' if (which == k)', ' return s;', ' which++;', ' }', ' return null;', ' } else {', ' // If k is less than the size of the extractedWords list,', ' // return the kth most frequent word from extractedWords', ' return extractedWords.get (k).extractWord ();', ' }', ' }', '}', '/**', ' * A JUnit test case class.', ' * Every method starting with the word "test" will be called when running', ' * the test with JUnit.', ' */', 'public class CounterTester extends TestCase {', '', ' /**', ' * File object to read words from, with buffering.', ' */', ' private FileReader file = null;', ' private BufferedReader reader = null;', '', ' /**', ' * Close file readers.', ' */', ' private void closeFiles() {', ' try {', ' if (file != null) {', ' file.close();', ' file = null;', ' }', ' if (reader != null) {', ' reader.close();', ' reader = null;', ' }', ' } catch (Exception e) {', ' System.err.println("Problem closing file");', ' System.err.println(e);', ' e.printStackTrace();', ' }', ' }', '', ' /**', ' * Close files no matter what happens in the test.', ' */', ' protected void tearDown () {', ' closeFiles();', ' }', '', ' /**', ' * Open file and set up readers.', ' *', ' * @param fileName name of file to open', ' * */', ' private void openFile(String fileName) {', ' try {', ' file = new FileReader(fileName);', ' reader = new BufferedReader(file);', ' } catch (Exception e) {', ' System.err.format("Problem opening %s file\\n", fileName);', ' System.err.println(e);', ' e.printStackTrace();', ' fail();', ' }', ' }', '', ' /**', ' * Read the next numWords from the file.', ' *', ' * @param counter word counter to update', ' * @param numWords number of words to read.', ' */', ' private void readWords(WordCounter counter, int numWords) {', ' try {', ' for (int i=0; i<numWords; i++) {', ' if (i % 100000 == 0)', ' System.out.print (".");', ' String word = reader.readLine();', ' if (word == null) {', ' return;', ' }', " // Insert 'word' into the counter.", ' counter.insert(word);', ' }', ' } catch (Exception e) {', ' System.err.println("Problem reading file");', ' System.err.println(e);', ' e.printStackTrace();', ' fail();', ' }', ' }', '', ' /**', ' * Read the next numWords from the file, mixing with queries', ' *', ' * @param counter word counter to update', ' * @param numWords number of words to read.', ' */', ' private void readWordsMixed (WordCounter counter, int numWords) {', ' try {', ' int j = 0;', ' for (int i=0; i<numWords; i++) {', ' String word = reader.readLine();', " // If 'word' is null, it means we've reached the end of the file. So, return from the method.", ' if (word == null) {', ' return;', ' }', ' counter.insert(word);', '', ' // If the current iteration number is a multiple of 10 and greater than 100,000, perform a query of the kth most frequent word.', ' if (i % 10 == 0 && i > 100000) {', ' String myStr = counter.getKthMostFrequent(j++);', ' }', '', ' // rest j once we get to 100', ' if (j == 100)', ' j = 0;', ' }', ' } catch (Exception e) {', ' System.err.println("Problem reading file");', ' System.err.println(e);', ' e.printStackTrace();', ' fail();', ' }', ' }', '', ' /**', ' * Check that a sequence of words starts at the "start"th most', ' * frequent word.', ' *', ' * @param counter word counter to lookup', ' * @param start frequency index to start checking at', ' * @param expected array of expected words that start at that frequency', ' */', ' private void checkExpected(WordCounter counter, int start, String [] expected) {', ' for (int i = 0; i<expected.length; i++) {', ' String actual = counter.getKthMostFrequent(start);', ' System.out.format("k: %d, expected: %s, actual: %s\\n",', ' start, expected[i], actual);', ' assertEquals(expected[i], actual);', ' start++;', ' }', ' }', '', ' /**', ' * A test method.', ' * (Replace "X" with a name describing the test. You may write as', ' * many "testSomething" methods in this class as you wish, and each', ' * one will be called when running JUnit over this class.)', ' */', ' public void testSimple() {', ' System.out.println("\\nChecking insert");', ' WordCounter counter = new WordCounter();', ' counter.insert("pizzaz");', ' // Insert the word "pizza" into the counter twice', ' counter.insert("pizza");', ' counter.insert("pizza");', ' String [] expected = {"pizza"};', ' checkExpected(counter, 0, expected);', ' }', '', ' public void testTie() {', ' System.out.println("\\nChecking tie for 2nd place");', ' WordCounter counter = new WordCounter();', ' counter.insert("panache");', ' counter.insert("pizzaz");', ' counter.insert("pizza");', ' counter.insert("zebra");', ' counter.insert("pizza");', ' counter.insert("lion");', ' counter.insert("pizzaz");', ' counter.insert("panache");', ' counter.insert("panache");', ' counter.insert("camel");', ' // order is important here', ' String [] expected = {"pizza", "pizzaz"};', ' checkExpected(counter, 1, expected);', ' }', '', '', ' public void testPastTheEnd() {', ' System.out.println("\\nChecking past the end");', ' WordCounter counter = new WordCounter();', ' counter.insert("hi");', ' counter.insert("hello");', ' counter.insert("greetings");', ' counter.insert("salutations");', ' counter.insert("hi");', ' counter.insert("welcome");', ' counter.insert("goodbye");', ' counter.insert("later");', ' counter.insert("hello");', ' counter.insert("when");', ' counter.insert("hi");', ' assertNull(counter.getKthMostFrequent(8));', ' }', '', ' public void test100Top5() {', ' System.out.println("\\nChecking top 5 of 100 words");', ' WordCounter counter = new WordCounter();', ' // Open the file "allWordsBig"', ' openFile("allWordsBig");', ' // Read the next 100 words', ' readWords(counter, 100);', ' String [] expected = {"edu", "comp", "cs", "windows", "cmu"};', ' checkExpected(counter, 0, expected);', ' }', '', ' public void test300Top5() {', ' System.out.println("\\nChecking top 5 of first 100 words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter, 100);', ' String [] expected1 = {"edu", "comp", "cs", "windows", "cmu"};', ' checkExpected(counter, 0, expected1);', '', ' System.out.println("Adding 100 more words and rechecking top 5");', ' readWords(counter, 100);', ' String [] expected2 = {"edu", "cmu", "comp", "cs", "state"};', ' checkExpected(counter, 0, expected2);', '', ' System.out.println("Adding 100 more words and rechecking top 5");', ' readWords(counter, 100);', ' String [] expected3 = {"edu", "cmu", "comp", "ohio", "state"};', ' checkExpected(counter, 0, expected3);', ' }', '', ' public void test300Words14Thru19() {', ' System.out.println("\\nChecking rank 14 through 19 of 300 words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' // Read the next 300 words', ' readWords(counter, 300);', ' String [] expected = {"cantaloupe", "from", "ksu", "okstate", "on", "srv"};', ' checkExpected(counter, 14, expected);', ' }', '', ' public void test300CorrectNumber() {', ' System.out.println("\\nChecking correct number of unique words in 300 words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter, 300);', ' /* check the 122th, 123th, and 124th most frequent word in the corpus so far ', ' * and check if the result is not null.', ' */', ' assertNotNull(counter.getKthMostFrequent(122));', ' assertNotNull(counter.getKthMostFrequent(123));', ' assertNotNull(counter.getKthMostFrequent(124));', ' // check the 125th most frequent word in the corpus so far', ' // and check if the result is null.', ' assertNull(counter.getKthMostFrequent(125));', ' }', '', ' public void test300and100() {', ' System.out.println("\\nChecking top 5 of 100 and 300 words with two counters");', ' WordCounter counter1 = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter1, 300);', ' closeFiles();', '', ' WordCounter counter2 = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter2, 100);', '', ' String [] expected1 = {"edu", "cmu", "comp", "ohio", "state"};', ' checkExpected(counter1, 0, expected1);', '', ' String [] expected2 = {"edu", "comp", "cs", "windows", "cmu"};', ' checkExpected(counter2, 0, expected2);', ' }', '', ' public void testAllTop15() {', ' System.out.println("\\nChecking top 15 of all words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' // Read the next 6000000 words', ' readWords(counter, 6000000);', ' String [] expected = {"the", "edu", "to", "of", "and",', ' "in", "is", "ax", "that", "it",', ' "cmu", "for", "com", "you", "cs"};', ' checkExpected(counter, 0, expected);', ' }', '', ' public void testAllTop10000() {', ' System.out.println("\\nChecking time to get top 10000 of all words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter, 6000000);', '', ' for (int i=0; i<10000; i++) {', ' counter.getKthMostFrequent(i);', ' }', ' }', '', ' public void testSpeed() {', ' System.out.println("\\nMixing adding data with finding top k");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWordsMixed (counter, 6000000);', ' String [] expected = {"the", "edu", "to", "of", "and",', ' "in", "is", "ax", "that", "it",', ' "cmu", "for", "com", "you", "cs"};', ' checkExpected(counter, 0, expected);', ' }', '', '}']
[{'reason_category': 'If Body', 'usage_line': 107}, {'reason_category': 'If Body', 'usage_line': 108}, {'reason_category': 'If Body', 'usage_line': 109}, {'reason_category': 'If Body', 'usage_line': 110}, {'reason_category': 'If Body', 'usage_line': 111}]
Global_Variable 'ones' used at line 107 is defined at line 94 and has a Medium-Range dependency. Variable 'addMe' used at line 107 is defined at line 105 and has a Short-Range dependency. Class 'WordWrapper' used at line 108 is defined at line 11 and has a Long-Range dependency. Variable 'addMe' used at line 108 is defined at line 105 and has a Short-Range dependency. Global_Variable 'extractedWords' used at line 108 is defined at line 97 and has a Medium-Range dependency. Global_Variable 'wordsIHaveSeen' used at line 109 is defined at line 93 and has a Medium-Range dependency. Variable 'addMe' used at line 109 is defined at line 105 and has a Short-Range dependency. Variable 'temp' used at line 109 is defined at line 108 and has a Short-Range dependency. Global_Variable 'extractedWords' used at line 110 is defined at line 97 and has a Medium-Range dependency. Variable 'temp' used at line 110 is defined at line 108 and has a Short-Range dependency.
{'If Body': 5}
{'Global_Variable Medium-Range': 4, 'Variable Short-Range': 5, 'Class Long-Range': 1}
infilling_java
CounterTester
116
117
['import junit.framework.TestCase;', 'import java.io.*;', 'import java.util.HashMap;', 'import java.util.TreeMap;', 'import java.util.ArrayList;', '', '/**', ' * This silly little class wraps String, int pairs so they can be sorted.', ' * Used by the WordCounter class.', ' */', 'class WordWrapper implements Comparable <WordWrapper> {', '', ' private String myWord;', ' private int myCount;', ' private int mySlot;', '', ' /**', ' * Creates a new WordWrapper with string "XXX" and count zero.', ' */', ' public WordWrapper (String myWordIn, int myCountIn, int mySlotIn) {', ' myWord = myWordIn;', ' myCount = myCountIn;', ' mySlot = mySlotIn;', ' }', ' /**', ' * Returns a copy of the string in the WordWrapper.', ' *', ' * @return The word contained in this object. Is copied out: no aliasing.', ' */', ' public String extractWord () {', ' return new String (myWord);', ' }', '', ' /**', ' * Returns the count in this WordWrapper.', ' *', ' * @return The count contained in the object.', ' */', ' public int extractCount () {', ' return myCount;', ' }', '', ' public void incCount () {', ' myCount++;', ' }', '', ' public int getCount () {', ' return myCount;', ' }', '', ' public int getPos () {', ' return mySlot;', ' }', '', ' public void setPos (int toWhat) {', ' ', ' mySlot = toWhat;', ' }', '', ' public String toString () {', ' return myWord + " " + mySlot;', ' }', '', ' /**', ' * Comparison operator so we implement the Comparable interface.', ' *', ' * @param w The word wrapper to compare to', ' * @return a positive, zero, or negative value depending upon whether this', ' * word wrapper is less then, equal to, or greater than param w. The', ' * relationship is determied by looking at the counts.', ' */', ' public int compareTo (WordWrapper w) {', " // Check if the count of the current word (myCount) is equal to the count of the word 'w'", ' // If the counts are equal, compare the words lexicographically', ' if (w.myCount == myCount) {', ' return myWord.compareTo(w.myWord);', ' }', " // If the counts are not equal, subtract the count of the current word from the count of the word 'w'", ' return w.myCount - myCount;', ' }', '}', '', '', '/**', ' * This class is used to count occurences of words (strings) in a corpus. Used by', ' * the IndexedDocumentCollection class to find the most frequent words in the corpus.', ' */', '', 'class WordCounter {', ' ', " // this is the map that holds (for each word) the number of times we've seen it", ' // note that Integer is a wrapper for the buitin Java int type', ' private HashMap <String, WordWrapper> wordsIHaveSeen = new HashMap <String, WordWrapper> ();', ' private TreeMap <String, String> ones = new TreeMap <String, String> ();', ' ', ' // the set of words that have been extracted', ' private ArrayList<WordWrapper> extractedWords = new ArrayList <WordWrapper> ();', '', ' /**', ' * Accepts a word and increments the count of the number of times we have seen it.', ' * Note that a deep copy of the param is done (no aliasing).', ' *', ' * @param addMe The string to count', ' */', ' public void insert (String addMe) {', ' if (ones.containsKey (addMe)) {', ' ones.remove (addMe);', ' WordWrapper temp = new WordWrapper (addMe, 1, extractedWords.size ());', ' wordsIHaveSeen.put(addMe, temp);', ' extractedWords.add (temp);', ' }', '', ' // first check to see if the word is alredy in wordsIHaveSeen', ' if (wordsIHaveSeen.containsKey (addMe)) {', ' // if it is, then remove and increment its count']
[' WordWrapper temp = wordsIHaveSeen.get (addMe);', ' temp.incCount ();']
['', ' // find the slot that we go to in the extractedWords list', " // by sorting the 'extractedWords' list in ascending order", ' for (int i = temp.getPos () - 1; i >= 0 && ', ' extractedWords.get (i).compareTo (extractedWords.get (i + 1)) > 0; i--) {', ' temp = extractedWords.get (i + 1);', ' temp.setPos (i);', ' ', ' WordWrapper temp2 = extractedWords.get (i);', ' temp2.setPos (i + 1);', ' ', ' extractedWords.set (i + 1, temp2);', ' extractedWords.set (i, temp);', ' }', '', ' // in this case it is not there, so just add it', ' } else {', ' ones.put (addMe, addMe);', ' }', ' }', '', ' /**', ' * Returns the kth most frequent word in the corpus so far. A deep copy is returned,', ' * so no aliasing is possible. Returns null if there are not enough words.', ' *', ' * @param k Note that the most frequent word is at k = 0', ' * @return The kth most frequent word in the corpus to date', ' */', ' public String getKthMostFrequent (int k) {', '', ' // if we got here, then the array is all set up, so just return the kth most frequent word', ' if (k >= extractedWords.size ()) {', ' int which = extractedWords.size ();', ' for (String s : ones.navigableKeySet ()) {', ' if (which == k)', ' return s;', ' which++;', ' }', ' return null;', ' } else {', ' // If k is less than the size of the extractedWords list,', ' // return the kth most frequent word from extractedWords', ' return extractedWords.get (k).extractWord ();', ' }', ' }', '}', '/**', ' * A JUnit test case class.', ' * Every method starting with the word "test" will be called when running', ' * the test with JUnit.', ' */', 'public class CounterTester extends TestCase {', '', ' /**', ' * File object to read words from, with buffering.', ' */', ' private FileReader file = null;', ' private BufferedReader reader = null;', '', ' /**', ' * Close file readers.', ' */', ' private void closeFiles() {', ' try {', ' if (file != null) {', ' file.close();', ' file = null;', ' }', ' if (reader != null) {', ' reader.close();', ' reader = null;', ' }', ' } catch (Exception e) {', ' System.err.println("Problem closing file");', ' System.err.println(e);', ' e.printStackTrace();', ' }', ' }', '', ' /**', ' * Close files no matter what happens in the test.', ' */', ' protected void tearDown () {', ' closeFiles();', ' }', '', ' /**', ' * Open file and set up readers.', ' *', ' * @param fileName name of file to open', ' * */', ' private void openFile(String fileName) {', ' try {', ' file = new FileReader(fileName);', ' reader = new BufferedReader(file);', ' } catch (Exception e) {', ' System.err.format("Problem opening %s file\\n", fileName);', ' System.err.println(e);', ' e.printStackTrace();', ' fail();', ' }', ' }', '', ' /**', ' * Read the next numWords from the file.', ' *', ' * @param counter word counter to update', ' * @param numWords number of words to read.', ' */', ' private void readWords(WordCounter counter, int numWords) {', ' try {', ' for (int i=0; i<numWords; i++) {', ' if (i % 100000 == 0)', ' System.out.print (".");', ' String word = reader.readLine();', ' if (word == null) {', ' return;', ' }', " // Insert 'word' into the counter.", ' counter.insert(word);', ' }', ' } catch (Exception e) {', ' System.err.println("Problem reading file");', ' System.err.println(e);', ' e.printStackTrace();', ' fail();', ' }', ' }', '', ' /**', ' * Read the next numWords from the file, mixing with queries', ' *', ' * @param counter word counter to update', ' * @param numWords number of words to read.', ' */', ' private void readWordsMixed (WordCounter counter, int numWords) {', ' try {', ' int j = 0;', ' for (int i=0; i<numWords; i++) {', ' String word = reader.readLine();', " // If 'word' is null, it means we've reached the end of the file. So, return from the method.", ' if (word == null) {', ' return;', ' }', ' counter.insert(word);', '', ' // If the current iteration number is a multiple of 10 and greater than 100,000, perform a query of the kth most frequent word.', ' if (i % 10 == 0 && i > 100000) {', ' String myStr = counter.getKthMostFrequent(j++);', ' }', '', ' // rest j once we get to 100', ' if (j == 100)', ' j = 0;', ' }', ' } catch (Exception e) {', ' System.err.println("Problem reading file");', ' System.err.println(e);', ' e.printStackTrace();', ' fail();', ' }', ' }', '', ' /**', ' * Check that a sequence of words starts at the "start"th most', ' * frequent word.', ' *', ' * @param counter word counter to lookup', ' * @param start frequency index to start checking at', ' * @param expected array of expected words that start at that frequency', ' */', ' private void checkExpected(WordCounter counter, int start, String [] expected) {', ' for (int i = 0; i<expected.length; i++) {', ' String actual = counter.getKthMostFrequent(start);', ' System.out.format("k: %d, expected: %s, actual: %s\\n",', ' start, expected[i], actual);', ' assertEquals(expected[i], actual);', ' start++;', ' }', ' }', '', ' /**', ' * A test method.', ' * (Replace "X" with a name describing the test. You may write as', ' * many "testSomething" methods in this class as you wish, and each', ' * one will be called when running JUnit over this class.)', ' */', ' public void testSimple() {', ' System.out.println("\\nChecking insert");', ' WordCounter counter = new WordCounter();', ' counter.insert("pizzaz");', ' // Insert the word "pizza" into the counter twice', ' counter.insert("pizza");', ' counter.insert("pizza");', ' String [] expected = {"pizza"};', ' checkExpected(counter, 0, expected);', ' }', '', ' public void testTie() {', ' System.out.println("\\nChecking tie for 2nd place");', ' WordCounter counter = new WordCounter();', ' counter.insert("panache");', ' counter.insert("pizzaz");', ' counter.insert("pizza");', ' counter.insert("zebra");', ' counter.insert("pizza");', ' counter.insert("lion");', ' counter.insert("pizzaz");', ' counter.insert("panache");', ' counter.insert("panache");', ' counter.insert("camel");', ' // order is important here', ' String [] expected = {"pizza", "pizzaz"};', ' checkExpected(counter, 1, expected);', ' }', '', '', ' public void testPastTheEnd() {', ' System.out.println("\\nChecking past the end");', ' WordCounter counter = new WordCounter();', ' counter.insert("hi");', ' counter.insert("hello");', ' counter.insert("greetings");', ' counter.insert("salutations");', ' counter.insert("hi");', ' counter.insert("welcome");', ' counter.insert("goodbye");', ' counter.insert("later");', ' counter.insert("hello");', ' counter.insert("when");', ' counter.insert("hi");', ' assertNull(counter.getKthMostFrequent(8));', ' }', '', ' public void test100Top5() {', ' System.out.println("\\nChecking top 5 of 100 words");', ' WordCounter counter = new WordCounter();', ' // Open the file "allWordsBig"', ' openFile("allWordsBig");', ' // Read the next 100 words', ' readWords(counter, 100);', ' String [] expected = {"edu", "comp", "cs", "windows", "cmu"};', ' checkExpected(counter, 0, expected);', ' }', '', ' public void test300Top5() {', ' System.out.println("\\nChecking top 5 of first 100 words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter, 100);', ' String [] expected1 = {"edu", "comp", "cs", "windows", "cmu"};', ' checkExpected(counter, 0, expected1);', '', ' System.out.println("Adding 100 more words and rechecking top 5");', ' readWords(counter, 100);', ' String [] expected2 = {"edu", "cmu", "comp", "cs", "state"};', ' checkExpected(counter, 0, expected2);', '', ' System.out.println("Adding 100 more words and rechecking top 5");', ' readWords(counter, 100);', ' String [] expected3 = {"edu", "cmu", "comp", "ohio", "state"};', ' checkExpected(counter, 0, expected3);', ' }', '', ' public void test300Words14Thru19() {', ' System.out.println("\\nChecking rank 14 through 19 of 300 words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' // Read the next 300 words', ' readWords(counter, 300);', ' String [] expected = {"cantaloupe", "from", "ksu", "okstate", "on", "srv"};', ' checkExpected(counter, 14, expected);', ' }', '', ' public void test300CorrectNumber() {', ' System.out.println("\\nChecking correct number of unique words in 300 words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter, 300);', ' /* check the 122th, 123th, and 124th most frequent word in the corpus so far ', ' * and check if the result is not null.', ' */', ' assertNotNull(counter.getKthMostFrequent(122));', ' assertNotNull(counter.getKthMostFrequent(123));', ' assertNotNull(counter.getKthMostFrequent(124));', ' // check the 125th most frequent word in the corpus so far', ' // and check if the result is null.', ' assertNull(counter.getKthMostFrequent(125));', ' }', '', ' public void test300and100() {', ' System.out.println("\\nChecking top 5 of 100 and 300 words with two counters");', ' WordCounter counter1 = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter1, 300);', ' closeFiles();', '', ' WordCounter counter2 = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter2, 100);', '', ' String [] expected1 = {"edu", "cmu", "comp", "ohio", "state"};', ' checkExpected(counter1, 0, expected1);', '', ' String [] expected2 = {"edu", "comp", "cs", "windows", "cmu"};', ' checkExpected(counter2, 0, expected2);', ' }', '', ' public void testAllTop15() {', ' System.out.println("\\nChecking top 15 of all words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' // Read the next 6000000 words', ' readWords(counter, 6000000);', ' String [] expected = {"the", "edu", "to", "of", "and",', ' "in", "is", "ax", "that", "it",', ' "cmu", "for", "com", "you", "cs"};', ' checkExpected(counter, 0, expected);', ' }', '', ' public void testAllTop10000() {', ' System.out.println("\\nChecking time to get top 10000 of all words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter, 6000000);', '', ' for (int i=0; i<10000; i++) {', ' counter.getKthMostFrequent(i);', ' }', ' }', '', ' public void testSpeed() {', ' System.out.println("\\nMixing adding data with finding top k");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWordsMixed (counter, 6000000);', ' String [] expected = {"the", "edu", "to", "of", "and",', ' "in", "is", "ax", "that", "it",', ' "cmu", "for", "com", "you", "cs"};', ' checkExpected(counter, 0, expected);', ' }', '', '}']
[{'reason_category': 'If Body', 'usage_line': 116}, {'reason_category': 'If Body', 'usage_line': 117}]
Class 'WordWrapper' used at line 116 is defined at line 11 and has a Long-Range dependency. Global_Variable 'wordsIHaveSeen' used at line 116 is defined at line 93 and has a Medium-Range dependency. Variable 'addMe' used at line 116 is defined at line 105 and has a Medium-Range dependency. Variable 'temp' used at line 117 is defined at line 116 and has a Short-Range dependency.
{'If Body': 2}
{'Class Long-Range': 1, 'Global_Variable Medium-Range': 1, 'Variable Medium-Range': 1, 'Variable Short-Range': 1}
infilling_java
CounterTester
153
153
['import junit.framework.TestCase;', 'import java.io.*;', 'import java.util.HashMap;', 'import java.util.TreeMap;', 'import java.util.ArrayList;', '', '/**', ' * This silly little class wraps String, int pairs so they can be sorted.', ' * Used by the WordCounter class.', ' */', 'class WordWrapper implements Comparable <WordWrapper> {', '', ' private String myWord;', ' private int myCount;', ' private int mySlot;', '', ' /**', ' * Creates a new WordWrapper with string "XXX" and count zero.', ' */', ' public WordWrapper (String myWordIn, int myCountIn, int mySlotIn) {', ' myWord = myWordIn;', ' myCount = myCountIn;', ' mySlot = mySlotIn;', ' }', ' /**', ' * Returns a copy of the string in the WordWrapper.', ' *', ' * @return The word contained in this object. Is copied out: no aliasing.', ' */', ' public String extractWord () {', ' return new String (myWord);', ' }', '', ' /**', ' * Returns the count in this WordWrapper.', ' *', ' * @return The count contained in the object.', ' */', ' public int extractCount () {', ' return myCount;', ' }', '', ' public void incCount () {', ' myCount++;', ' }', '', ' public int getCount () {', ' return myCount;', ' }', '', ' public int getPos () {', ' return mySlot;', ' }', '', ' public void setPos (int toWhat) {', ' ', ' mySlot = toWhat;', ' }', '', ' public String toString () {', ' return myWord + " " + mySlot;', ' }', '', ' /**', ' * Comparison operator so we implement the Comparable interface.', ' *', ' * @param w The word wrapper to compare to', ' * @return a positive, zero, or negative value depending upon whether this', ' * word wrapper is less then, equal to, or greater than param w. The', ' * relationship is determied by looking at the counts.', ' */', ' public int compareTo (WordWrapper w) {', " // Check if the count of the current word (myCount) is equal to the count of the word 'w'", ' // If the counts are equal, compare the words lexicographically', ' if (w.myCount == myCount) {', ' return myWord.compareTo(w.myWord);', ' }', " // If the counts are not equal, subtract the count of the current word from the count of the word 'w'", ' return w.myCount - myCount;', ' }', '}', '', '', '/**', ' * This class is used to count occurences of words (strings) in a corpus. Used by', ' * the IndexedDocumentCollection class to find the most frequent words in the corpus.', ' */', '', 'class WordCounter {', ' ', " // this is the map that holds (for each word) the number of times we've seen it", ' // note that Integer is a wrapper for the buitin Java int type', ' private HashMap <String, WordWrapper> wordsIHaveSeen = new HashMap <String, WordWrapper> ();', ' private TreeMap <String, String> ones = new TreeMap <String, String> ();', ' ', ' // the set of words that have been extracted', ' private ArrayList<WordWrapper> extractedWords = new ArrayList <WordWrapper> ();', '', ' /**', ' * Accepts a word and increments the count of the number of times we have seen it.', ' * Note that a deep copy of the param is done (no aliasing).', ' *', ' * @param addMe The string to count', ' */', ' public void insert (String addMe) {', ' if (ones.containsKey (addMe)) {', ' ones.remove (addMe);', ' WordWrapper temp = new WordWrapper (addMe, 1, extractedWords.size ());', ' wordsIHaveSeen.put(addMe, temp);', ' extractedWords.add (temp);', ' }', '', ' // first check to see if the word is alredy in wordsIHaveSeen', ' if (wordsIHaveSeen.containsKey (addMe)) {', ' // if it is, then remove and increment its count', ' WordWrapper temp = wordsIHaveSeen.get (addMe);', ' temp.incCount ();', '', ' // find the slot that we go to in the extractedWords list', " // by sorting the 'extractedWords' list in ascending order", ' for (int i = temp.getPos () - 1; i >= 0 && ', ' extractedWords.get (i).compareTo (extractedWords.get (i + 1)) > 0; i--) {', ' temp = extractedWords.get (i + 1);', ' temp.setPos (i);', ' ', ' WordWrapper temp2 = extractedWords.get (i);', ' temp2.setPos (i + 1);', ' ', ' extractedWords.set (i + 1, temp2);', ' extractedWords.set (i, temp);', ' }', '', ' // in this case it is not there, so just add it', ' } else {', ' ones.put (addMe, addMe);', ' }', ' }', '', ' /**', ' * Returns the kth most frequent word in the corpus so far. A deep copy is returned,', ' * so no aliasing is possible. Returns null if there are not enough words.', ' *', ' * @param k Note that the most frequent word is at k = 0', ' * @return The kth most frequent word in the corpus to date', ' */', ' public String getKthMostFrequent (int k) {', '', ' // if we got here, then the array is all set up, so just return the kth most frequent word', ' if (k >= extractedWords.size ()) {', ' int which = extractedWords.size ();', ' for (String s : ones.navigableKeySet ()) {', ' if (which == k)']
[' return s;']
[' which++;', ' }', ' return null;', ' } else {', ' // If k is less than the size of the extractedWords list,', ' // return the kth most frequent word from extractedWords', ' return extractedWords.get (k).extractWord ();', ' }', ' }', '}', '/**', ' * A JUnit test case class.', ' * Every method starting with the word "test" will be called when running', ' * the test with JUnit.', ' */', 'public class CounterTester extends TestCase {', '', ' /**', ' * File object to read words from, with buffering.', ' */', ' private FileReader file = null;', ' private BufferedReader reader = null;', '', ' /**', ' * Close file readers.', ' */', ' private void closeFiles() {', ' try {', ' if (file != null) {', ' file.close();', ' file = null;', ' }', ' if (reader != null) {', ' reader.close();', ' reader = null;', ' }', ' } catch (Exception e) {', ' System.err.println("Problem closing file");', ' System.err.println(e);', ' e.printStackTrace();', ' }', ' }', '', ' /**', ' * Close files no matter what happens in the test.', ' */', ' protected void tearDown () {', ' closeFiles();', ' }', '', ' /**', ' * Open file and set up readers.', ' *', ' * @param fileName name of file to open', ' * */', ' private void openFile(String fileName) {', ' try {', ' file = new FileReader(fileName);', ' reader = new BufferedReader(file);', ' } catch (Exception e) {', ' System.err.format("Problem opening %s file\\n", fileName);', ' System.err.println(e);', ' e.printStackTrace();', ' fail();', ' }', ' }', '', ' /**', ' * Read the next numWords from the file.', ' *', ' * @param counter word counter to update', ' * @param numWords number of words to read.', ' */', ' private void readWords(WordCounter counter, int numWords) {', ' try {', ' for (int i=0; i<numWords; i++) {', ' if (i % 100000 == 0)', ' System.out.print (".");', ' String word = reader.readLine();', ' if (word == null) {', ' return;', ' }', " // Insert 'word' into the counter.", ' counter.insert(word);', ' }', ' } catch (Exception e) {', ' System.err.println("Problem reading file");', ' System.err.println(e);', ' e.printStackTrace();', ' fail();', ' }', ' }', '', ' /**', ' * Read the next numWords from the file, mixing with queries', ' *', ' * @param counter word counter to update', ' * @param numWords number of words to read.', ' */', ' private void readWordsMixed (WordCounter counter, int numWords) {', ' try {', ' int j = 0;', ' for (int i=0; i<numWords; i++) {', ' String word = reader.readLine();', " // If 'word' is null, it means we've reached the end of the file. So, return from the method.", ' if (word == null) {', ' return;', ' }', ' counter.insert(word);', '', ' // If the current iteration number is a multiple of 10 and greater than 100,000, perform a query of the kth most frequent word.', ' if (i % 10 == 0 && i > 100000) {', ' String myStr = counter.getKthMostFrequent(j++);', ' }', '', ' // rest j once we get to 100', ' if (j == 100)', ' j = 0;', ' }', ' } catch (Exception e) {', ' System.err.println("Problem reading file");', ' System.err.println(e);', ' e.printStackTrace();', ' fail();', ' }', ' }', '', ' /**', ' * Check that a sequence of words starts at the "start"th most', ' * frequent word.', ' *', ' * @param counter word counter to lookup', ' * @param start frequency index to start checking at', ' * @param expected array of expected words that start at that frequency', ' */', ' private void checkExpected(WordCounter counter, int start, String [] expected) {', ' for (int i = 0; i<expected.length; i++) {', ' String actual = counter.getKthMostFrequent(start);', ' System.out.format("k: %d, expected: %s, actual: %s\\n",', ' start, expected[i], actual);', ' assertEquals(expected[i], actual);', ' start++;', ' }', ' }', '', ' /**', ' * A test method.', ' * (Replace "X" with a name describing the test. You may write as', ' * many "testSomething" methods in this class as you wish, and each', ' * one will be called when running JUnit over this class.)', ' */', ' public void testSimple() {', ' System.out.println("\\nChecking insert");', ' WordCounter counter = new WordCounter();', ' counter.insert("pizzaz");', ' // Insert the word "pizza" into the counter twice', ' counter.insert("pizza");', ' counter.insert("pizza");', ' String [] expected = {"pizza"};', ' checkExpected(counter, 0, expected);', ' }', '', ' public void testTie() {', ' System.out.println("\\nChecking tie for 2nd place");', ' WordCounter counter = new WordCounter();', ' counter.insert("panache");', ' counter.insert("pizzaz");', ' counter.insert("pizza");', ' counter.insert("zebra");', ' counter.insert("pizza");', ' counter.insert("lion");', ' counter.insert("pizzaz");', ' counter.insert("panache");', ' counter.insert("panache");', ' counter.insert("camel");', ' // order is important here', ' String [] expected = {"pizza", "pizzaz"};', ' checkExpected(counter, 1, expected);', ' }', '', '', ' public void testPastTheEnd() {', ' System.out.println("\\nChecking past the end");', ' WordCounter counter = new WordCounter();', ' counter.insert("hi");', ' counter.insert("hello");', ' counter.insert("greetings");', ' counter.insert("salutations");', ' counter.insert("hi");', ' counter.insert("welcome");', ' counter.insert("goodbye");', ' counter.insert("later");', ' counter.insert("hello");', ' counter.insert("when");', ' counter.insert("hi");', ' assertNull(counter.getKthMostFrequent(8));', ' }', '', ' public void test100Top5() {', ' System.out.println("\\nChecking top 5 of 100 words");', ' WordCounter counter = new WordCounter();', ' // Open the file "allWordsBig"', ' openFile("allWordsBig");', ' // Read the next 100 words', ' readWords(counter, 100);', ' String [] expected = {"edu", "comp", "cs", "windows", "cmu"};', ' checkExpected(counter, 0, expected);', ' }', '', ' public void test300Top5() {', ' System.out.println("\\nChecking top 5 of first 100 words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter, 100);', ' String [] expected1 = {"edu", "comp", "cs", "windows", "cmu"};', ' checkExpected(counter, 0, expected1);', '', ' System.out.println("Adding 100 more words and rechecking top 5");', ' readWords(counter, 100);', ' String [] expected2 = {"edu", "cmu", "comp", "cs", "state"};', ' checkExpected(counter, 0, expected2);', '', ' System.out.println("Adding 100 more words and rechecking top 5");', ' readWords(counter, 100);', ' String [] expected3 = {"edu", "cmu", "comp", "ohio", "state"};', ' checkExpected(counter, 0, expected3);', ' }', '', ' public void test300Words14Thru19() {', ' System.out.println("\\nChecking rank 14 through 19 of 300 words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' // Read the next 300 words', ' readWords(counter, 300);', ' String [] expected = {"cantaloupe", "from", "ksu", "okstate", "on", "srv"};', ' checkExpected(counter, 14, expected);', ' }', '', ' public void test300CorrectNumber() {', ' System.out.println("\\nChecking correct number of unique words in 300 words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter, 300);', ' /* check the 122th, 123th, and 124th most frequent word in the corpus so far ', ' * and check if the result is not null.', ' */', ' assertNotNull(counter.getKthMostFrequent(122));', ' assertNotNull(counter.getKthMostFrequent(123));', ' assertNotNull(counter.getKthMostFrequent(124));', ' // check the 125th most frequent word in the corpus so far', ' // and check if the result is null.', ' assertNull(counter.getKthMostFrequent(125));', ' }', '', ' public void test300and100() {', ' System.out.println("\\nChecking top 5 of 100 and 300 words with two counters");', ' WordCounter counter1 = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter1, 300);', ' closeFiles();', '', ' WordCounter counter2 = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter2, 100);', '', ' String [] expected1 = {"edu", "cmu", "comp", "ohio", "state"};', ' checkExpected(counter1, 0, expected1);', '', ' String [] expected2 = {"edu", "comp", "cs", "windows", "cmu"};', ' checkExpected(counter2, 0, expected2);', ' }', '', ' public void testAllTop15() {', ' System.out.println("\\nChecking top 15 of all words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' // Read the next 6000000 words', ' readWords(counter, 6000000);', ' String [] expected = {"the", "edu", "to", "of", "and",', ' "in", "is", "ax", "that", "it",', ' "cmu", "for", "com", "you", "cs"};', ' checkExpected(counter, 0, expected);', ' }', '', ' public void testAllTop10000() {', ' System.out.println("\\nChecking time to get top 10000 of all words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter, 6000000);', '', ' for (int i=0; i<10000; i++) {', ' counter.getKthMostFrequent(i);', ' }', ' }', '', ' public void testSpeed() {', ' System.out.println("\\nMixing adding data with finding top k");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWordsMixed (counter, 6000000);', ' String [] expected = {"the", "edu", "to", "of", "and",', ' "in", "is", "ax", "that", "it",', ' "cmu", "for", "com", "you", "cs"};', ' checkExpected(counter, 0, expected);', ' }', '', '}']
[{'reason_category': 'If Body', 'usage_line': 153}, {'reason_category': 'Loop Body', 'usage_line': 153}]
Variable 's' used at line 153 is defined at line 151 and has a Short-Range dependency.
{'If Body': 1, 'Loop Body': 1}
{'Variable Short-Range': 1}
infilling_java
CounterTester
152
155
['import junit.framework.TestCase;', 'import java.io.*;', 'import java.util.HashMap;', 'import java.util.TreeMap;', 'import java.util.ArrayList;', '', '/**', ' * This silly little class wraps String, int pairs so they can be sorted.', ' * Used by the WordCounter class.', ' */', 'class WordWrapper implements Comparable <WordWrapper> {', '', ' private String myWord;', ' private int myCount;', ' private int mySlot;', '', ' /**', ' * Creates a new WordWrapper with string "XXX" and count zero.', ' */', ' public WordWrapper (String myWordIn, int myCountIn, int mySlotIn) {', ' myWord = myWordIn;', ' myCount = myCountIn;', ' mySlot = mySlotIn;', ' }', ' /**', ' * Returns a copy of the string in the WordWrapper.', ' *', ' * @return The word contained in this object. Is copied out: no aliasing.', ' */', ' public String extractWord () {', ' return new String (myWord);', ' }', '', ' /**', ' * Returns the count in this WordWrapper.', ' *', ' * @return The count contained in the object.', ' */', ' public int extractCount () {', ' return myCount;', ' }', '', ' public void incCount () {', ' myCount++;', ' }', '', ' public int getCount () {', ' return myCount;', ' }', '', ' public int getPos () {', ' return mySlot;', ' }', '', ' public void setPos (int toWhat) {', ' ', ' mySlot = toWhat;', ' }', '', ' public String toString () {', ' return myWord + " " + mySlot;', ' }', '', ' /**', ' * Comparison operator so we implement the Comparable interface.', ' *', ' * @param w The word wrapper to compare to', ' * @return a positive, zero, or negative value depending upon whether this', ' * word wrapper is less then, equal to, or greater than param w. The', ' * relationship is determied by looking at the counts.', ' */', ' public int compareTo (WordWrapper w) {', " // Check if the count of the current word (myCount) is equal to the count of the word 'w'", ' // If the counts are equal, compare the words lexicographically', ' if (w.myCount == myCount) {', ' return myWord.compareTo(w.myWord);', ' }', " // If the counts are not equal, subtract the count of the current word from the count of the word 'w'", ' return w.myCount - myCount;', ' }', '}', '', '', '/**', ' * This class is used to count occurences of words (strings) in a corpus. Used by', ' * the IndexedDocumentCollection class to find the most frequent words in the corpus.', ' */', '', 'class WordCounter {', ' ', " // this is the map that holds (for each word) the number of times we've seen it", ' // note that Integer is a wrapper for the buitin Java int type', ' private HashMap <String, WordWrapper> wordsIHaveSeen = new HashMap <String, WordWrapper> ();', ' private TreeMap <String, String> ones = new TreeMap <String, String> ();', ' ', ' // the set of words that have been extracted', ' private ArrayList<WordWrapper> extractedWords = new ArrayList <WordWrapper> ();', '', ' /**', ' * Accepts a word and increments the count of the number of times we have seen it.', ' * Note that a deep copy of the param is done (no aliasing).', ' *', ' * @param addMe The string to count', ' */', ' public void insert (String addMe) {', ' if (ones.containsKey (addMe)) {', ' ones.remove (addMe);', ' WordWrapper temp = new WordWrapper (addMe, 1, extractedWords.size ());', ' wordsIHaveSeen.put(addMe, temp);', ' extractedWords.add (temp);', ' }', '', ' // first check to see if the word is alredy in wordsIHaveSeen', ' if (wordsIHaveSeen.containsKey (addMe)) {', ' // if it is, then remove and increment its count', ' WordWrapper temp = wordsIHaveSeen.get (addMe);', ' temp.incCount ();', '', ' // find the slot that we go to in the extractedWords list', " // by sorting the 'extractedWords' list in ascending order", ' for (int i = temp.getPos () - 1; i >= 0 && ', ' extractedWords.get (i).compareTo (extractedWords.get (i + 1)) > 0; i--) {', ' temp = extractedWords.get (i + 1);', ' temp.setPos (i);', ' ', ' WordWrapper temp2 = extractedWords.get (i);', ' temp2.setPos (i + 1);', ' ', ' extractedWords.set (i + 1, temp2);', ' extractedWords.set (i, temp);', ' }', '', ' // in this case it is not there, so just add it', ' } else {', ' ones.put (addMe, addMe);', ' }', ' }', '', ' /**', ' * Returns the kth most frequent word in the corpus so far. A deep copy is returned,', ' * so no aliasing is possible. Returns null if there are not enough words.', ' *', ' * @param k Note that the most frequent word is at k = 0', ' * @return The kth most frequent word in the corpus to date', ' */', ' public String getKthMostFrequent (int k) {', '', ' // if we got here, then the array is all set up, so just return the kth most frequent word', ' if (k >= extractedWords.size ()) {', ' int which = extractedWords.size ();', ' for (String s : ones.navigableKeySet ()) {']
[' if (which == k)', ' return s;', ' which++;', ' }']
[' return null;', ' } else {', ' // If k is less than the size of the extractedWords list,', ' // return the kth most frequent word from extractedWords', ' return extractedWords.get (k).extractWord ();', ' }', ' }', '}', '/**', ' * A JUnit test case class.', ' * Every method starting with the word "test" will be called when running', ' * the test with JUnit.', ' */', 'public class CounterTester extends TestCase {', '', ' /**', ' * File object to read words from, with buffering.', ' */', ' private FileReader file = null;', ' private BufferedReader reader = null;', '', ' /**', ' * Close file readers.', ' */', ' private void closeFiles() {', ' try {', ' if (file != null) {', ' file.close();', ' file = null;', ' }', ' if (reader != null) {', ' reader.close();', ' reader = null;', ' }', ' } catch (Exception e) {', ' System.err.println("Problem closing file");', ' System.err.println(e);', ' e.printStackTrace();', ' }', ' }', '', ' /**', ' * Close files no matter what happens in the test.', ' */', ' protected void tearDown () {', ' closeFiles();', ' }', '', ' /**', ' * Open file and set up readers.', ' *', ' * @param fileName name of file to open', ' * */', ' private void openFile(String fileName) {', ' try {', ' file = new FileReader(fileName);', ' reader = new BufferedReader(file);', ' } catch (Exception e) {', ' System.err.format("Problem opening %s file\\n", fileName);', ' System.err.println(e);', ' e.printStackTrace();', ' fail();', ' }', ' }', '', ' /**', ' * Read the next numWords from the file.', ' *', ' * @param counter word counter to update', ' * @param numWords number of words to read.', ' */', ' private void readWords(WordCounter counter, int numWords) {', ' try {', ' for (int i=0; i<numWords; i++) {', ' if (i % 100000 == 0)', ' System.out.print (".");', ' String word = reader.readLine();', ' if (word == null) {', ' return;', ' }', " // Insert 'word' into the counter.", ' counter.insert(word);', ' }', ' } catch (Exception e) {', ' System.err.println("Problem reading file");', ' System.err.println(e);', ' e.printStackTrace();', ' fail();', ' }', ' }', '', ' /**', ' * Read the next numWords from the file, mixing with queries', ' *', ' * @param counter word counter to update', ' * @param numWords number of words to read.', ' */', ' private void readWordsMixed (WordCounter counter, int numWords) {', ' try {', ' int j = 0;', ' for (int i=0; i<numWords; i++) {', ' String word = reader.readLine();', " // If 'word' is null, it means we've reached the end of the file. So, return from the method.", ' if (word == null) {', ' return;', ' }', ' counter.insert(word);', '', ' // If the current iteration number is a multiple of 10 and greater than 100,000, perform a query of the kth most frequent word.', ' if (i % 10 == 0 && i > 100000) {', ' String myStr = counter.getKthMostFrequent(j++);', ' }', '', ' // rest j once we get to 100', ' if (j == 100)', ' j = 0;', ' }', ' } catch (Exception e) {', ' System.err.println("Problem reading file");', ' System.err.println(e);', ' e.printStackTrace();', ' fail();', ' }', ' }', '', ' /**', ' * Check that a sequence of words starts at the "start"th most', ' * frequent word.', ' *', ' * @param counter word counter to lookup', ' * @param start frequency index to start checking at', ' * @param expected array of expected words that start at that frequency', ' */', ' private void checkExpected(WordCounter counter, int start, String [] expected) {', ' for (int i = 0; i<expected.length; i++) {', ' String actual = counter.getKthMostFrequent(start);', ' System.out.format("k: %d, expected: %s, actual: %s\\n",', ' start, expected[i], actual);', ' assertEquals(expected[i], actual);', ' start++;', ' }', ' }', '', ' /**', ' * A test method.', ' * (Replace "X" with a name describing the test. You may write as', ' * many "testSomething" methods in this class as you wish, and each', ' * one will be called when running JUnit over this class.)', ' */', ' public void testSimple() {', ' System.out.println("\\nChecking insert");', ' WordCounter counter = new WordCounter();', ' counter.insert("pizzaz");', ' // Insert the word "pizza" into the counter twice', ' counter.insert("pizza");', ' counter.insert("pizza");', ' String [] expected = {"pizza"};', ' checkExpected(counter, 0, expected);', ' }', '', ' public void testTie() {', ' System.out.println("\\nChecking tie for 2nd place");', ' WordCounter counter = new WordCounter();', ' counter.insert("panache");', ' counter.insert("pizzaz");', ' counter.insert("pizza");', ' counter.insert("zebra");', ' counter.insert("pizza");', ' counter.insert("lion");', ' counter.insert("pizzaz");', ' counter.insert("panache");', ' counter.insert("panache");', ' counter.insert("camel");', ' // order is important here', ' String [] expected = {"pizza", "pizzaz"};', ' checkExpected(counter, 1, expected);', ' }', '', '', ' public void testPastTheEnd() {', ' System.out.println("\\nChecking past the end");', ' WordCounter counter = new WordCounter();', ' counter.insert("hi");', ' counter.insert("hello");', ' counter.insert("greetings");', ' counter.insert("salutations");', ' counter.insert("hi");', ' counter.insert("welcome");', ' counter.insert("goodbye");', ' counter.insert("later");', ' counter.insert("hello");', ' counter.insert("when");', ' counter.insert("hi");', ' assertNull(counter.getKthMostFrequent(8));', ' }', '', ' public void test100Top5() {', ' System.out.println("\\nChecking top 5 of 100 words");', ' WordCounter counter = new WordCounter();', ' // Open the file "allWordsBig"', ' openFile("allWordsBig");', ' // Read the next 100 words', ' readWords(counter, 100);', ' String [] expected = {"edu", "comp", "cs", "windows", "cmu"};', ' checkExpected(counter, 0, expected);', ' }', '', ' public void test300Top5() {', ' System.out.println("\\nChecking top 5 of first 100 words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter, 100);', ' String [] expected1 = {"edu", "comp", "cs", "windows", "cmu"};', ' checkExpected(counter, 0, expected1);', '', ' System.out.println("Adding 100 more words and rechecking top 5");', ' readWords(counter, 100);', ' String [] expected2 = {"edu", "cmu", "comp", "cs", "state"};', ' checkExpected(counter, 0, expected2);', '', ' System.out.println("Adding 100 more words and rechecking top 5");', ' readWords(counter, 100);', ' String [] expected3 = {"edu", "cmu", "comp", "ohio", "state"};', ' checkExpected(counter, 0, expected3);', ' }', '', ' public void test300Words14Thru19() {', ' System.out.println("\\nChecking rank 14 through 19 of 300 words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' // Read the next 300 words', ' readWords(counter, 300);', ' String [] expected = {"cantaloupe", "from", "ksu", "okstate", "on", "srv"};', ' checkExpected(counter, 14, expected);', ' }', '', ' public void test300CorrectNumber() {', ' System.out.println("\\nChecking correct number of unique words in 300 words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter, 300);', ' /* check the 122th, 123th, and 124th most frequent word in the corpus so far ', ' * and check if the result is not null.', ' */', ' assertNotNull(counter.getKthMostFrequent(122));', ' assertNotNull(counter.getKthMostFrequent(123));', ' assertNotNull(counter.getKthMostFrequent(124));', ' // check the 125th most frequent word in the corpus so far', ' // and check if the result is null.', ' assertNull(counter.getKthMostFrequent(125));', ' }', '', ' public void test300and100() {', ' System.out.println("\\nChecking top 5 of 100 and 300 words with two counters");', ' WordCounter counter1 = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter1, 300);', ' closeFiles();', '', ' WordCounter counter2 = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter2, 100);', '', ' String [] expected1 = {"edu", "cmu", "comp", "ohio", "state"};', ' checkExpected(counter1, 0, expected1);', '', ' String [] expected2 = {"edu", "comp", "cs", "windows", "cmu"};', ' checkExpected(counter2, 0, expected2);', ' }', '', ' public void testAllTop15() {', ' System.out.println("\\nChecking top 15 of all words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' // Read the next 6000000 words', ' readWords(counter, 6000000);', ' String [] expected = {"the", "edu", "to", "of", "and",', ' "in", "is", "ax", "that", "it",', ' "cmu", "for", "com", "you", "cs"};', ' checkExpected(counter, 0, expected);', ' }', '', ' public void testAllTop10000() {', ' System.out.println("\\nChecking time to get top 10000 of all words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter, 6000000);', '', ' for (int i=0; i<10000; i++) {', ' counter.getKthMostFrequent(i);', ' }', ' }', '', ' public void testSpeed() {', ' System.out.println("\\nMixing adding data with finding top k");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWordsMixed (counter, 6000000);', ' String [] expected = {"the", "edu", "to", "of", "and",', ' "in", "is", "ax", "that", "it",', ' "cmu", "for", "com", "you", "cs"};', ' checkExpected(counter, 0, expected);', ' }', '', '}']
[{'reason_category': 'Loop Body', 'usage_line': 152}, {'reason_category': 'If Body', 'usage_line': 152}, {'reason_category': 'If Condition', 'usage_line': 152}, {'reason_category': 'If Body', 'usage_line': 153}, {'reason_category': 'Loop Body', 'usage_line': 153}, {'reason_category': 'If Body', 'usage_line': 154}, {'reason_category': 'Loop Body', 'usage_line': 154}, {'reason_category': 'If Body', 'usage_line': 155}, {'reason_category': 'Loop Body', 'usage_line': 155}]
Variable 'which' used at line 152 is defined at line 150 and has a Short-Range dependency. Variable 'k' used at line 152 is defined at line 146 and has a Short-Range dependency. Variable 's' used at line 153 is defined at line 151 and has a Short-Range dependency. Variable 'which' used at line 154 is defined at line 150 and has a Short-Range dependency.
{'Loop Body': 4, 'If Body': 4, 'If Condition': 1}
{'Variable Short-Range': 4}
infilling_java
CounterTester
150
156
['import junit.framework.TestCase;', 'import java.io.*;', 'import java.util.HashMap;', 'import java.util.TreeMap;', 'import java.util.ArrayList;', '', '/**', ' * This silly little class wraps String, int pairs so they can be sorted.', ' * Used by the WordCounter class.', ' */', 'class WordWrapper implements Comparable <WordWrapper> {', '', ' private String myWord;', ' private int myCount;', ' private int mySlot;', '', ' /**', ' * Creates a new WordWrapper with string "XXX" and count zero.', ' */', ' public WordWrapper (String myWordIn, int myCountIn, int mySlotIn) {', ' myWord = myWordIn;', ' myCount = myCountIn;', ' mySlot = mySlotIn;', ' }', ' /**', ' * Returns a copy of the string in the WordWrapper.', ' *', ' * @return The word contained in this object. Is copied out: no aliasing.', ' */', ' public String extractWord () {', ' return new String (myWord);', ' }', '', ' /**', ' * Returns the count in this WordWrapper.', ' *', ' * @return The count contained in the object.', ' */', ' public int extractCount () {', ' return myCount;', ' }', '', ' public void incCount () {', ' myCount++;', ' }', '', ' public int getCount () {', ' return myCount;', ' }', '', ' public int getPos () {', ' return mySlot;', ' }', '', ' public void setPos (int toWhat) {', ' ', ' mySlot = toWhat;', ' }', '', ' public String toString () {', ' return myWord + " " + mySlot;', ' }', '', ' /**', ' * Comparison operator so we implement the Comparable interface.', ' *', ' * @param w The word wrapper to compare to', ' * @return a positive, zero, or negative value depending upon whether this', ' * word wrapper is less then, equal to, or greater than param w. The', ' * relationship is determied by looking at the counts.', ' */', ' public int compareTo (WordWrapper w) {', " // Check if the count of the current word (myCount) is equal to the count of the word 'w'", ' // If the counts are equal, compare the words lexicographically', ' if (w.myCount == myCount) {', ' return myWord.compareTo(w.myWord);', ' }', " // If the counts are not equal, subtract the count of the current word from the count of the word 'w'", ' return w.myCount - myCount;', ' }', '}', '', '', '/**', ' * This class is used to count occurences of words (strings) in a corpus. Used by', ' * the IndexedDocumentCollection class to find the most frequent words in the corpus.', ' */', '', 'class WordCounter {', ' ', " // this is the map that holds (for each word) the number of times we've seen it", ' // note that Integer is a wrapper for the buitin Java int type', ' private HashMap <String, WordWrapper> wordsIHaveSeen = new HashMap <String, WordWrapper> ();', ' private TreeMap <String, String> ones = new TreeMap <String, String> ();', ' ', ' // the set of words that have been extracted', ' private ArrayList<WordWrapper> extractedWords = new ArrayList <WordWrapper> ();', '', ' /**', ' * Accepts a word and increments the count of the number of times we have seen it.', ' * Note that a deep copy of the param is done (no aliasing).', ' *', ' * @param addMe The string to count', ' */', ' public void insert (String addMe) {', ' if (ones.containsKey (addMe)) {', ' ones.remove (addMe);', ' WordWrapper temp = new WordWrapper (addMe, 1, extractedWords.size ());', ' wordsIHaveSeen.put(addMe, temp);', ' extractedWords.add (temp);', ' }', '', ' // first check to see if the word is alredy in wordsIHaveSeen', ' if (wordsIHaveSeen.containsKey (addMe)) {', ' // if it is, then remove and increment its count', ' WordWrapper temp = wordsIHaveSeen.get (addMe);', ' temp.incCount ();', '', ' // find the slot that we go to in the extractedWords list', " // by sorting the 'extractedWords' list in ascending order", ' for (int i = temp.getPos () - 1; i >= 0 && ', ' extractedWords.get (i).compareTo (extractedWords.get (i + 1)) > 0; i--) {', ' temp = extractedWords.get (i + 1);', ' temp.setPos (i);', ' ', ' WordWrapper temp2 = extractedWords.get (i);', ' temp2.setPos (i + 1);', ' ', ' extractedWords.set (i + 1, temp2);', ' extractedWords.set (i, temp);', ' }', '', ' // in this case it is not there, so just add it', ' } else {', ' ones.put (addMe, addMe);', ' }', ' }', '', ' /**', ' * Returns the kth most frequent word in the corpus so far. A deep copy is returned,', ' * so no aliasing is possible. Returns null if there are not enough words.', ' *', ' * @param k Note that the most frequent word is at k = 0', ' * @return The kth most frequent word in the corpus to date', ' */', ' public String getKthMostFrequent (int k) {', '', ' // if we got here, then the array is all set up, so just return the kth most frequent word', ' if (k >= extractedWords.size ()) {']
[' int which = extractedWords.size ();', ' for (String s : ones.navigableKeySet ()) {', ' if (which == k)', ' return s;', ' which++;', ' }', ' return null;']
[' } else {', ' // If k is less than the size of the extractedWords list,', ' // return the kth most frequent word from extractedWords', ' return extractedWords.get (k).extractWord ();', ' }', ' }', '}', '/**', ' * A JUnit test case class.', ' * Every method starting with the word "test" will be called when running', ' * the test with JUnit.', ' */', 'public class CounterTester extends TestCase {', '', ' /**', ' * File object to read words from, with buffering.', ' */', ' private FileReader file = null;', ' private BufferedReader reader = null;', '', ' /**', ' * Close file readers.', ' */', ' private void closeFiles() {', ' try {', ' if (file != null) {', ' file.close();', ' file = null;', ' }', ' if (reader != null) {', ' reader.close();', ' reader = null;', ' }', ' } catch (Exception e) {', ' System.err.println("Problem closing file");', ' System.err.println(e);', ' e.printStackTrace();', ' }', ' }', '', ' /**', ' * Close files no matter what happens in the test.', ' */', ' protected void tearDown () {', ' closeFiles();', ' }', '', ' /**', ' * Open file and set up readers.', ' *', ' * @param fileName name of file to open', ' * */', ' private void openFile(String fileName) {', ' try {', ' file = new FileReader(fileName);', ' reader = new BufferedReader(file);', ' } catch (Exception e) {', ' System.err.format("Problem opening %s file\\n", fileName);', ' System.err.println(e);', ' e.printStackTrace();', ' fail();', ' }', ' }', '', ' /**', ' * Read the next numWords from the file.', ' *', ' * @param counter word counter to update', ' * @param numWords number of words to read.', ' */', ' private void readWords(WordCounter counter, int numWords) {', ' try {', ' for (int i=0; i<numWords; i++) {', ' if (i % 100000 == 0)', ' System.out.print (".");', ' String word = reader.readLine();', ' if (word == null) {', ' return;', ' }', " // Insert 'word' into the counter.", ' counter.insert(word);', ' }', ' } catch (Exception e) {', ' System.err.println("Problem reading file");', ' System.err.println(e);', ' e.printStackTrace();', ' fail();', ' }', ' }', '', ' /**', ' * Read the next numWords from the file, mixing with queries', ' *', ' * @param counter word counter to update', ' * @param numWords number of words to read.', ' */', ' private void readWordsMixed (WordCounter counter, int numWords) {', ' try {', ' int j = 0;', ' for (int i=0; i<numWords; i++) {', ' String word = reader.readLine();', " // If 'word' is null, it means we've reached the end of the file. So, return from the method.", ' if (word == null) {', ' return;', ' }', ' counter.insert(word);', '', ' // If the current iteration number is a multiple of 10 and greater than 100,000, perform a query of the kth most frequent word.', ' if (i % 10 == 0 && i > 100000) {', ' String myStr = counter.getKthMostFrequent(j++);', ' }', '', ' // rest j once we get to 100', ' if (j == 100)', ' j = 0;', ' }', ' } catch (Exception e) {', ' System.err.println("Problem reading file");', ' System.err.println(e);', ' e.printStackTrace();', ' fail();', ' }', ' }', '', ' /**', ' * Check that a sequence of words starts at the "start"th most', ' * frequent word.', ' *', ' * @param counter word counter to lookup', ' * @param start frequency index to start checking at', ' * @param expected array of expected words that start at that frequency', ' */', ' private void checkExpected(WordCounter counter, int start, String [] expected) {', ' for (int i = 0; i<expected.length; i++) {', ' String actual = counter.getKthMostFrequent(start);', ' System.out.format("k: %d, expected: %s, actual: %s\\n",', ' start, expected[i], actual);', ' assertEquals(expected[i], actual);', ' start++;', ' }', ' }', '', ' /**', ' * A test method.', ' * (Replace "X" with a name describing the test. You may write as', ' * many "testSomething" methods in this class as you wish, and each', ' * one will be called when running JUnit over this class.)', ' */', ' public void testSimple() {', ' System.out.println("\\nChecking insert");', ' WordCounter counter = new WordCounter();', ' counter.insert("pizzaz");', ' // Insert the word "pizza" into the counter twice', ' counter.insert("pizza");', ' counter.insert("pizza");', ' String [] expected = {"pizza"};', ' checkExpected(counter, 0, expected);', ' }', '', ' public void testTie() {', ' System.out.println("\\nChecking tie for 2nd place");', ' WordCounter counter = new WordCounter();', ' counter.insert("panache");', ' counter.insert("pizzaz");', ' counter.insert("pizza");', ' counter.insert("zebra");', ' counter.insert("pizza");', ' counter.insert("lion");', ' counter.insert("pizzaz");', ' counter.insert("panache");', ' counter.insert("panache");', ' counter.insert("camel");', ' // order is important here', ' String [] expected = {"pizza", "pizzaz"};', ' checkExpected(counter, 1, expected);', ' }', '', '', ' public void testPastTheEnd() {', ' System.out.println("\\nChecking past the end");', ' WordCounter counter = new WordCounter();', ' counter.insert("hi");', ' counter.insert("hello");', ' counter.insert("greetings");', ' counter.insert("salutations");', ' counter.insert("hi");', ' counter.insert("welcome");', ' counter.insert("goodbye");', ' counter.insert("later");', ' counter.insert("hello");', ' counter.insert("when");', ' counter.insert("hi");', ' assertNull(counter.getKthMostFrequent(8));', ' }', '', ' public void test100Top5() {', ' System.out.println("\\nChecking top 5 of 100 words");', ' WordCounter counter = new WordCounter();', ' // Open the file "allWordsBig"', ' openFile("allWordsBig");', ' // Read the next 100 words', ' readWords(counter, 100);', ' String [] expected = {"edu", "comp", "cs", "windows", "cmu"};', ' checkExpected(counter, 0, expected);', ' }', '', ' public void test300Top5() {', ' System.out.println("\\nChecking top 5 of first 100 words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter, 100);', ' String [] expected1 = {"edu", "comp", "cs", "windows", "cmu"};', ' checkExpected(counter, 0, expected1);', '', ' System.out.println("Adding 100 more words and rechecking top 5");', ' readWords(counter, 100);', ' String [] expected2 = {"edu", "cmu", "comp", "cs", "state"};', ' checkExpected(counter, 0, expected2);', '', ' System.out.println("Adding 100 more words and rechecking top 5");', ' readWords(counter, 100);', ' String [] expected3 = {"edu", "cmu", "comp", "ohio", "state"};', ' checkExpected(counter, 0, expected3);', ' }', '', ' public void test300Words14Thru19() {', ' System.out.println("\\nChecking rank 14 through 19 of 300 words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' // Read the next 300 words', ' readWords(counter, 300);', ' String [] expected = {"cantaloupe", "from", "ksu", "okstate", "on", "srv"};', ' checkExpected(counter, 14, expected);', ' }', '', ' public void test300CorrectNumber() {', ' System.out.println("\\nChecking correct number of unique words in 300 words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter, 300);', ' /* check the 122th, 123th, and 124th most frequent word in the corpus so far ', ' * and check if the result is not null.', ' */', ' assertNotNull(counter.getKthMostFrequent(122));', ' assertNotNull(counter.getKthMostFrequent(123));', ' assertNotNull(counter.getKthMostFrequent(124));', ' // check the 125th most frequent word in the corpus so far', ' // and check if the result is null.', ' assertNull(counter.getKthMostFrequent(125));', ' }', '', ' public void test300and100() {', ' System.out.println("\\nChecking top 5 of 100 and 300 words with two counters");', ' WordCounter counter1 = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter1, 300);', ' closeFiles();', '', ' WordCounter counter2 = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter2, 100);', '', ' String [] expected1 = {"edu", "cmu", "comp", "ohio", "state"};', ' checkExpected(counter1, 0, expected1);', '', ' String [] expected2 = {"edu", "comp", "cs", "windows", "cmu"};', ' checkExpected(counter2, 0, expected2);', ' }', '', ' public void testAllTop15() {', ' System.out.println("\\nChecking top 15 of all words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' // Read the next 6000000 words', ' readWords(counter, 6000000);', ' String [] expected = {"the", "edu", "to", "of", "and",', ' "in", "is", "ax", "that", "it",', ' "cmu", "for", "com", "you", "cs"};', ' checkExpected(counter, 0, expected);', ' }', '', ' public void testAllTop10000() {', ' System.out.println("\\nChecking time to get top 10000 of all words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter, 6000000);', '', ' for (int i=0; i<10000; i++) {', ' counter.getKthMostFrequent(i);', ' }', ' }', '', ' public void testSpeed() {', ' System.out.println("\\nMixing adding data with finding top k");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWordsMixed (counter, 6000000);', ' String [] expected = {"the", "edu", "to", "of", "and",', ' "in", "is", "ax", "that", "it",', ' "cmu", "for", "com", "you", "cs"};', ' checkExpected(counter, 0, expected);', ' }', '', '}']
[{'reason_category': 'If Body', 'usage_line': 150}, {'reason_category': 'If Body', 'usage_line': 151}, {'reason_category': 'Define Stop Criteria', 'usage_line': 151}, {'reason_category': 'Loop Body', 'usage_line': 152}, {'reason_category': 'If Body', 'usage_line': 152}, {'reason_category': 'If Condition', 'usage_line': 152}, {'reason_category': 'If Body', 'usage_line': 153}, {'reason_category': 'Loop Body', 'usage_line': 153}, {'reason_category': 'If Body', 'usage_line': 154}, {'reason_category': 'Loop Body', 'usage_line': 154}, {'reason_category': 'If Body', 'usage_line': 155}, {'reason_category': 'Loop Body', 'usage_line': 155}, {'reason_category': 'If Body', 'usage_line': 156}]
Global_Variable 'extractedWords' used at line 150 is defined at line 97 and has a Long-Range dependency. Global_Variable 'ones' used at line 151 is defined at line 94 and has a Long-Range dependency. Variable 'which' used at line 152 is defined at line 150 and has a Short-Range dependency. Variable 'k' used at line 152 is defined at line 146 and has a Short-Range dependency. Variable 's' used at line 153 is defined at line 151 and has a Short-Range dependency. Variable 'which' used at line 154 is defined at line 150 and has a Short-Range dependency.
{'If Body': 7, 'Define Stop Criteria': 1, 'Loop Body': 4, 'If Condition': 1}
{'Global_Variable Long-Range': 2, 'Variable Short-Range': 4}
infilling_java
CounterTester
160
161
['import junit.framework.TestCase;', 'import java.io.*;', 'import java.util.HashMap;', 'import java.util.TreeMap;', 'import java.util.ArrayList;', '', '/**', ' * This silly little class wraps String, int pairs so they can be sorted.', ' * Used by the WordCounter class.', ' */', 'class WordWrapper implements Comparable <WordWrapper> {', '', ' private String myWord;', ' private int myCount;', ' private int mySlot;', '', ' /**', ' * Creates a new WordWrapper with string "XXX" and count zero.', ' */', ' public WordWrapper (String myWordIn, int myCountIn, int mySlotIn) {', ' myWord = myWordIn;', ' myCount = myCountIn;', ' mySlot = mySlotIn;', ' }', ' /**', ' * Returns a copy of the string in the WordWrapper.', ' *', ' * @return The word contained in this object. Is copied out: no aliasing.', ' */', ' public String extractWord () {', ' return new String (myWord);', ' }', '', ' /**', ' * Returns the count in this WordWrapper.', ' *', ' * @return The count contained in the object.', ' */', ' public int extractCount () {', ' return myCount;', ' }', '', ' public void incCount () {', ' myCount++;', ' }', '', ' public int getCount () {', ' return myCount;', ' }', '', ' public int getPos () {', ' return mySlot;', ' }', '', ' public void setPos (int toWhat) {', ' ', ' mySlot = toWhat;', ' }', '', ' public String toString () {', ' return myWord + " " + mySlot;', ' }', '', ' /**', ' * Comparison operator so we implement the Comparable interface.', ' *', ' * @param w The word wrapper to compare to', ' * @return a positive, zero, or negative value depending upon whether this', ' * word wrapper is less then, equal to, or greater than param w. The', ' * relationship is determied by looking at the counts.', ' */', ' public int compareTo (WordWrapper w) {', " // Check if the count of the current word (myCount) is equal to the count of the word 'w'", ' // If the counts are equal, compare the words lexicographically', ' if (w.myCount == myCount) {', ' return myWord.compareTo(w.myWord);', ' }', " // If the counts are not equal, subtract the count of the current word from the count of the word 'w'", ' return w.myCount - myCount;', ' }', '}', '', '', '/**', ' * This class is used to count occurences of words (strings) in a corpus. Used by', ' * the IndexedDocumentCollection class to find the most frequent words in the corpus.', ' */', '', 'class WordCounter {', ' ', " // this is the map that holds (for each word) the number of times we've seen it", ' // note that Integer is a wrapper for the buitin Java int type', ' private HashMap <String, WordWrapper> wordsIHaveSeen = new HashMap <String, WordWrapper> ();', ' private TreeMap <String, String> ones = new TreeMap <String, String> ();', ' ', ' // the set of words that have been extracted', ' private ArrayList<WordWrapper> extractedWords = new ArrayList <WordWrapper> ();', '', ' /**', ' * Accepts a word and increments the count of the number of times we have seen it.', ' * Note that a deep copy of the param is done (no aliasing).', ' *', ' * @param addMe The string to count', ' */', ' public void insert (String addMe) {', ' if (ones.containsKey (addMe)) {', ' ones.remove (addMe);', ' WordWrapper temp = new WordWrapper (addMe, 1, extractedWords.size ());', ' wordsIHaveSeen.put(addMe, temp);', ' extractedWords.add (temp);', ' }', '', ' // first check to see if the word is alredy in wordsIHaveSeen', ' if (wordsIHaveSeen.containsKey (addMe)) {', ' // if it is, then remove and increment its count', ' WordWrapper temp = wordsIHaveSeen.get (addMe);', ' temp.incCount ();', '', ' // find the slot that we go to in the extractedWords list', " // by sorting the 'extractedWords' list in ascending order", ' for (int i = temp.getPos () - 1; i >= 0 && ', ' extractedWords.get (i).compareTo (extractedWords.get (i + 1)) > 0; i--) {', ' temp = extractedWords.get (i + 1);', ' temp.setPos (i);', ' ', ' WordWrapper temp2 = extractedWords.get (i);', ' temp2.setPos (i + 1);', ' ', ' extractedWords.set (i + 1, temp2);', ' extractedWords.set (i, temp);', ' }', '', ' // in this case it is not there, so just add it', ' } else {', ' ones.put (addMe, addMe);', ' }', ' }', '', ' /**', ' * Returns the kth most frequent word in the corpus so far. A deep copy is returned,', ' * so no aliasing is possible. Returns null if there are not enough words.', ' *', ' * @param k Note that the most frequent word is at k = 0', ' * @return The kth most frequent word in the corpus to date', ' */', ' public String getKthMostFrequent (int k) {', '', ' // if we got here, then the array is all set up, so just return the kth most frequent word', ' if (k >= extractedWords.size ()) {', ' int which = extractedWords.size ();', ' for (String s : ones.navigableKeySet ()) {', ' if (which == k)', ' return s;', ' which++;', ' }', ' return null;', ' } else {', ' // If k is less than the size of the extractedWords list,', ' // return the kth most frequent word from extractedWords']
[' return extractedWords.get (k).extractWord ();', ' }']
[' }', '}', '/**', ' * A JUnit test case class.', ' * Every method starting with the word "test" will be called when running', ' * the test with JUnit.', ' */', 'public class CounterTester extends TestCase {', '', ' /**', ' * File object to read words from, with buffering.', ' */', ' private FileReader file = null;', ' private BufferedReader reader = null;', '', ' /**', ' * Close file readers.', ' */', ' private void closeFiles() {', ' try {', ' if (file != null) {', ' file.close();', ' file = null;', ' }', ' if (reader != null) {', ' reader.close();', ' reader = null;', ' }', ' } catch (Exception e) {', ' System.err.println("Problem closing file");', ' System.err.println(e);', ' e.printStackTrace();', ' }', ' }', '', ' /**', ' * Close files no matter what happens in the test.', ' */', ' protected void tearDown () {', ' closeFiles();', ' }', '', ' /**', ' * Open file and set up readers.', ' *', ' * @param fileName name of file to open', ' * */', ' private void openFile(String fileName) {', ' try {', ' file = new FileReader(fileName);', ' reader = new BufferedReader(file);', ' } catch (Exception e) {', ' System.err.format("Problem opening %s file\\n", fileName);', ' System.err.println(e);', ' e.printStackTrace();', ' fail();', ' }', ' }', '', ' /**', ' * Read the next numWords from the file.', ' *', ' * @param counter word counter to update', ' * @param numWords number of words to read.', ' */', ' private void readWords(WordCounter counter, int numWords) {', ' try {', ' for (int i=0; i<numWords; i++) {', ' if (i % 100000 == 0)', ' System.out.print (".");', ' String word = reader.readLine();', ' if (word == null) {', ' return;', ' }', " // Insert 'word' into the counter.", ' counter.insert(word);', ' }', ' } catch (Exception e) {', ' System.err.println("Problem reading file");', ' System.err.println(e);', ' e.printStackTrace();', ' fail();', ' }', ' }', '', ' /**', ' * Read the next numWords from the file, mixing with queries', ' *', ' * @param counter word counter to update', ' * @param numWords number of words to read.', ' */', ' private void readWordsMixed (WordCounter counter, int numWords) {', ' try {', ' int j = 0;', ' for (int i=0; i<numWords; i++) {', ' String word = reader.readLine();', " // If 'word' is null, it means we've reached the end of the file. So, return from the method.", ' if (word == null) {', ' return;', ' }', ' counter.insert(word);', '', ' // If the current iteration number is a multiple of 10 and greater than 100,000, perform a query of the kth most frequent word.', ' if (i % 10 == 0 && i > 100000) {', ' String myStr = counter.getKthMostFrequent(j++);', ' }', '', ' // rest j once we get to 100', ' if (j == 100)', ' j = 0;', ' }', ' } catch (Exception e) {', ' System.err.println("Problem reading file");', ' System.err.println(e);', ' e.printStackTrace();', ' fail();', ' }', ' }', '', ' /**', ' * Check that a sequence of words starts at the "start"th most', ' * frequent word.', ' *', ' * @param counter word counter to lookup', ' * @param start frequency index to start checking at', ' * @param expected array of expected words that start at that frequency', ' */', ' private void checkExpected(WordCounter counter, int start, String [] expected) {', ' for (int i = 0; i<expected.length; i++) {', ' String actual = counter.getKthMostFrequent(start);', ' System.out.format("k: %d, expected: %s, actual: %s\\n",', ' start, expected[i], actual);', ' assertEquals(expected[i], actual);', ' start++;', ' }', ' }', '', ' /**', ' * A test method.', ' * (Replace "X" with a name describing the test. You may write as', ' * many "testSomething" methods in this class as you wish, and each', ' * one will be called when running JUnit over this class.)', ' */', ' public void testSimple() {', ' System.out.println("\\nChecking insert");', ' WordCounter counter = new WordCounter();', ' counter.insert("pizzaz");', ' // Insert the word "pizza" into the counter twice', ' counter.insert("pizza");', ' counter.insert("pizza");', ' String [] expected = {"pizza"};', ' checkExpected(counter, 0, expected);', ' }', '', ' public void testTie() {', ' System.out.println("\\nChecking tie for 2nd place");', ' WordCounter counter = new WordCounter();', ' counter.insert("panache");', ' counter.insert("pizzaz");', ' counter.insert("pizza");', ' counter.insert("zebra");', ' counter.insert("pizza");', ' counter.insert("lion");', ' counter.insert("pizzaz");', ' counter.insert("panache");', ' counter.insert("panache");', ' counter.insert("camel");', ' // order is important here', ' String [] expected = {"pizza", "pizzaz"};', ' checkExpected(counter, 1, expected);', ' }', '', '', ' public void testPastTheEnd() {', ' System.out.println("\\nChecking past the end");', ' WordCounter counter = new WordCounter();', ' counter.insert("hi");', ' counter.insert("hello");', ' counter.insert("greetings");', ' counter.insert("salutations");', ' counter.insert("hi");', ' counter.insert("welcome");', ' counter.insert("goodbye");', ' counter.insert("later");', ' counter.insert("hello");', ' counter.insert("when");', ' counter.insert("hi");', ' assertNull(counter.getKthMostFrequent(8));', ' }', '', ' public void test100Top5() {', ' System.out.println("\\nChecking top 5 of 100 words");', ' WordCounter counter = new WordCounter();', ' // Open the file "allWordsBig"', ' openFile("allWordsBig");', ' // Read the next 100 words', ' readWords(counter, 100);', ' String [] expected = {"edu", "comp", "cs", "windows", "cmu"};', ' checkExpected(counter, 0, expected);', ' }', '', ' public void test300Top5() {', ' System.out.println("\\nChecking top 5 of first 100 words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter, 100);', ' String [] expected1 = {"edu", "comp", "cs", "windows", "cmu"};', ' checkExpected(counter, 0, expected1);', '', ' System.out.println("Adding 100 more words and rechecking top 5");', ' readWords(counter, 100);', ' String [] expected2 = {"edu", "cmu", "comp", "cs", "state"};', ' checkExpected(counter, 0, expected2);', '', ' System.out.println("Adding 100 more words and rechecking top 5");', ' readWords(counter, 100);', ' String [] expected3 = {"edu", "cmu", "comp", "ohio", "state"};', ' checkExpected(counter, 0, expected3);', ' }', '', ' public void test300Words14Thru19() {', ' System.out.println("\\nChecking rank 14 through 19 of 300 words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' // Read the next 300 words', ' readWords(counter, 300);', ' String [] expected = {"cantaloupe", "from", "ksu", "okstate", "on", "srv"};', ' checkExpected(counter, 14, expected);', ' }', '', ' public void test300CorrectNumber() {', ' System.out.println("\\nChecking correct number of unique words in 300 words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter, 300);', ' /* check the 122th, 123th, and 124th most frequent word in the corpus so far ', ' * and check if the result is not null.', ' */', ' assertNotNull(counter.getKthMostFrequent(122));', ' assertNotNull(counter.getKthMostFrequent(123));', ' assertNotNull(counter.getKthMostFrequent(124));', ' // check the 125th most frequent word in the corpus so far', ' // and check if the result is null.', ' assertNull(counter.getKthMostFrequent(125));', ' }', '', ' public void test300and100() {', ' System.out.println("\\nChecking top 5 of 100 and 300 words with two counters");', ' WordCounter counter1 = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter1, 300);', ' closeFiles();', '', ' WordCounter counter2 = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter2, 100);', '', ' String [] expected1 = {"edu", "cmu", "comp", "ohio", "state"};', ' checkExpected(counter1, 0, expected1);', '', ' String [] expected2 = {"edu", "comp", "cs", "windows", "cmu"};', ' checkExpected(counter2, 0, expected2);', ' }', '', ' public void testAllTop15() {', ' System.out.println("\\nChecking top 15 of all words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' // Read the next 6000000 words', ' readWords(counter, 6000000);', ' String [] expected = {"the", "edu", "to", "of", "and",', ' "in", "is", "ax", "that", "it",', ' "cmu", "for", "com", "you", "cs"};', ' checkExpected(counter, 0, expected);', ' }', '', ' public void testAllTop10000() {', ' System.out.println("\\nChecking time to get top 10000 of all words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter, 6000000);', '', ' for (int i=0; i<10000; i++) {', ' counter.getKthMostFrequent(i);', ' }', ' }', '', ' public void testSpeed() {', ' System.out.println("\\nMixing adding data with finding top k");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWordsMixed (counter, 6000000);', ' String [] expected = {"the", "edu", "to", "of", "and",', ' "in", "is", "ax", "that", "it",', ' "cmu", "for", "com", "you", "cs"};', ' checkExpected(counter, 0, expected);', ' }', '', '}']
[{'reason_category': 'Else Reasoning', 'usage_line': 160}, {'reason_category': 'Else Reasoning', 'usage_line': 161}]
Global_Variable 'extractedWords' used at line 160 is defined at line 97 and has a Long-Range dependency. Variable 'k' used at line 160 is defined at line 146 and has a Medium-Range dependency. Function 'extractWord' used at line 160 is defined at line 30 and has a Long-Range dependency.
{'Else Reasoning': 2}
{'Global_Variable Long-Range': 1, 'Variable Medium-Range': 1, 'Function Long-Range': 1}
infilling_java
CounterTester
183
185
['import junit.framework.TestCase;', 'import java.io.*;', 'import java.util.HashMap;', 'import java.util.TreeMap;', 'import java.util.ArrayList;', '', '/**', ' * This silly little class wraps String, int pairs so they can be sorted.', ' * Used by the WordCounter class.', ' */', 'class WordWrapper implements Comparable <WordWrapper> {', '', ' private String myWord;', ' private int myCount;', ' private int mySlot;', '', ' /**', ' * Creates a new WordWrapper with string "XXX" and count zero.', ' */', ' public WordWrapper (String myWordIn, int myCountIn, int mySlotIn) {', ' myWord = myWordIn;', ' myCount = myCountIn;', ' mySlot = mySlotIn;', ' }', ' /**', ' * Returns a copy of the string in the WordWrapper.', ' *', ' * @return The word contained in this object. Is copied out: no aliasing.', ' */', ' public String extractWord () {', ' return new String (myWord);', ' }', '', ' /**', ' * Returns the count in this WordWrapper.', ' *', ' * @return The count contained in the object.', ' */', ' public int extractCount () {', ' return myCount;', ' }', '', ' public void incCount () {', ' myCount++;', ' }', '', ' public int getCount () {', ' return myCount;', ' }', '', ' public int getPos () {', ' return mySlot;', ' }', '', ' public void setPos (int toWhat) {', ' ', ' mySlot = toWhat;', ' }', '', ' public String toString () {', ' return myWord + " " + mySlot;', ' }', '', ' /**', ' * Comparison operator so we implement the Comparable interface.', ' *', ' * @param w The word wrapper to compare to', ' * @return a positive, zero, or negative value depending upon whether this', ' * word wrapper is less then, equal to, or greater than param w. The', ' * relationship is determied by looking at the counts.', ' */', ' public int compareTo (WordWrapper w) {', " // Check if the count of the current word (myCount) is equal to the count of the word 'w'", ' // If the counts are equal, compare the words lexicographically', ' if (w.myCount == myCount) {', ' return myWord.compareTo(w.myWord);', ' }', " // If the counts are not equal, subtract the count of the current word from the count of the word 'w'", ' return w.myCount - myCount;', ' }', '}', '', '', '/**', ' * This class is used to count occurences of words (strings) in a corpus. Used by', ' * the IndexedDocumentCollection class to find the most frequent words in the corpus.', ' */', '', 'class WordCounter {', ' ', " // this is the map that holds (for each word) the number of times we've seen it", ' // note that Integer is a wrapper for the buitin Java int type', ' private HashMap <String, WordWrapper> wordsIHaveSeen = new HashMap <String, WordWrapper> ();', ' private TreeMap <String, String> ones = new TreeMap <String, String> ();', ' ', ' // the set of words that have been extracted', ' private ArrayList<WordWrapper> extractedWords = new ArrayList <WordWrapper> ();', '', ' /**', ' * Accepts a word and increments the count of the number of times we have seen it.', ' * Note that a deep copy of the param is done (no aliasing).', ' *', ' * @param addMe The string to count', ' */', ' public void insert (String addMe) {', ' if (ones.containsKey (addMe)) {', ' ones.remove (addMe);', ' WordWrapper temp = new WordWrapper (addMe, 1, extractedWords.size ());', ' wordsIHaveSeen.put(addMe, temp);', ' extractedWords.add (temp);', ' }', '', ' // first check to see if the word is alredy in wordsIHaveSeen', ' if (wordsIHaveSeen.containsKey (addMe)) {', ' // if it is, then remove and increment its count', ' WordWrapper temp = wordsIHaveSeen.get (addMe);', ' temp.incCount ();', '', ' // find the slot that we go to in the extractedWords list', " // by sorting the 'extractedWords' list in ascending order", ' for (int i = temp.getPos () - 1; i >= 0 && ', ' extractedWords.get (i).compareTo (extractedWords.get (i + 1)) > 0; i--) {', ' temp = extractedWords.get (i + 1);', ' temp.setPos (i);', ' ', ' WordWrapper temp2 = extractedWords.get (i);', ' temp2.setPos (i + 1);', ' ', ' extractedWords.set (i + 1, temp2);', ' extractedWords.set (i, temp);', ' }', '', ' // in this case it is not there, so just add it', ' } else {', ' ones.put (addMe, addMe);', ' }', ' }', '', ' /**', ' * Returns the kth most frequent word in the corpus so far. A deep copy is returned,', ' * so no aliasing is possible. Returns null if there are not enough words.', ' *', ' * @param k Note that the most frequent word is at k = 0', ' * @return The kth most frequent word in the corpus to date', ' */', ' public String getKthMostFrequent (int k) {', '', ' // if we got here, then the array is all set up, so just return the kth most frequent word', ' if (k >= extractedWords.size ()) {', ' int which = extractedWords.size ();', ' for (String s : ones.navigableKeySet ()) {', ' if (which == k)', ' return s;', ' which++;', ' }', ' return null;', ' } else {', ' // If k is less than the size of the extractedWords list,', ' // return the kth most frequent word from extractedWords', ' return extractedWords.get (k).extractWord ();', ' }', ' }', '}', '/**', ' * A JUnit test case class.', ' * Every method starting with the word "test" will be called when running', ' * the test with JUnit.', ' */', 'public class CounterTester extends TestCase {', '', ' /**', ' * File object to read words from, with buffering.', ' */', ' private FileReader file = null;', ' private BufferedReader reader = null;', '', ' /**', ' * Close file readers.', ' */', ' private void closeFiles() {', ' try {', ' if (file != null) {']
[' file.close();', ' file = null;', ' }']
[' if (reader != null) {', ' reader.close();', ' reader = null;', ' }', ' } catch (Exception e) {', ' System.err.println("Problem closing file");', ' System.err.println(e);', ' e.printStackTrace();', ' }', ' }', '', ' /**', ' * Close files no matter what happens in the test.', ' */', ' protected void tearDown () {', ' closeFiles();', ' }', '', ' /**', ' * Open file and set up readers.', ' *', ' * @param fileName name of file to open', ' * */', ' private void openFile(String fileName) {', ' try {', ' file = new FileReader(fileName);', ' reader = new BufferedReader(file);', ' } catch (Exception e) {', ' System.err.format("Problem opening %s file\\n", fileName);', ' System.err.println(e);', ' e.printStackTrace();', ' fail();', ' }', ' }', '', ' /**', ' * Read the next numWords from the file.', ' *', ' * @param counter word counter to update', ' * @param numWords number of words to read.', ' */', ' private void readWords(WordCounter counter, int numWords) {', ' try {', ' for (int i=0; i<numWords; i++) {', ' if (i % 100000 == 0)', ' System.out.print (".");', ' String word = reader.readLine();', ' if (word == null) {', ' return;', ' }', " // Insert 'word' into the counter.", ' counter.insert(word);', ' }', ' } catch (Exception e) {', ' System.err.println("Problem reading file");', ' System.err.println(e);', ' e.printStackTrace();', ' fail();', ' }', ' }', '', ' /**', ' * Read the next numWords from the file, mixing with queries', ' *', ' * @param counter word counter to update', ' * @param numWords number of words to read.', ' */', ' private void readWordsMixed (WordCounter counter, int numWords) {', ' try {', ' int j = 0;', ' for (int i=0; i<numWords; i++) {', ' String word = reader.readLine();', " // If 'word' is null, it means we've reached the end of the file. So, return from the method.", ' if (word == null) {', ' return;', ' }', ' counter.insert(word);', '', ' // If the current iteration number is a multiple of 10 and greater than 100,000, perform a query of the kth most frequent word.', ' if (i % 10 == 0 && i > 100000) {', ' String myStr = counter.getKthMostFrequent(j++);', ' }', '', ' // rest j once we get to 100', ' if (j == 100)', ' j = 0;', ' }', ' } catch (Exception e) {', ' System.err.println("Problem reading file");', ' System.err.println(e);', ' e.printStackTrace();', ' fail();', ' }', ' }', '', ' /**', ' * Check that a sequence of words starts at the "start"th most', ' * frequent word.', ' *', ' * @param counter word counter to lookup', ' * @param start frequency index to start checking at', ' * @param expected array of expected words that start at that frequency', ' */', ' private void checkExpected(WordCounter counter, int start, String [] expected) {', ' for (int i = 0; i<expected.length; i++) {', ' String actual = counter.getKthMostFrequent(start);', ' System.out.format("k: %d, expected: %s, actual: %s\\n",', ' start, expected[i], actual);', ' assertEquals(expected[i], actual);', ' start++;', ' }', ' }', '', ' /**', ' * A test method.', ' * (Replace "X" with a name describing the test. You may write as', ' * many "testSomething" methods in this class as you wish, and each', ' * one will be called when running JUnit over this class.)', ' */', ' public void testSimple() {', ' System.out.println("\\nChecking insert");', ' WordCounter counter = new WordCounter();', ' counter.insert("pizzaz");', ' // Insert the word "pizza" into the counter twice', ' counter.insert("pizza");', ' counter.insert("pizza");', ' String [] expected = {"pizza"};', ' checkExpected(counter, 0, expected);', ' }', '', ' public void testTie() {', ' System.out.println("\\nChecking tie for 2nd place");', ' WordCounter counter = new WordCounter();', ' counter.insert("panache");', ' counter.insert("pizzaz");', ' counter.insert("pizza");', ' counter.insert("zebra");', ' counter.insert("pizza");', ' counter.insert("lion");', ' counter.insert("pizzaz");', ' counter.insert("panache");', ' counter.insert("panache");', ' counter.insert("camel");', ' // order is important here', ' String [] expected = {"pizza", "pizzaz"};', ' checkExpected(counter, 1, expected);', ' }', '', '', ' public void testPastTheEnd() {', ' System.out.println("\\nChecking past the end");', ' WordCounter counter = new WordCounter();', ' counter.insert("hi");', ' counter.insert("hello");', ' counter.insert("greetings");', ' counter.insert("salutations");', ' counter.insert("hi");', ' counter.insert("welcome");', ' counter.insert("goodbye");', ' counter.insert("later");', ' counter.insert("hello");', ' counter.insert("when");', ' counter.insert("hi");', ' assertNull(counter.getKthMostFrequent(8));', ' }', '', ' public void test100Top5() {', ' System.out.println("\\nChecking top 5 of 100 words");', ' WordCounter counter = new WordCounter();', ' // Open the file "allWordsBig"', ' openFile("allWordsBig");', ' // Read the next 100 words', ' readWords(counter, 100);', ' String [] expected = {"edu", "comp", "cs", "windows", "cmu"};', ' checkExpected(counter, 0, expected);', ' }', '', ' public void test300Top5() {', ' System.out.println("\\nChecking top 5 of first 100 words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter, 100);', ' String [] expected1 = {"edu", "comp", "cs", "windows", "cmu"};', ' checkExpected(counter, 0, expected1);', '', ' System.out.println("Adding 100 more words and rechecking top 5");', ' readWords(counter, 100);', ' String [] expected2 = {"edu", "cmu", "comp", "cs", "state"};', ' checkExpected(counter, 0, expected2);', '', ' System.out.println("Adding 100 more words and rechecking top 5");', ' readWords(counter, 100);', ' String [] expected3 = {"edu", "cmu", "comp", "ohio", "state"};', ' checkExpected(counter, 0, expected3);', ' }', '', ' public void test300Words14Thru19() {', ' System.out.println("\\nChecking rank 14 through 19 of 300 words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' // Read the next 300 words', ' readWords(counter, 300);', ' String [] expected = {"cantaloupe", "from", "ksu", "okstate", "on", "srv"};', ' checkExpected(counter, 14, expected);', ' }', '', ' public void test300CorrectNumber() {', ' System.out.println("\\nChecking correct number of unique words in 300 words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter, 300);', ' /* check the 122th, 123th, and 124th most frequent word in the corpus so far ', ' * and check if the result is not null.', ' */', ' assertNotNull(counter.getKthMostFrequent(122));', ' assertNotNull(counter.getKthMostFrequent(123));', ' assertNotNull(counter.getKthMostFrequent(124));', ' // check the 125th most frequent word in the corpus so far', ' // and check if the result is null.', ' assertNull(counter.getKthMostFrequent(125));', ' }', '', ' public void test300and100() {', ' System.out.println("\\nChecking top 5 of 100 and 300 words with two counters");', ' WordCounter counter1 = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter1, 300);', ' closeFiles();', '', ' WordCounter counter2 = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter2, 100);', '', ' String [] expected1 = {"edu", "cmu", "comp", "ohio", "state"};', ' checkExpected(counter1, 0, expected1);', '', ' String [] expected2 = {"edu", "comp", "cs", "windows", "cmu"};', ' checkExpected(counter2, 0, expected2);', ' }', '', ' public void testAllTop15() {', ' System.out.println("\\nChecking top 15 of all words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' // Read the next 6000000 words', ' readWords(counter, 6000000);', ' String [] expected = {"the", "edu", "to", "of", "and",', ' "in", "is", "ax", "that", "it",', ' "cmu", "for", "com", "you", "cs"};', ' checkExpected(counter, 0, expected);', ' }', '', ' public void testAllTop10000() {', ' System.out.println("\\nChecking time to get top 10000 of all words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter, 6000000);', '', ' for (int i=0; i<10000; i++) {', ' counter.getKthMostFrequent(i);', ' }', ' }', '', ' public void testSpeed() {', ' System.out.println("\\nMixing adding data with finding top k");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWordsMixed (counter, 6000000);', ' String [] expected = {"the", "edu", "to", "of", "and",', ' "in", "is", "ax", "that", "it",', ' "cmu", "for", "com", "you", "cs"};', ' checkExpected(counter, 0, expected);', ' }', '', '}']
[{'reason_category': 'If Body', 'usage_line': 183}, {'reason_category': 'If Body', 'usage_line': 184}, {'reason_category': 'If Body', 'usage_line': 185}]
Global_Variable 'file' used at line 183 is defined at line 174 and has a Short-Range dependency. Global_Variable 'file' used at line 184 is defined at line 174 and has a Short-Range dependency.
{'If Body': 3}
{'Global_Variable Short-Range': 2}
infilling_java
CounterTester
187
189
['import junit.framework.TestCase;', 'import java.io.*;', 'import java.util.HashMap;', 'import java.util.TreeMap;', 'import java.util.ArrayList;', '', '/**', ' * This silly little class wraps String, int pairs so they can be sorted.', ' * Used by the WordCounter class.', ' */', 'class WordWrapper implements Comparable <WordWrapper> {', '', ' private String myWord;', ' private int myCount;', ' private int mySlot;', '', ' /**', ' * Creates a new WordWrapper with string "XXX" and count zero.', ' */', ' public WordWrapper (String myWordIn, int myCountIn, int mySlotIn) {', ' myWord = myWordIn;', ' myCount = myCountIn;', ' mySlot = mySlotIn;', ' }', ' /**', ' * Returns a copy of the string in the WordWrapper.', ' *', ' * @return The word contained in this object. Is copied out: no aliasing.', ' */', ' public String extractWord () {', ' return new String (myWord);', ' }', '', ' /**', ' * Returns the count in this WordWrapper.', ' *', ' * @return The count contained in the object.', ' */', ' public int extractCount () {', ' return myCount;', ' }', '', ' public void incCount () {', ' myCount++;', ' }', '', ' public int getCount () {', ' return myCount;', ' }', '', ' public int getPos () {', ' return mySlot;', ' }', '', ' public void setPos (int toWhat) {', ' ', ' mySlot = toWhat;', ' }', '', ' public String toString () {', ' return myWord + " " + mySlot;', ' }', '', ' /**', ' * Comparison operator so we implement the Comparable interface.', ' *', ' * @param w The word wrapper to compare to', ' * @return a positive, zero, or negative value depending upon whether this', ' * word wrapper is less then, equal to, or greater than param w. The', ' * relationship is determied by looking at the counts.', ' */', ' public int compareTo (WordWrapper w) {', " // Check if the count of the current word (myCount) is equal to the count of the word 'w'", ' // If the counts are equal, compare the words lexicographically', ' if (w.myCount == myCount) {', ' return myWord.compareTo(w.myWord);', ' }', " // If the counts are not equal, subtract the count of the current word from the count of the word 'w'", ' return w.myCount - myCount;', ' }', '}', '', '', '/**', ' * This class is used to count occurences of words (strings) in a corpus. Used by', ' * the IndexedDocumentCollection class to find the most frequent words in the corpus.', ' */', '', 'class WordCounter {', ' ', " // this is the map that holds (for each word) the number of times we've seen it", ' // note that Integer is a wrapper for the buitin Java int type', ' private HashMap <String, WordWrapper> wordsIHaveSeen = new HashMap <String, WordWrapper> ();', ' private TreeMap <String, String> ones = new TreeMap <String, String> ();', ' ', ' // the set of words that have been extracted', ' private ArrayList<WordWrapper> extractedWords = new ArrayList <WordWrapper> ();', '', ' /**', ' * Accepts a word and increments the count of the number of times we have seen it.', ' * Note that a deep copy of the param is done (no aliasing).', ' *', ' * @param addMe The string to count', ' */', ' public void insert (String addMe) {', ' if (ones.containsKey (addMe)) {', ' ones.remove (addMe);', ' WordWrapper temp = new WordWrapper (addMe, 1, extractedWords.size ());', ' wordsIHaveSeen.put(addMe, temp);', ' extractedWords.add (temp);', ' }', '', ' // first check to see if the word is alredy in wordsIHaveSeen', ' if (wordsIHaveSeen.containsKey (addMe)) {', ' // if it is, then remove and increment its count', ' WordWrapper temp = wordsIHaveSeen.get (addMe);', ' temp.incCount ();', '', ' // find the slot that we go to in the extractedWords list', " // by sorting the 'extractedWords' list in ascending order", ' for (int i = temp.getPos () - 1; i >= 0 && ', ' extractedWords.get (i).compareTo (extractedWords.get (i + 1)) > 0; i--) {', ' temp = extractedWords.get (i + 1);', ' temp.setPos (i);', ' ', ' WordWrapper temp2 = extractedWords.get (i);', ' temp2.setPos (i + 1);', ' ', ' extractedWords.set (i + 1, temp2);', ' extractedWords.set (i, temp);', ' }', '', ' // in this case it is not there, so just add it', ' } else {', ' ones.put (addMe, addMe);', ' }', ' }', '', ' /**', ' * Returns the kth most frequent word in the corpus so far. A deep copy is returned,', ' * so no aliasing is possible. Returns null if there are not enough words.', ' *', ' * @param k Note that the most frequent word is at k = 0', ' * @return The kth most frequent word in the corpus to date', ' */', ' public String getKthMostFrequent (int k) {', '', ' // if we got here, then the array is all set up, so just return the kth most frequent word', ' if (k >= extractedWords.size ()) {', ' int which = extractedWords.size ();', ' for (String s : ones.navigableKeySet ()) {', ' if (which == k)', ' return s;', ' which++;', ' }', ' return null;', ' } else {', ' // If k is less than the size of the extractedWords list,', ' // return the kth most frequent word from extractedWords', ' return extractedWords.get (k).extractWord ();', ' }', ' }', '}', '/**', ' * A JUnit test case class.', ' * Every method starting with the word "test" will be called when running', ' * the test with JUnit.', ' */', 'public class CounterTester extends TestCase {', '', ' /**', ' * File object to read words from, with buffering.', ' */', ' private FileReader file = null;', ' private BufferedReader reader = null;', '', ' /**', ' * Close file readers.', ' */', ' private void closeFiles() {', ' try {', ' if (file != null) {', ' file.close();', ' file = null;', ' }', ' if (reader != null) {']
[' reader.close();', ' reader = null;', ' }']
[' } catch (Exception e) {', ' System.err.println("Problem closing file");', ' System.err.println(e);', ' e.printStackTrace();', ' }', ' }', '', ' /**', ' * Close files no matter what happens in the test.', ' */', ' protected void tearDown () {', ' closeFiles();', ' }', '', ' /**', ' * Open file and set up readers.', ' *', ' * @param fileName name of file to open', ' * */', ' private void openFile(String fileName) {', ' try {', ' file = new FileReader(fileName);', ' reader = new BufferedReader(file);', ' } catch (Exception e) {', ' System.err.format("Problem opening %s file\\n", fileName);', ' System.err.println(e);', ' e.printStackTrace();', ' fail();', ' }', ' }', '', ' /**', ' * Read the next numWords from the file.', ' *', ' * @param counter word counter to update', ' * @param numWords number of words to read.', ' */', ' private void readWords(WordCounter counter, int numWords) {', ' try {', ' for (int i=0; i<numWords; i++) {', ' if (i % 100000 == 0)', ' System.out.print (".");', ' String word = reader.readLine();', ' if (word == null) {', ' return;', ' }', " // Insert 'word' into the counter.", ' counter.insert(word);', ' }', ' } catch (Exception e) {', ' System.err.println("Problem reading file");', ' System.err.println(e);', ' e.printStackTrace();', ' fail();', ' }', ' }', '', ' /**', ' * Read the next numWords from the file, mixing with queries', ' *', ' * @param counter word counter to update', ' * @param numWords number of words to read.', ' */', ' private void readWordsMixed (WordCounter counter, int numWords) {', ' try {', ' int j = 0;', ' for (int i=0; i<numWords; i++) {', ' String word = reader.readLine();', " // If 'word' is null, it means we've reached the end of the file. So, return from the method.", ' if (word == null) {', ' return;', ' }', ' counter.insert(word);', '', ' // If the current iteration number is a multiple of 10 and greater than 100,000, perform a query of the kth most frequent word.', ' if (i % 10 == 0 && i > 100000) {', ' String myStr = counter.getKthMostFrequent(j++);', ' }', '', ' // rest j once we get to 100', ' if (j == 100)', ' j = 0;', ' }', ' } catch (Exception e) {', ' System.err.println("Problem reading file");', ' System.err.println(e);', ' e.printStackTrace();', ' fail();', ' }', ' }', '', ' /**', ' * Check that a sequence of words starts at the "start"th most', ' * frequent word.', ' *', ' * @param counter word counter to lookup', ' * @param start frequency index to start checking at', ' * @param expected array of expected words that start at that frequency', ' */', ' private void checkExpected(WordCounter counter, int start, String [] expected) {', ' for (int i = 0; i<expected.length; i++) {', ' String actual = counter.getKthMostFrequent(start);', ' System.out.format("k: %d, expected: %s, actual: %s\\n",', ' start, expected[i], actual);', ' assertEquals(expected[i], actual);', ' start++;', ' }', ' }', '', ' /**', ' * A test method.', ' * (Replace "X" with a name describing the test. You may write as', ' * many "testSomething" methods in this class as you wish, and each', ' * one will be called when running JUnit over this class.)', ' */', ' public void testSimple() {', ' System.out.println("\\nChecking insert");', ' WordCounter counter = new WordCounter();', ' counter.insert("pizzaz");', ' // Insert the word "pizza" into the counter twice', ' counter.insert("pizza");', ' counter.insert("pizza");', ' String [] expected = {"pizza"};', ' checkExpected(counter, 0, expected);', ' }', '', ' public void testTie() {', ' System.out.println("\\nChecking tie for 2nd place");', ' WordCounter counter = new WordCounter();', ' counter.insert("panache");', ' counter.insert("pizzaz");', ' counter.insert("pizza");', ' counter.insert("zebra");', ' counter.insert("pizza");', ' counter.insert("lion");', ' counter.insert("pizzaz");', ' counter.insert("panache");', ' counter.insert("panache");', ' counter.insert("camel");', ' // order is important here', ' String [] expected = {"pizza", "pizzaz"};', ' checkExpected(counter, 1, expected);', ' }', '', '', ' public void testPastTheEnd() {', ' System.out.println("\\nChecking past the end");', ' WordCounter counter = new WordCounter();', ' counter.insert("hi");', ' counter.insert("hello");', ' counter.insert("greetings");', ' counter.insert("salutations");', ' counter.insert("hi");', ' counter.insert("welcome");', ' counter.insert("goodbye");', ' counter.insert("later");', ' counter.insert("hello");', ' counter.insert("when");', ' counter.insert("hi");', ' assertNull(counter.getKthMostFrequent(8));', ' }', '', ' public void test100Top5() {', ' System.out.println("\\nChecking top 5 of 100 words");', ' WordCounter counter = new WordCounter();', ' // Open the file "allWordsBig"', ' openFile("allWordsBig");', ' // Read the next 100 words', ' readWords(counter, 100);', ' String [] expected = {"edu", "comp", "cs", "windows", "cmu"};', ' checkExpected(counter, 0, expected);', ' }', '', ' public void test300Top5() {', ' System.out.println("\\nChecking top 5 of first 100 words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter, 100);', ' String [] expected1 = {"edu", "comp", "cs", "windows", "cmu"};', ' checkExpected(counter, 0, expected1);', '', ' System.out.println("Adding 100 more words and rechecking top 5");', ' readWords(counter, 100);', ' String [] expected2 = {"edu", "cmu", "comp", "cs", "state"};', ' checkExpected(counter, 0, expected2);', '', ' System.out.println("Adding 100 more words and rechecking top 5");', ' readWords(counter, 100);', ' String [] expected3 = {"edu", "cmu", "comp", "ohio", "state"};', ' checkExpected(counter, 0, expected3);', ' }', '', ' public void test300Words14Thru19() {', ' System.out.println("\\nChecking rank 14 through 19 of 300 words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' // Read the next 300 words', ' readWords(counter, 300);', ' String [] expected = {"cantaloupe", "from", "ksu", "okstate", "on", "srv"};', ' checkExpected(counter, 14, expected);', ' }', '', ' public void test300CorrectNumber() {', ' System.out.println("\\nChecking correct number of unique words in 300 words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter, 300);', ' /* check the 122th, 123th, and 124th most frequent word in the corpus so far ', ' * and check if the result is not null.', ' */', ' assertNotNull(counter.getKthMostFrequent(122));', ' assertNotNull(counter.getKthMostFrequent(123));', ' assertNotNull(counter.getKthMostFrequent(124));', ' // check the 125th most frequent word in the corpus so far', ' // and check if the result is null.', ' assertNull(counter.getKthMostFrequent(125));', ' }', '', ' public void test300and100() {', ' System.out.println("\\nChecking top 5 of 100 and 300 words with two counters");', ' WordCounter counter1 = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter1, 300);', ' closeFiles();', '', ' WordCounter counter2 = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter2, 100);', '', ' String [] expected1 = {"edu", "cmu", "comp", "ohio", "state"};', ' checkExpected(counter1, 0, expected1);', '', ' String [] expected2 = {"edu", "comp", "cs", "windows", "cmu"};', ' checkExpected(counter2, 0, expected2);', ' }', '', ' public void testAllTop15() {', ' System.out.println("\\nChecking top 15 of all words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' // Read the next 6000000 words', ' readWords(counter, 6000000);', ' String [] expected = {"the", "edu", "to", "of", "and",', ' "in", "is", "ax", "that", "it",', ' "cmu", "for", "com", "you", "cs"};', ' checkExpected(counter, 0, expected);', ' }', '', ' public void testAllTop10000() {', ' System.out.println("\\nChecking time to get top 10000 of all words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter, 6000000);', '', ' for (int i=0; i<10000; i++) {', ' counter.getKthMostFrequent(i);', ' }', ' }', '', ' public void testSpeed() {', ' System.out.println("\\nMixing adding data with finding top k");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWordsMixed (counter, 6000000);', ' String [] expected = {"the", "edu", "to", "of", "and",', ' "in", "is", "ax", "that", "it",', ' "cmu", "for", "com", "you", "cs"};', ' checkExpected(counter, 0, expected);', ' }', '', '}']
[{'reason_category': 'If Body', 'usage_line': 187}, {'reason_category': 'If Body', 'usage_line': 188}, {'reason_category': 'If Body', 'usage_line': 189}]
Global_Variable 'reader' used at line 187 is defined at line 175 and has a Medium-Range dependency. Global_Variable 'reader' used at line 188 is defined at line 175 and has a Medium-Range dependency.
{'If Body': 3}
{'Global_Variable Medium-Range': 2}
infilling_java
CounterTester
182
189
['import junit.framework.TestCase;', 'import java.io.*;', 'import java.util.HashMap;', 'import java.util.TreeMap;', 'import java.util.ArrayList;', '', '/**', ' * This silly little class wraps String, int pairs so they can be sorted.', ' * Used by the WordCounter class.', ' */', 'class WordWrapper implements Comparable <WordWrapper> {', '', ' private String myWord;', ' private int myCount;', ' private int mySlot;', '', ' /**', ' * Creates a new WordWrapper with string "XXX" and count zero.', ' */', ' public WordWrapper (String myWordIn, int myCountIn, int mySlotIn) {', ' myWord = myWordIn;', ' myCount = myCountIn;', ' mySlot = mySlotIn;', ' }', ' /**', ' * Returns a copy of the string in the WordWrapper.', ' *', ' * @return The word contained in this object. Is copied out: no aliasing.', ' */', ' public String extractWord () {', ' return new String (myWord);', ' }', '', ' /**', ' * Returns the count in this WordWrapper.', ' *', ' * @return The count contained in the object.', ' */', ' public int extractCount () {', ' return myCount;', ' }', '', ' public void incCount () {', ' myCount++;', ' }', '', ' public int getCount () {', ' return myCount;', ' }', '', ' public int getPos () {', ' return mySlot;', ' }', '', ' public void setPos (int toWhat) {', ' ', ' mySlot = toWhat;', ' }', '', ' public String toString () {', ' return myWord + " " + mySlot;', ' }', '', ' /**', ' * Comparison operator so we implement the Comparable interface.', ' *', ' * @param w The word wrapper to compare to', ' * @return a positive, zero, or negative value depending upon whether this', ' * word wrapper is less then, equal to, or greater than param w. The', ' * relationship is determied by looking at the counts.', ' */', ' public int compareTo (WordWrapper w) {', " // Check if the count of the current word (myCount) is equal to the count of the word 'w'", ' // If the counts are equal, compare the words lexicographically', ' if (w.myCount == myCount) {', ' return myWord.compareTo(w.myWord);', ' }', " // If the counts are not equal, subtract the count of the current word from the count of the word 'w'", ' return w.myCount - myCount;', ' }', '}', '', '', '/**', ' * This class is used to count occurences of words (strings) in a corpus. Used by', ' * the IndexedDocumentCollection class to find the most frequent words in the corpus.', ' */', '', 'class WordCounter {', ' ', " // this is the map that holds (for each word) the number of times we've seen it", ' // note that Integer is a wrapper for the buitin Java int type', ' private HashMap <String, WordWrapper> wordsIHaveSeen = new HashMap <String, WordWrapper> ();', ' private TreeMap <String, String> ones = new TreeMap <String, String> ();', ' ', ' // the set of words that have been extracted', ' private ArrayList<WordWrapper> extractedWords = new ArrayList <WordWrapper> ();', '', ' /**', ' * Accepts a word and increments the count of the number of times we have seen it.', ' * Note that a deep copy of the param is done (no aliasing).', ' *', ' * @param addMe The string to count', ' */', ' public void insert (String addMe) {', ' if (ones.containsKey (addMe)) {', ' ones.remove (addMe);', ' WordWrapper temp = new WordWrapper (addMe, 1, extractedWords.size ());', ' wordsIHaveSeen.put(addMe, temp);', ' extractedWords.add (temp);', ' }', '', ' // first check to see if the word is alredy in wordsIHaveSeen', ' if (wordsIHaveSeen.containsKey (addMe)) {', ' // if it is, then remove and increment its count', ' WordWrapper temp = wordsIHaveSeen.get (addMe);', ' temp.incCount ();', '', ' // find the slot that we go to in the extractedWords list', " // by sorting the 'extractedWords' list in ascending order", ' for (int i = temp.getPos () - 1; i >= 0 && ', ' extractedWords.get (i).compareTo (extractedWords.get (i + 1)) > 0; i--) {', ' temp = extractedWords.get (i + 1);', ' temp.setPos (i);', ' ', ' WordWrapper temp2 = extractedWords.get (i);', ' temp2.setPos (i + 1);', ' ', ' extractedWords.set (i + 1, temp2);', ' extractedWords.set (i, temp);', ' }', '', ' // in this case it is not there, so just add it', ' } else {', ' ones.put (addMe, addMe);', ' }', ' }', '', ' /**', ' * Returns the kth most frequent word in the corpus so far. A deep copy is returned,', ' * so no aliasing is possible. Returns null if there are not enough words.', ' *', ' * @param k Note that the most frequent word is at k = 0', ' * @return The kth most frequent word in the corpus to date', ' */', ' public String getKthMostFrequent (int k) {', '', ' // if we got here, then the array is all set up, so just return the kth most frequent word', ' if (k >= extractedWords.size ()) {', ' int which = extractedWords.size ();', ' for (String s : ones.navigableKeySet ()) {', ' if (which == k)', ' return s;', ' which++;', ' }', ' return null;', ' } else {', ' // If k is less than the size of the extractedWords list,', ' // return the kth most frequent word from extractedWords', ' return extractedWords.get (k).extractWord ();', ' }', ' }', '}', '/**', ' * A JUnit test case class.', ' * Every method starting with the word "test" will be called when running', ' * the test with JUnit.', ' */', 'public class CounterTester extends TestCase {', '', ' /**', ' * File object to read words from, with buffering.', ' */', ' private FileReader file = null;', ' private BufferedReader reader = null;', '', ' /**', ' * Close file readers.', ' */', ' private void closeFiles() {', ' try {']
[' if (file != null) {', ' file.close();', ' file = null;', ' }', ' if (reader != null) {', ' reader.close();', ' reader = null;', ' }']
[' } catch (Exception e) {', ' System.err.println("Problem closing file");', ' System.err.println(e);', ' e.printStackTrace();', ' }', ' }', '', ' /**', ' * Close files no matter what happens in the test.', ' */', ' protected void tearDown () {', ' closeFiles();', ' }', '', ' /**', ' * Open file and set up readers.', ' *', ' * @param fileName name of file to open', ' * */', ' private void openFile(String fileName) {', ' try {', ' file = new FileReader(fileName);', ' reader = new BufferedReader(file);', ' } catch (Exception e) {', ' System.err.format("Problem opening %s file\\n", fileName);', ' System.err.println(e);', ' e.printStackTrace();', ' fail();', ' }', ' }', '', ' /**', ' * Read the next numWords from the file.', ' *', ' * @param counter word counter to update', ' * @param numWords number of words to read.', ' */', ' private void readWords(WordCounter counter, int numWords) {', ' try {', ' for (int i=0; i<numWords; i++) {', ' if (i % 100000 == 0)', ' System.out.print (".");', ' String word = reader.readLine();', ' if (word == null) {', ' return;', ' }', " // Insert 'word' into the counter.", ' counter.insert(word);', ' }', ' } catch (Exception e) {', ' System.err.println("Problem reading file");', ' System.err.println(e);', ' e.printStackTrace();', ' fail();', ' }', ' }', '', ' /**', ' * Read the next numWords from the file, mixing with queries', ' *', ' * @param counter word counter to update', ' * @param numWords number of words to read.', ' */', ' private void readWordsMixed (WordCounter counter, int numWords) {', ' try {', ' int j = 0;', ' for (int i=0; i<numWords; i++) {', ' String word = reader.readLine();', " // If 'word' is null, it means we've reached the end of the file. So, return from the method.", ' if (word == null) {', ' return;', ' }', ' counter.insert(word);', '', ' // If the current iteration number is a multiple of 10 and greater than 100,000, perform a query of the kth most frequent word.', ' if (i % 10 == 0 && i > 100000) {', ' String myStr = counter.getKthMostFrequent(j++);', ' }', '', ' // rest j once we get to 100', ' if (j == 100)', ' j = 0;', ' }', ' } catch (Exception e) {', ' System.err.println("Problem reading file");', ' System.err.println(e);', ' e.printStackTrace();', ' fail();', ' }', ' }', '', ' /**', ' * Check that a sequence of words starts at the "start"th most', ' * frequent word.', ' *', ' * @param counter word counter to lookup', ' * @param start frequency index to start checking at', ' * @param expected array of expected words that start at that frequency', ' */', ' private void checkExpected(WordCounter counter, int start, String [] expected) {', ' for (int i = 0; i<expected.length; i++) {', ' String actual = counter.getKthMostFrequent(start);', ' System.out.format("k: %d, expected: %s, actual: %s\\n",', ' start, expected[i], actual);', ' assertEquals(expected[i], actual);', ' start++;', ' }', ' }', '', ' /**', ' * A test method.', ' * (Replace "X" with a name describing the test. You may write as', ' * many "testSomething" methods in this class as you wish, and each', ' * one will be called when running JUnit over this class.)', ' */', ' public void testSimple() {', ' System.out.println("\\nChecking insert");', ' WordCounter counter = new WordCounter();', ' counter.insert("pizzaz");', ' // Insert the word "pizza" into the counter twice', ' counter.insert("pizza");', ' counter.insert("pizza");', ' String [] expected = {"pizza"};', ' checkExpected(counter, 0, expected);', ' }', '', ' public void testTie() {', ' System.out.println("\\nChecking tie for 2nd place");', ' WordCounter counter = new WordCounter();', ' counter.insert("panache");', ' counter.insert("pizzaz");', ' counter.insert("pizza");', ' counter.insert("zebra");', ' counter.insert("pizza");', ' counter.insert("lion");', ' counter.insert("pizzaz");', ' counter.insert("panache");', ' counter.insert("panache");', ' counter.insert("camel");', ' // order is important here', ' String [] expected = {"pizza", "pizzaz"};', ' checkExpected(counter, 1, expected);', ' }', '', '', ' public void testPastTheEnd() {', ' System.out.println("\\nChecking past the end");', ' WordCounter counter = new WordCounter();', ' counter.insert("hi");', ' counter.insert("hello");', ' counter.insert("greetings");', ' counter.insert("salutations");', ' counter.insert("hi");', ' counter.insert("welcome");', ' counter.insert("goodbye");', ' counter.insert("later");', ' counter.insert("hello");', ' counter.insert("when");', ' counter.insert("hi");', ' assertNull(counter.getKthMostFrequent(8));', ' }', '', ' public void test100Top5() {', ' System.out.println("\\nChecking top 5 of 100 words");', ' WordCounter counter = new WordCounter();', ' // Open the file "allWordsBig"', ' openFile("allWordsBig");', ' // Read the next 100 words', ' readWords(counter, 100);', ' String [] expected = {"edu", "comp", "cs", "windows", "cmu"};', ' checkExpected(counter, 0, expected);', ' }', '', ' public void test300Top5() {', ' System.out.println("\\nChecking top 5 of first 100 words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter, 100);', ' String [] expected1 = {"edu", "comp", "cs", "windows", "cmu"};', ' checkExpected(counter, 0, expected1);', '', ' System.out.println("Adding 100 more words and rechecking top 5");', ' readWords(counter, 100);', ' String [] expected2 = {"edu", "cmu", "comp", "cs", "state"};', ' checkExpected(counter, 0, expected2);', '', ' System.out.println("Adding 100 more words and rechecking top 5");', ' readWords(counter, 100);', ' String [] expected3 = {"edu", "cmu", "comp", "ohio", "state"};', ' checkExpected(counter, 0, expected3);', ' }', '', ' public void test300Words14Thru19() {', ' System.out.println("\\nChecking rank 14 through 19 of 300 words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' // Read the next 300 words', ' readWords(counter, 300);', ' String [] expected = {"cantaloupe", "from", "ksu", "okstate", "on", "srv"};', ' checkExpected(counter, 14, expected);', ' }', '', ' public void test300CorrectNumber() {', ' System.out.println("\\nChecking correct number of unique words in 300 words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter, 300);', ' /* check the 122th, 123th, and 124th most frequent word in the corpus so far ', ' * and check if the result is not null.', ' */', ' assertNotNull(counter.getKthMostFrequent(122));', ' assertNotNull(counter.getKthMostFrequent(123));', ' assertNotNull(counter.getKthMostFrequent(124));', ' // check the 125th most frequent word in the corpus so far', ' // and check if the result is null.', ' assertNull(counter.getKthMostFrequent(125));', ' }', '', ' public void test300and100() {', ' System.out.println("\\nChecking top 5 of 100 and 300 words with two counters");', ' WordCounter counter1 = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter1, 300);', ' closeFiles();', '', ' WordCounter counter2 = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter2, 100);', '', ' String [] expected1 = {"edu", "cmu", "comp", "ohio", "state"};', ' checkExpected(counter1, 0, expected1);', '', ' String [] expected2 = {"edu", "comp", "cs", "windows", "cmu"};', ' checkExpected(counter2, 0, expected2);', ' }', '', ' public void testAllTop15() {', ' System.out.println("\\nChecking top 15 of all words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' // Read the next 6000000 words', ' readWords(counter, 6000000);', ' String [] expected = {"the", "edu", "to", "of", "and",', ' "in", "is", "ax", "that", "it",', ' "cmu", "for", "com", "you", "cs"};', ' checkExpected(counter, 0, expected);', ' }', '', ' public void testAllTop10000() {', ' System.out.println("\\nChecking time to get top 10000 of all words");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWords(counter, 6000000);', '', ' for (int i=0; i<10000; i++) {', ' counter.getKthMostFrequent(i);', ' }', ' }', '', ' public void testSpeed() {', ' System.out.println("\\nMixing adding data with finding top k");', ' WordCounter counter = new WordCounter();', ' openFile("allWordsBig");', ' readWordsMixed (counter, 6000000);', ' String [] expected = {"the", "edu", "to", "of", "and",', ' "in", "is", "ax", "that", "it",', ' "cmu", "for", "com", "you", "cs"};', ' checkExpected(counter, 0, expected);', ' }', '', '}']
[{'reason_category': 'If Condition', 'usage_line': 182}, {'reason_category': 'If Body', 'usage_line': 183}, {'reason_category': 'If Body', 'usage_line': 184}, {'reason_category': 'If Body', 'usage_line': 185}, {'reason_category': 'If Condition', 'usage_line': 186}, {'reason_category': 'If Body', 'usage_line': 187}, {'reason_category': 'If Body', 'usage_line': 188}, {'reason_category': 'If Body', 'usage_line': 189}]
Global_Variable 'file' used at line 182 is defined at line 174 and has a Short-Range dependency. Global_Variable 'file' used at line 183 is defined at line 174 and has a Short-Range dependency. Global_Variable 'file' used at line 184 is defined at line 174 and has a Short-Range dependency. Global_Variable 'reader' used at line 186 is defined at line 175 and has a Medium-Range dependency. Global_Variable 'reader' used at line 187 is defined at line 175 and has a Medium-Range dependency. Global_Variable 'reader' used at line 188 is defined at line 175 and has a Medium-Range dependency.
{'If Condition': 2, 'If Body': 6}
{'Global_Variable Short-Range': 3, 'Global_Variable Medium-Range': 3}

Dataset Card for Dataset Name

SimCoPilot is a benchmark for evaluating LLMs to perform as a "copilot"-style, interactive coding assistant.

Dataset Details

Dataset Description

SimCoPilot is a benchmark for evaluating LLMs to perform as a "copilot"-style, interactive coding assistant, testing their ability to add and complete code in complex real-world software environments and analyzing how LLMs manage different code dependencies and logic complexities.

  • Curated by: Mingchao Jiang
  • Funded by [optional]: [More Information Needed]
  • Shared by [optional]: [More Information Needed]
  • Language(s) (NLP): Python & Java
  • License: CC BY-NC-ND 4.0

Dataset Sources [optional]

The source code and supporting material can be found in the Github link below

Uses

Program Synthesis, Benchmarking and Research, Development of New Coding Tools

Direct Use

Program Synthesis, Benchmarking and Research, Development of New Coding Tools

Out-of-Scope Use

Commercial Purposes, Infer Personal Information

Dataset Structure

The dataset comprises 11 columns, detailed as follows:

  • task_type: Identifies the task category, with options including infilling_java, completion_java, infilling_python, and completion_python.

  • code_task: Describes the nature of the coding tasks. For Java, the tasks involve advanced academic projects focusing on text processing, data structures (such as AVL, B-tree, M-Tree), and statistical algorithms. Python tasks span from notebook scripts to object-oriented programming, covering areas like linear programming, computer vision, and reinforcement learning.

  • start_line and end_line: Specify the beginning and ending line numbers of the code segment targeted for completion.

  • before, between, and after: Capture the code preceding the target code block, the ground truth of the target code block, and the code following the target block, respectively.

  • reason_categories_output: A collection of dictionaries detailing the usage_line for logical components within the target code block, including elements like If Body, If Condition, Loop Body, etc.

  • horizon_categories_output: Documents the programming constructs such as Global_Variable, Function, Class, along with their define_line and usage_line.

  • reason_freq_analysis and horizon_freq_analysis: These dictionaries tally the occurrences within reason_categories_output and horizon_categories_output, respectively.

Dataset Creation

Curation Rationale

Currently, the most widely-used benchmarks for checking the ability of AI models to perform program synthesis (``AI-for-code'') consist of a detailed English description of a concise, self-contained code to synthesize, as well as a few test cases to test the correctness of the synthesized code. While such benchmarks are useful, they match one particularly narrow use case, where the goal is to synthesize a relatively short, complete, standalone program.

We introduce SimCoPilot, a novel benchmark crafted to simulate the ability of an AI such as a large language model (LLM) to perform as a ``copilot''-style, interactive coding assistant.

Source Data

Source Code

Data Collection and Processing

Emails were sent to faculty and students within the Rice University Computer Science, Electrical Engineering, and Statistics departments, inviting them to contribute Java and Python code private repositories for AI-for-code research. Upon receipt, 1,163 code generation tasks were curated to ensure a diverse and representative sample of real-world code, gathering approximately 11,000 lines of code.

Who are the source data producers?

The dataset includes Java and Python code contributions primarily from students and faculty at Rice University's Computer Science department, Electrical Engineering, and Statistics departments, representing a community of academic programmers and developers.

Annotations [optional]

Annotation process

Each of the 1,163 programming tasks was created from eight Java repositories and seven Python repositories, totaling nearly 11,000 lines of code.

Our team went through these codes, generating both infill and completion tasks.
To create an infill task, the annotator picks a meaningful starting point for the AI-for-code model to begin writing code (at the beginning of the boolean if condition, or at the beginning of the body of a for loop, for example) and then marks the rest of that particular code block for deletion, to be re-created by the AI-for-code model.

In the case of an if condition, the entire boolean predicate would be marked for deletion.
In the case of a for-loop body, the entire body would be marked.

A completion task is created in much the same way, but the code for the remainder of the method or function is marked for deletion.

Who are the annotators?

A team of graduate students from Rice Univerity with medium to advanced programming skill level with 5-10 years experience each.

Personal and Sensitive Information

N/A

Bias, Risks, and Limitations

Sample Bias: Contributions mainly from students and faculty at a single institution (Rice University) could reflect a biased sample of coding styles, proficiency levels, and problem-solving approaches. Overfitting Risks: Models trained on this dataset might perform well on similar academic or controlled environments but may not generalize well to diverse coding tasks outside of these parameters.

Recommendations

Diversifying the Data Sources: Expand the dataset to include code from a broader range of contributors beyond the academic circle of Rice University. This could involve soliciting code from developers in different industries, countries, and cultural backgrounds to enhance the dataset's diversity and representativeness. Cross-Validation with External Datasets: Use external datasets for cross-validation of the AI models trained with this dataset. This helps in assessing the model’s performance and generalizability to other coding environments and tasks.

Citation [optional]

BibTeX:

[More Information Needed]

APA:

[More Information Needed]

Glossary [optional]

[More Information Needed]

More Information [optional]

[More Information Needed]

Dataset Card Authors [optional]

[More Information Needed]

Dataset Card Contact

[More Information Needed]

Downloads last month
2
Edit dataset card