id
stringlengths
36
36
text
stringlengths
1
1.25M
2ce7a244-b8ce-4292-ad80-087918b0f8a9
public static void second() { Integer[] array = new Integer[SIZE]; for (int i = 0; i < SIZE; i++) { array[i] = i; } }
7f61bed9-266d-487b-b604-3540f0ce535f
public static void first() { Bean bean = new Bean("abc"); String attribute = bean.getAttribute(); //System.out.println(attribute); }
9742920f-e8ed-4147-9769-08fbe682754e
public static void second() throws Exception { Bean bean = new Bean("abc"); Method method = Bean.class.getMethod("getAttribute"); Object result = method.invoke(bean); //System.out.println(result); }
da2820e4-e790-44c2-831e-892fc4f149d8
private Bean(String attribute) { this.attribute = attribute; }
9c9d7831-e47c-4e75-88f8-a01a389f3a74
public String getAttribute() { return attribute; }
32bc0338-f62d-46b5-946f-7d5faf41f1e9
public static void first() { Bean abc = new Bean("abc"); System.out.println(abc.getAttribute()); System.out.println(abc.getAttribute()); System.out.println(abc.getAttribute()); }
29c5333c-9423-4098-9040-008539b05925
public static void second() { final Bean abc = new Bean("abc"); final String attribute = abc.attribute; System.out.println(attribute); System.out.println(attribute); System.out.println(attribute); }
bb751924-b62b-4225-b9bf-1a39884226ea
public Bean(String attribute) { this.attribute = attribute; }
4f70ba14-cbb8-49e7-8083-1a3db4a36434
public String getAttribute() { return attribute; }
18b160ba-3a15-42e9-b0f0-0ed9911b4b43
public static void run() { System.out.println("Hello, World!"); }
606e22b3-e271-41da-89d0-90a264d46013
@Test public void simpleTest() { long start = System.currentTimeMillis(); MyCode.run(); long stop = System.currentTimeMillis(); System.out.println(stop - start); }
9efe71a3-ea84-4822-8e1f-12c15aa60a81
@Test public void benchmark() throws Exception { Benchmark benchmark = new Benchmark(new Runnable() { @Override public void run() { MyCode.run(); } }); System.out.println(benchmark.getFirst()); System.out.println(benchmark.getStats()); System.out.println(benchmark.getCallResult()); }
ae4fa41b-92ab-42f9-965b-1448b207f3ad
@Override public void run() { MyCode.run(); }
c4bb43db-51c7-4850-921f-749b21051997
public static void main(String[] args) throws Exception { BenchmarkRunner runner = new BenchmarkRunner(); runner.run(new Runnable() { @Override public void run() { try { MyCode4.second(); } catch (Exception e) { e.printStackTrace(); } } }); }
10fd9c08-d59a-40aa-b81a-910a5ef0931b
@Override public void run() { try { MyCode4.second(); } catch (Exception e) { e.printStackTrace(); } }
fc808f96-7f2e-4b8e-bc94-9fb65d7afc1b
public BenchmarkRunner() { classLoading = ManagementFactory.getClassLoadingMXBean(); compilation = ManagementFactory.getCompilationMXBean(); memory = ManagementFactory.getMemoryMXBean(); garbageCollectors = ManagementFactory.getGarbageCollectorMXBeans(); threadBean = ManagementFactory.getThreadMXBean(); }
cf4a0e90-9ab3-4061-94c3-3c4ff0e21018
public void run(Runnable runnable) throws Exception { Stats startStats; long startTime; long endTime; Stats endStats; warmUp(runnable, WARM_UPS); jvmClean(); long[] timings = new long[ITERATIONS]; int skips = 0; for (int i = 0; i < ITERATIONS + DRY_RUNS; i++) { startStats = stats(); startTime = timeNs(); runnable.run(); endTime = timeNs(); endStats = stats(); if (i < DRY_RUNS) { continue; } if (startStats.equals(endStats)) { timings[i - DRY_RUNS] = endTime - startTime; } else { System.out.println(startStats); System.out.println("---------"); System.out.println(endStats); skips++; i--; } } long total = 0; for (long timing : timings) { total += timing; } System.out.println(Arrays.toString(timings)); System.out.println("Skipped : " + skips); System.out.println("Average value : " + total / ITERATIONS); }
9025c44a-acec-457f-9f35-73aac1953e93
private void jvmClean() throws Exception { System.runFinalization(); System.gc(); Thread.sleep(100); }
3255efc8-cd99-4e76-ab0c-7d15a97612e5
private void warmUp(Runnable runnable, int times) { for (int i = 0; i < times; i++) { runnable.run(); } }
79dca385-536c-4907-883c-61d55c73041f
private Stats stats() { Stats stats = new Stats(); stats.setLoadedClassCount(classLoading.getTotalLoadedClassCount()); stats.setCompilationTime(compilation.getTotalCompilationTime()); long[] collectionCount = new long[garbageCollectors.size()]; for (int i = 0, garbageCollectorsSize = garbageCollectors.size(); i < garbageCollectorsSize; i++) { GarbageCollectorMXBean collector = garbageCollectors.get(i); collectionCount[i] = collector.getCollectionCount(); } stats.setGC(collectionCount); return stats; }
259c5021-d76c-474a-809d-d9f3a572f85d
private long timeNs() { return threadBean.getCurrentThreadCpuTime(); }
8679a8ef-7f2c-4d4c-bd28-fef8dcb3a187
public void setLoadedClassCount(long loadedClassCount) { this.loadedClassCount = loadedClassCount; }
f6fe18ce-3854-462f-8eb5-e94ae15dfd17
public void setCompilationTime(long compilationTime) { this.compilationTime = compilationTime; }
061c2718-af97-45e1-a949-ed588a1794ae
public void setGC(long[] collectionCount) { this.collectionCount = collectionCount; }
e7b3e7de-d6f0-4654-836f-4238da64b1cf
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Stats stats = (Stats) o; if (compilationTime != stats.compilationTime) return false; if (loadedClassCount != stats.loadedClassCount) return false; if (!Arrays.equals(collectionCount, stats.collectionCount)) return false; return true; }
f4d7d7c9-86b3-4ba6-b7f6-9afd6fe0bbeb
@Override public int hashCode() { int result = (int) (loadedClassCount ^ (loadedClassCount >>> 32)); result = 31 * result + (int) (compilationTime ^ (compilationTime >>> 32)); result = 31 * result + (collectionCount != null ? Arrays.hashCode(collectionCount) : 0); return result; }
334f1942-360d-4e6e-8313-28be2ca4f7c5
@Override public String toString() { return "Stats{" + "loadedClassCount=" + loadedClassCount + ", compilationTime=" + compilationTime + ", collectionCount=" + Arrays.toString(collectionCount) + '}'; }
f6ee4176-c65f-42c1-8157-43f219215df5
public Bat(int x, int y, int width, int height) { super(x, y, width, height); this.setSpeed(3f); this.setDx(0); this.setDy(0); hitbox = new Rectangle(this.x, this.y, this.width, this.height); }
b08b292e-22e0-4445-9541-252af4e51c75
public void draw(){ glBegin(GL_QUADS); glVertex2i(this.getX(), this.getY()); glVertex2i(this.getX()+this.getWidth(), this.getY()); glVertex2i(this.getX()+this.getWidth(), this.getY()+this.getHeight()); glVertex2i(this.getX(), this.getY()+this.getHeight()); glEnd(); }
f20e8ba9-3ca9-4b53-aeba-6cbfce8d56d6
public void update(int dx, int dy){ this.x += dx * this.speed; this.y += dy * this.speed; }
f83cced8-39a1-46c1-a830-3d9e8fb214fe
public Ball(int x, int y, int width, int height) { super(x, y, width, height); this.setSpeed(2f); this.setDx(0); this.setDy(0); hitbox = new Rectangle(this.x, this.y, this.width, this.height); }
74692a06-3dd9-46de-9135-a2863004ec9b
public void draw(){ if(isAlive){ glBegin(GL_QUADS); glVertex2i(this.getX(), this.getY()); glVertex2i(this.getX()+this.getWidth(), this.getY()); glVertex2i(this.getX()+this.getWidth(), this.getY()+this.getHeight()); glVertex2i(this.getX(), this.getY()+this.getHeight()); glEnd(); } }
2928d954-e35a-4e4d-bca4-82bf9a254261
public void update(int dx, int dy){ this.x += dx * this.speed; this.y += dy * this.speed; }
d500775e-b17b-486e-bb7f-f5aedf595ab4
public void spawn(int x, int y,int dx, int dy){ isAlive = true; this.x = x; this.y = y; this.dx = dx; this.dy = dy; }
ec77498e-c3f7-4280-af3b-1f78f2468a46
public void kill(){ isAlive = false; this.dx = 0; this.dy = 0; this.x = 0; this.y = 0; }
aa917ffb-bbc1-4735-8e52-6ec7b0e956e7
public boolean isAlive(){ return this.isAlive; }
10d05d64-0881-453e-abff-8e639021cfda
public Line(int x, int z, int width) { this.x = x; this.y = z; this.width = width; }
27144298-66dd-4f94-8d9a-7a8969f7f3dc
public void draw() { glBegin(GL_QUADS); glVertex2i(x, 0); glVertex2i(x + width, 0); glVertex2i(x + width, y); glVertex2i(x, y); glEnd(); }
ca8cfca8-bf26-4501-9919-b0d459ae4370
public int getX() { return x; }
e46f26bc-f594-466a-9014-edba189086a9
public int getY() { return y; }
449f872a-f17e-47c6-af6f-3d1b004bbe70
public int getWidth() { return width; }
d8fbd8c1-fa63-4cc5-89f6-a30aa36ad758
public Entity(int x, int y, int width, int height){ this.x = x; this.y = y; this.width = width; this.height = height; dx = 0; dy = 0; hitbox = new Rectangle(this.x, this.y, this.width, this.height); }
2e899d15-f8ec-4e36-af2a-7d8002f67846
public int getX() { return x; }
f81613b9-50e3-44e9-9434-5f0ee207d277
public int getY() { return y; }
dfba0431-d56b-41fb-8d97-1294caf5529b
public int getWidth() { return width; }
7f59cf7d-2d6d-4e15-9ff6-ae246b5ce9b7
public int getHeight() { return height; }
782f797a-9037-4375-9209-3119501b15bf
public float getSpeed() { return speed; }
76a9a540-9689-4e95-aa10-3000991e771a
public int getDx() { return dx; }
6d8b7de1-895f-4b39-a600-8f09f2622e8b
public int getDy() { return dy; }
99158d03-bbe9-4501-b22c-6a73ae8d5216
public Rectangle getHitbox() { return hitbox; }
e39b4dd1-f450-451f-b90a-df2c6c1a38c6
public void setX(int x) { this.x = x; }
d704cd83-eeb5-465a-a2e7-ee620cf4b3e5
public void setY(int y) { this.y = y; }
781dc6e3-3b0e-40cf-8573-958e1f3a0a46
public void setWidth(int width) { this.width = width; }
f1a52eb1-5741-4a6a-8946-f9b817eca03a
public void setHeight(int height) { this.height = height; }
0835acf8-594d-4c44-8f9b-ede814d3b97c
public void setSpeed(float speed) { this.speed = speed; }
75ab25fd-e183-4023-b0d0-dd99c02efffb
public void setDx(int dx) { this.dx = dx; }
23165b0e-54c7-4de0-a2e0-8b371af1c3de
public void setDy(int dy) { this.dy = dy; }
56fb01c1-2b61-4431-a76d-4ccb6669da13
public void setHitbox(Rectangle hitbox) { this.hitbox = hitbox; }
6f66751f-4bd5-45d6-bf67-ca4bb5e0d4b2
public boolean isIn(Entity other) { if (this.hitbox.intersects(other.getHitbox())){ return true; } else { return false; } }
f92dafc0-7dd6-4201-87c2-41d2055e845e
public void update(){ this.x += dx * this.speed; this.y += dy * this.speed; }
204cca64-c341-490a-b609-01fd3c14055a
public abstract void draw();
0253f449-54ac-436b-9fef-2a734b50874a
public GodBat(Bat bat, Ball ball) { this.bat = bat; this.ball = ball; this.start(); }
d8cd7a05-0e36-488f-8ba7-10266283305f
@Override public void run() { while (ball.isAlive()) { if (ball.getY() > bat.getY() && bat.getDy() != 1) { bat.update(0, 1); } else if (bat.getDy() != -1) { bat.update(0, -1); } } }
2b7d6a52-bafd-4868-aa4f-45cf591edb91
public GameController(Game game) { this.game = game; this.logger = Logger.getLogger(Game.class); logger.info("OS : " + System.getProperty("os.name") + " (" + System.getProperty("os.version") + ") " + System.getProperty("os.arch")); logger.info("Java : " + System.getProperty("java.version") + " / " + System.getProperty("java.vendor")); logger.info("Started new Game v" + VERSION + " with OpenGL : " + GL_VERSION); game.launch(); }
7df9b559-d433-4958-824e-aa5f44795fb2
public void switchTo(GameState state) { this.state = state; }
926cdc89-e080-40d8-910f-be45a8c15efa
public static void main(String[] args) { Game game = new Game(640, 480); GameController app = new GameController(game); app.switchTo(GameState.MENU); }
925bb0d1-994c-4f18-a6b4-2279bc4f97c7
public static Logger getLogger() { return Logger.getLogger(Game.class); }
701c364f-9bb1-4e7c-9067-f6c22bf5bdd5
public Game(int width, int height) { WIDTH = width; HEIGHT = height; }
32315708-2864-4780-b7ab-a5432099194b
public int inverse(int tar){ if(tar < 0) return +tar; else if(tar > 0) return -tar; return 0; }
45ad8fe9-f3a6-477e-bfd2-9ad10f5c9bd6
public void setUpGl() { /** Create & Show a display frame * @since v0.1 * @see Game#launch() */ try { Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT)); Display.setTitle("Pong, You vs God"); Display.create(); } catch (LWJGLException e) { e.printStackTrace(); Display.destroy(); System.exit(1); } /* Init */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, WIDTH, HEIGHT, 0, 1, -1); glMatrixMode(GL_MODELVIEW); logger.info("Displayed the Game frame"); }
ab59c400-d711-4799-a9d4-55fa518c3693
public void setUpObjects() { bat.draw(); god_bat.draw(); ball.spawn(WIDTH / 2 + 15 / 2, HEIGHT / 2 - 12 / 2,-1,-1); ball.setDx(-1); ball.setDy(1); ai1 = new GodBat(god_bat, ball); ai2 = new GodBat(bat, ball); logger.info("Created the objects"); }
56bccccb-ac14-4f11-8922-149e5e22190e
private void input() { if(Keyboard.isKeyDown(Keyboard.KEY_DOWN) && (bat.getY() + bat.getHeight()) <= HEIGHT){ bat.update(0,1); } if(Keyboard.isKeyDown(Keyboard.KEY_UP) && bat.getY() >= 0){ bat.update(0,-1); } if(Keyboard.isKeyDown(Keyboard.KEY_NUMPAD5) && (god_bat.getY() + god_bat.getHeight()) <= HEIGHT){ god_bat.update(0,1); } if(Keyboard.isKeyDown(Keyboard.KEY_NUMPAD8) && god_bat.getY() >= 0){ god_bat.update(0,-1); } if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) { ball.spawn(WIDTH / 2 + 15 / 2, HEIGHT / 2 - 12 / 2,-1,-1); } }
b838b778-0fd2-43b3-b696-4e98a50cefa5
public void update() { input(); if(ball.getY() < 1) ball.setDy(1); if(ball.getY() > HEIGHT - 1) ball.setDy(-1); if(bat.getX() + bat.getWidth() >= ball.getX() && bat.getX() <= ball.getX() && bat.getY() <= ball.getY() && bat.getY()+bat.getHeight() >= ball.getY()){ ball.setDx(1); if(ball.getY() < HEIGHT / 2){ ball.setDy(inverse(ball.getDy())); } } if(god_bat.getX()-god_bat.getWidth() <= ball.getX() && god_bat.getY() <= ball.getY() && god_bat.getY()+god_bat.getHeight() >= ball.getY()){ ball.setDx(-1); if(ball.getY() < HEIGHT / 2){ ball.setDy(inverse(ball.getDy())); } } if(ball.getX() < -12) ball.kill(); if(ball.getX() > WIDTH + 12) ball.kill(); ball.update(); bat.draw(); god_bat.draw(); ball.draw(); }
2a05f967-fbab-4383-a5a4-0d9115d9cc35
public void launch() { setUpGl(); setUpObjects(); logger.info("Launching the game (" + WIDTH + "x" + HEIGHT + ")"); while(!Display.isCloseRequested()) { glClear(GL_COLOR_BUFFER_BIT); update(); Display.update(); Display.sync(60); } Display.destroy(); logger.info("Exiting the game with 0 exit code"); System.exit(0); }
91aadd84-c6bb-4746-a3a5-6b5bba09c237
public Parser(String inFile) { try { this.in = new BufferedReader(new InputStreamReader(new FileInputStream(inFile), "UTF-8"), BUFFER_SIZE); } catch (FileNotFoundException | UnsupportedEncodingException e) { e.printStackTrace(); } this.state = Parser.States.s_del; this.event = Parser.Events.ev_NONE; this.event_changed = false; this.current_tag = ""; this.current_char = 0; this.tags = new Stack<String>(); this.charsRead = 0; }
b09f6136-1315-4dea-a25b-563b03fc3a3f
public Parser.Events parse() { int c = 0; boolean ret = true; try { while ((c = in.read()) != -1 && ret) { this.charsRead += 1; ret = this.updateState(c); if (ret) { updateEvent(); ret = updateStrings(c); } if (ret && this.event_changed) { return this.event; } } } catch (IOException e) { e.printStackTrace(); } if (!ret) { System.err.println("Error at position [" + this.charsRead + "] while parsing the input file."); while (this.tags.size() != 0) { System.out.println(this.tags.pop()); } System.out.println(this.current_tag); } return Parser.Events.ev_NONE; }
225eade3-e617-4db8-8414-20784cf2cbb0
private boolean updateState(int i) { int c = (char)i; switch(this.state) { case s_del: this.state = ('<' == c) ? Parser.States.s_small : (('\n' == c || ' ' == c) ? Parser.States.s_del : Parser.States.s_error); break; case s_small: this.state = ('/' == c) ? Parser.States.s_end_tag_slash : (('!' != c) ? Parser.States.s_tag : Parser.States.s_doctype); break; case s_tag: this.state = ('>' == c) ? Parser.States.s_tag_gt : ((' ' == c) ? Parser.States.s_tag_space : (('/' == c) ? Parser.States.s_tag_slash : Parser.States.s_tag)); break; case s_tag_gt: this.state = ('<' == c) ? Parser.States.s_small : (('\n' == c || ' ' == c) ? Parser.States.s_tag_delim : Parser.States.s_text); break; case s_tag_delim: this.state = ('\n' == c || ' ' == c) ? Parser.States.s_tag_delim : (('<' == c) ? Parser.States.s_small : Parser.States.s_text); break; case s_text: this.state = ('<' == c) ? Parser.States.s_end_tag_sm : Parser.States.s_text; break; case s_end_tag_sm: this.state = ('/' == c) ? Parser.States.s_end_tag_slash : Parser.States.s_error; break; case s_end_tag_slash: this.state = Parser.States.s_end_tag; break; case s_end_tag: this.state = ('>' == c) ? Parser.States.s_end_tag_gt : Parser.States.s_end_tag; break; case s_end_tag_gt: this.state = ('<' == c) ? Parser.States.s_small : (('\n' == c || ' ' == c) ? Parser.States.s_del : Parser.States.s_error); break; case s_doctype: this.state = ('>' == c) ? Parser.States.s_doctype_gt : Parser.States.s_doctype; break; case s_doctype_gt: this.state = ('<' == c) ? Parser.States.s_small : (('\n' == c || ' ' == c) ? Parser.States.s_del : Parser.States.s_error); break; case s_tag_space: this.state = (' ' == c) ? Parser.States.s_tag_space : (('/' == c) ? Parser.States.s_tag_slash : Parser.States.s_tag_att); break; case s_tag_att: this.state = ('/' == c) ? Parser.States.s_tag_slash : (('>' == c) ? Parser.States.s_tag_gt : Parser.States.s_tag_att); break; case s_tag_slash: this.state = ('>' == c) ? Parser.States.s_tag_no_end_gt : Parser.States.s_tag_att; break; case s_tag_no_end_gt: this.state = ('<' == c) ? Parser.States.s_small : (('\n' == c || ' ' == c) ? Parser.States.s_del : Parser.States.s_error); break; default: System.err.println("Unknown state" + this.state); return false; } if (Parser.States.s_error != this.state) { return true; } else { return false; } }
06d80c2d-2a66-476a-a818-d6c86172531a
private void updateEvent() { this.event_changed = false; switch(this.state) { case s_del: break; case s_small: break; case s_tag: break; case s_tag_gt: this.event = Parser.Events.ev_TAG; this.event_changed = true; break; case s_tag_delim: break; case s_text: this.event = Parser.Events.ev_TEXT_CHAR; this.event_changed = true; break; case s_end_tag_sm: break; case s_end_tag_slash: break; case s_end_tag: break; case s_end_tag_gt: this.event = Parser.Events.ev_END_TAG; this.event_changed = true; break; case s_doctype: break; case s_doctype_gt: break; case s_tag_space: break; case s_tag_att: break; case s_tag_slash: break; case s_tag_no_end_gt: this.event = Parser.Events.ev_TAG_NO_END; this.event_changed = true; break; default: System.err.println("Unknown state" + this.state); } }
44059a44-d2ad-442c-b29a-cdc628d7823b
private boolean updateStrings(int i) { int c = (char) i; switch(this.state) { case s_del: break; case s_small: this.current_tag = ""; break; case s_tag: this.current_tag += (char)c; break; case s_tag_gt: this.tags.push(this.current_tag); break; case s_tag_delim: break; case s_text: this.current_char = c; break; case s_end_tag_sm: this.current_tag = ""; break; case s_end_tag_slash: break; case s_end_tag: this.current_tag += (char)c; break; case s_end_tag_gt: if (0 >= this.tags.size()) { System.err.println("Ending tag error!"); return false; } String eTag = this.tags.pop(); if (0 != this.current_tag.compareTo(eTag)) { System.err.println("Ending tag error!"); return false; } break; case s_doctype: break; case s_doctype_gt: break; case s_tag_space: break; case s_tag_att: break; case s_tag_slash: break; case s_tag_no_end_gt: this.current_tag += (char)c; break; default: System.err.println("Unknown state" + this.state); return false; } return true; }
91c070fd-c597-4c53-97de-c8a584c745f9
public String getLastTag() { return this.current_tag; }
2264a1f0-37c7-4fc4-b2a2-6331a1a4684b
public int getLastChar() { return this.current_char; }
93de0aad-119f-4692-b052-d241288f66a4
public Trie() { this.is_word = false; this.l = new Trie[26]; }
e5395299-e0b4-4d09-889e-38a6becb89e8
public boolean addWord(String s) { char w[] = new char[s.length()]; s.getChars(0, w.length, w, 0); return this.addWord(w, 0, w.length); }
f75eedad-9f0a-45b9-bd62-ae62b1dfd17b
public boolean addWord(char w[], int start, int len) { int pos = -1; boolean posAdded = false; if (0 == len) { return true; } if (false == isASCIIAlpha(w[start])) { return false; } pos = (w[start] | 32) - 97; if (null == this.l[pos]) { this.l[pos] = new Trie(); posAdded = true; } if (1 == len) { this.l[pos].is_word = true; return true; } boolean rc = this.l[pos].addWord(w, start + 1, len - 1); if (false == rc && posAdded) { this.l[pos] = null; } return rc; }
d1fff07c-f1b6-4628-9c39-1f95fc64ec7a
public boolean searchWord(String s) { char w[] = new char[s.length()]; s.getChars(0, w.length, w, 0); return this.searchWord(w, 0, w.length); }
e60af9e3-bf99-471e-abe4-73db5712d76b
public boolean searchWord(char w[], int start, int len) { int pos = -1; if (0 == len) { return false; } if (false == isASCIIAlpha(w[start])) { return false; } pos = (w[start] | 32) - 97; if (null == this.l[pos]) { return false; } if (1 == len) { return this.l[pos].is_word; } return this.l[pos].searchWord(w, start + 1, len - 1); }
6a822862-b6a1-4ccb-a4c9-672f64e2ad4e
public static boolean isASCIIAlpha(int c) { if (('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z')) { return true; } return false; }
98c7cb77-a7e2-4d40-9283-00d18883d44d
public static boolean isAlphanumeric(int c) { if ((Character.isLetterOrDigit(c) == false) && (c != ' ')) { return false; } return true; }
290ef0ef-c9b8-4968-82ec-884427eba156
private static boolean checkFile(String path) { boolean exists = false; File f = new File(path); exists = f.isFile() && f.canRead(); if (false == exists) { System.err.println("Path '" + path + "' does not exist or it is not readable."); } return exists; }
efea2dcf-44a0-43d6-9b66-37c671cacaf5
public static void main(String[] args) { Processor p = null; if (0 == args[0].compareTo("--help")) { System.out.println("Extracts the pages from a wikipedia dump xml that contains a specific word in title.\n" + "This is a tool intended to be used for natural language processing and a helper for WikipediaESA tool.\n"); System.out.println("WikiParser input_xml words_file output_xml\n"); System.out.println("input_xml path to the wikipedia dump xml or another xml that has the same structure\n" + " from which the pages will be extracted"); System.out.println("words_file path to a file that contains the terms separated on each line used for\n" + " selecting the pages. The words must contain only english letters."); System.out.println("output_xml path to a file that will be created and will contain the extracted pages\n" + " in a structure similar to the wikipedia dump xml for training WikipediaESA"); System.out.println("output_txt path to a file that will be created and will contain the extracted pages\n" + " oen on each row for LSA training from 'airhead-research'"); System.exit(0); } if (4 > args.length) { System.err.println("Too few arguments. Check '--help' command for more information."); System.exit(1); } if (false == checkFile(args[0]) || false == checkFile(args[1])) { System.err.println("Command line check failed. Check '--help' command for more information."); System.exit(1); } try { p = new Processor(args[0], args[1], args[2], args[3]); p.process(); } catch (Exception e) { e.printStackTrace(); } }
dd66ca79-ae7a-4cd0-8718-927643a5fa1e
public Processor(String inFile, String wordsFile, String outFileXml, String outFileTxt) throws Exception { this.p = new Parser(inFile); this.t = new Trie(); this.currentTitle = ""; this.inTitle = false; this.foundTitle = false; this.inText = false; this.firstLineOfText = true; if (null != outFileXml && 0 < outFileXml.length()) { this.outXmlFd = openFileForWrite(outFileXml); } if (null != outFileTxt && 0 < outFileTxt.length()) { this.outTxtFd = openFileForWrite(outFileTxt); } this.loadTrie(wordsFile); }
bbc6cfd4-e374-455d-b141-61e286ffedae
private BufferedWriter openFileForWrite(String path) { BufferedWriter fd = null; try { fd = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path), "UTF-8")); } catch (IOException e) { e.printStackTrace(); } return fd; }
6ed82545-3af6-4c04-8108-bb602bbae0fb
private void loadTrie(String f) { BufferedReader br = null; String line = ""; try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } try { while ((line = br.readLine()) != null) { this.t.addWord(line); } br.close(); } catch (IOException e) { e.printStackTrace(); } }
2ded3623-7a04-47f1-9aeb-215220fc6000
private boolean isTitleGood() { boolean titleGood = false; int start = -1; for (int i = 0; i < this.currentTitle.length(); i++) { if (!Trie.isASCIIAlpha(this.currentTitle.charAt(i))) { if (-1 != start) { String word = this.currentTitle.substring(start, i); titleGood = this.t.searchWord(word); if (titleGood) { return true; } start = -1; } } else { start = (-1 == start) ? i : start; } } if (-1 != start) { return this.t.searchWord(this.currentTitle.substring(start, this.currentTitle.length())); } return false; }
2b8b07b7-a6ed-4cd6-b440-7b03bf036b91
private void startTag() { String tag = this.p.getLastTag(); this.firstLineOfText = true; if (0 == "page".compareTo(tag)) { } if (0 == "title".compareTo(tag)) { this.currentTitle = ""; this.inTitle = true; } if (0 == "text".compareTo(tag)) { this.inText = true; if (this.foundTitle) { try { this.outXmlFd.write("\n <text>\n"); } catch (IOException e) { e.printStackTrace(); } } } }
d1c87ad6-eebc-419c-a3b3-236ca12857de
private void stopTag() { String tag = this.p.getLastTag(); if (0 == "page".compareTo(tag)) { if (true == this.foundTitle) { try { this.outXmlFd.write("\n </page>"); this.outTxtFd.write("\n"); } catch (IOException e) { e.printStackTrace(); } } this.foundTitle = false; } if (0 == "title".compareTo(tag)) { this.inTitle = false; if (this.isTitleGood()) { this.foundTitle = true; try { this.outXmlFd.write("\n <page>\n <title>" + currentTitle + "</title>"); } catch (IOException e) { e.printStackTrace(); } } } if (0 == "text".compareTo(tag)) { this.inText = false; if (true == this.foundTitle) { try { this.outXmlFd.write("\n </text>"); } catch (IOException e) { e.printStackTrace(); } } } }
f411c7ac-3f17-416d-a847-227c3764fc2c
private void chars() { if (true == this.inTitle) { this.currentTitle += (char)this.p.getLastChar(); } if (true == this.foundTitle && true == this.inText) { int c = p.getLastChar(); try { /* write the text to the xml file */ this.outXmlFd.write(c); } catch (IOException e) { e.printStackTrace(); } try { /* write the title of the page */ if (this.firstLineOfText) { this.outTxtFd.write(this.currentTitle); this.outTxtFd.write(" "); } /* write the text to the individual file */ if ('\n' != c && '\r' != c) { if (Trie.isAlphanumeric(c)) { this.outTxtFd.write(c); } else { this.outTxtFd.write(" "); } } } catch (IOException e) { e.printStackTrace(); } } this.firstLineOfText = false; }
65be0e0e-a0b1-414f-bc4a-19540e8369bf
public void process() { Parser.Events event = Parser.Events.ev_TAG; try { outXmlFd.write("<mediawiki>"); } catch (IOException e) { e.printStackTrace(); } while (Parser.Events.ev_NONE != event) { event = this.p.parse(); switch(event) { case ev_NONE: break; case ev_TAG: this.startTag(); break; case ev_TEXT_CHAR: this.chars(); break; case ev_END_TAG: this.stopTag(); break; case ev_TAG_NO_END: break; } } try { this.outXmlFd.write("</mediawiki>"); outXmlFd.close(); outTxtFd.close(); } catch (IOException e) { e.printStackTrace(); } }
f0177aaf-f32a-4165-ab24-7854b56655a2
public void testInvalidWord() { Trie t = new Trie(); assertEquals(false, t.addWord("georgelĈ")); assertEquals(false, t.addWord("georĈgel")); assertEquals(false, t.addWord("Ĉgeorgel")); assertEquals(false, t.addWord("Ĉ")); }
2b10039a-79c5-48c4-8989-01555d45d80d
public void testGoodWord(){ Trie t = new Trie(); assertEquals(true, t.addWord("georgel")); }