├── .gitignore ├── CMakeLists.txt ├── README.md ├── error.cpp ├── error.h ├── main.cpp ├── res ├── input-big.txt ├── input-small.txt ├── random.txt └── sample-outputs.txt ├── setup.sh ├── wikiscraper.cpp.o └── wikiscraper.h /.gitignore: -------------------------------------------------------------------------------- 1 | vcpkg/ 2 | build/ 3 | build_and_run.sh 4 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0) 2 | project(wikiracer) 3 | 4 | set(CMAKE_CXX_STANDARD 17) 5 | set(CMAKE_CXX_STANDARD_REQUIRED True) 6 | 7 | find_package(cpr CONFIG REQUIRED) 8 | 9 | # adding all files 10 | add_executable(main main.cpp wikiscraper.cpp.o error.cpp) 11 | 12 | target_link_libraries(main PRIVATE cpr::cpr) 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # cs106L-assignment1 2 | Assignment #1 for CS106L. Check out our course website [here](http://cs106l.stanford.edu)! 3 | -------------------------------------------------------------------------------- /error.cpp: -------------------------------------------------------------------------------- 1 | #include "error.h" 2 | #include 3 | 4 | using std::cerr; using std::endl; 5 | using std::string; 6 | 7 | void errorPrint(const std::string& msg, const std::string& leftDisp) { 8 | cerr << leftDisp << msg << endl; 9 | } 10 | -------------------------------------------------------------------------------- /error.h: -------------------------------------------------------------------------------- 1 | #ifndef ERROR_H 2 | #define ERROR_H 3 | 4 | #include 5 | 6 | void errorPrint(const std::string& msg = "", const std::string& leftDisp = "===Error== "); 7 | #endif // ERROR_H 8 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include // for cout, cin 2 | #include // for ifstream 3 | #include // for stringstream 4 | #include // making inputting files easier 5 | #include // containers for the wikiscraper 6 | #include 7 | #include 8 | #include 9 | #include "wikiscraper.h" // wikiscraper methods 10 | 11 | using std::cout; using std::endl; 12 | using std::ifstream; using std::stringstream; 13 | using std::string; using std::vector; 14 | using std::priority_queue; using std::unordered_map; 15 | using std::unordered_set; using std::cin; 16 | 17 | int main() { 18 | // a quick working directory fix to allow for easier filename inputs 19 | auto path = std::filesystem::current_path() / "res/"; 20 | std::filesystem::current_path(path); 21 | std::string filenames = "Available input files: "; 22 | for (const auto& entry : std::filesystem::directory_iterator(path)) { 23 | std::string filename = entry.path().string(); 24 | filename = filename.substr(filename.rfind("/") + 1); 25 | filenames += filename + ", "; 26 | } 27 | // omit last ", ". 28 | cout << filenames.substr(0, filenames.size() - 2) << "." << endl; 29 | 30 | /* Container to store the found ladders in */ 31 | vector> outputLadders; 32 | 33 | cout << "Enter a file name: "; 34 | string filename; 35 | getline(cin, filename); 36 | 37 | /* 38 | TODO: Create a filestream from the filename. 39 | For each pair {start_page, end_page} in the input file, 40 | retrieve the result of findWikiLadder(start_page, end_page) 41 | and append that vector to outputLadders. 42 | */ 43 | // Write code here 44 | 45 | /* 46 | * Print out all ladders in outputLadders. 47 | * We've already implemented this for you! 48 | */ 49 | // Write code here 50 | 51 | return 0; 52 | } 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /res/input-big.txt: -------------------------------------------------------------------------------- 1 | 3 2 | Fruit Strawberry 3 | Malted_milk Gene 4 | Emu Stanford_University 5 | -------------------------------------------------------------------------------- /res/input-small.txt: -------------------------------------------------------------------------------- 1 | 1 2 | Fruit Strawberry 3 | -------------------------------------------------------------------------------- /res/random.txt: -------------------------------------------------------------------------------- 1 | // Feel free to replace this file's contents with whichever pairs of 2 | // words you want to try out! Remember that the first line needs to 3 | // be the number of pairs in this file. Take a look at input-small.txt 4 | // or input-big.txt for an example of the format. 5 | -------------------------------------------------------------------------------- /res/sample-outputs.txt: -------------------------------------------------------------------------------- 1 | These are some sample runs of the program between different pages. 2 | Essentially, the code is printing the ladder (vector) every time 3 | it is dequeued at the top of the while loop. We encourage you to do the same 4 | in your code so that you can compare with this sample output and see if your 5 | code is going towards the high priority links first. The later samples will 6 | be especially useful. 7 | 8 | As always, feel free to reach out to us if you want to discuss anything! 9 | 10 | 11 | ==================================== 12 | Start page: Fruit 13 | End page: Strawberry 14 | ==================================== 15 | 16 | {Fruit} 17 | 18 | Ladder found: 19 | Fruit -> Strawberry 20 | 21 | ==================================== 22 | Start page: Malted_milk 23 | End page: Gene 24 | ==================================== 25 | 26 | {Malted_milk} 27 | {Malted_milk, Enzyme} 28 | {Malted_milk, Enzyme, Transcription_(genetics)} 29 | Duration: 69043786 30 | Ladder found: 31 | Malted_milk -> Enzyme -> Transcription_(genetics) -> Gene 32 | 33 | 34 | ==================================== 35 | Start page: Emu 36 | End page: Stanford 37 | ==================================== 38 | 39 | {Emu} 40 | {Emu, Food_and_Drug_Administration} 41 | {Emu, Food_and_Drug_Administration, Duke_University} 42 | Duration: 91933946 43 | Ladder found: 44 | Emu -> Food_and_Drug_Administration -> Duke_University -> Stanford 45 | 46 | 47 | ==================================== 48 | Start page: Gummy_bear 49 | End page: Marc_Tessier-Lavigne 50 | ==================================== 51 | 52 | {Gummy_bear} 53 | {Gummy_bear, Germany} 54 | {Gummy_bear, Germany, Gerhard_Casper} 55 | 56 | Ladder found: 57 | {Gummy_bear, Germany, Gerhard_Casper, Marc_Tessier-Lavigne} 58 | 59 | 60 | ==================================== 61 | Start page: Albert_Einstein 62 | End page: Scientology 63 | ==================================== 64 | 65 | {Albert_Einstein} 66 | {Albert_Einstein, Relationship_between_religion_and_science} 67 | 68 | Ladder found: 69 | {Albert_Einstein, Relationship_between_religion_and_science, Scientology} 70 | 71 | Note: Wikipedia pages are constantly changing. If you match all outputs for input-big.txt, 72 | don't worry if your ladder is different for this start and end page. 73 | -------------------------------------------------------------------------------- /setup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # download cpr and dependencies 4 | echo 'Now downloading dependencies. Sit tight! This might take a few minutes!' 5 | git clone https://github.com/Microsoft/vcpkg.git 6 | cd vcpkg 7 | ./bootstrap-vcpkg.sh 8 | ./vcpkg integrate install 9 | ./vcpkg install cpr 10 | cd .. 11 | 12 | # set up build directory 13 | echo 'Now building, compiling, and running project.' 14 | mkdir build 15 | cd build 16 | echo '#!/bin/bash 17 | 18 | cmake .. "-DCMAKE_TOOLCHAIN_FILE=../vcpkg/scripts/buildsystems/vcpkg.cmake" 19 | cmake --build .' > build.sh 20 | chmod +x build.sh 21 | 22 | ./build.sh 23 | echo 'Successfully built! now wrapping up' 24 | 25 | cd .. 26 | # confirm and print out confirmation message + instructions. 27 | echo '#!/bin/bash 28 | set -e 29 | 30 | cd build 31 | ./build.sh 32 | cd .. 33 | ./build/main' > build_and_run.sh 34 | chmod +x build_and_run.sh 35 | 36 | echo 'From now on, all you have to do to compile, build, and run your project 37 | is to run "./build_and_run.sh" (without the quotes) and hit enter. 38 | If you run into any issues and want to quit out of the program, press the ctrl and c key 39 | at the same time!' 40 | 41 | -------------------------------------------------------------------------------- /wikiscraper.cpp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/snme/cs106L-assignment1/da967c37ddad11f936fcb59e32e7f51a042bb9c9/wikiscraper.cpp.o -------------------------------------------------------------------------------- /wikiscraper.h: -------------------------------------------------------------------------------- 1 | #ifndef WIKISCRAPER_H 2 | #define WIKISCRAPER_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include // for cout, cin 10 | #include // for ifstream 11 | #include // for stringstream 12 | #include // making inputting files easier 13 | #include 14 | #include 15 | 16 | using std::cout; using std::endl; 17 | using std::ifstream; using std::stringstream; 18 | using std::string; using std::vector; 19 | using std::priority_queue; using std::unordered_map; 20 | using std::unordered_set; using std::cin; 21 | 22 | class WikiScraper { 23 | 24 | public: 25 | WikiScraper(); 26 | 27 | /* 28 | * This method takes the page name of a Wikipedia page and 29 | * returns an unordered_set containing all the links on this page. 30 | */ 31 | std::unordered_set getLinkSet(const std::string& page_name); 32 | 33 | 34 | private: 35 | std::string getPageSource(const std::string& page_name); 36 | std::unordered_map page_cache; 37 | std::unordered_map> linkset_cache; 38 | }; 39 | 40 | vector findWikiLadder(const string& start_page, const string& end_page); 41 | 42 | 43 | #endif // WIKISCRAPER_H 44 | 45 | 46 | --------------------------------------------------------------------------------