Skip to content

Instantly share code, notes, and snippets.

@tc-imba
Last active June 23, 2020 07:31
Show Gist options
  • Save tc-imba/35ddaec22f2c144bdbaa7c7bdd035acc to your computer and use it in GitHub Desktop.
Save tc-imba/35ddaec22f2c144bdbaa7c7bdd035acc to your computer and use it in GitHub Desktop.
VE280 SU2020 reference parsing
VE280 SU2020 reference parsing
Weng, Paul; Fairness in Reinforcement Learning (2019); AI for Social Good Workshop in IJCAI
Wu, Qitian and Zheng, Hengrui and Gao, Xiaofeng and He, Peng and Weng, Pual and Gao, Han and Chen, Guihai; Dual Graph Attention Networks for Deep Latent Representation of Multifaceted Social Effects in Recommender Systems (2019); The Web Conference
Fairness in Reinforcement Learning - P. Weng - AI for Social Good Workshop in IJCAI 2019
Dual Graph Attention Networks for Deep Latent Representation of Multifaceted Social Effects in Recommender Systems - Q. Wu; H. Zheng; X. Gao; P. He; P. Weng; H. Gao; G. Chen - The Web Conference 2019
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <sstream>
#include <vector>
using namespace std;
class Author {
private:
string firstName, lastName;
public:
explicit Author(const string &name) {
auto pos = name.find(',');
if (pos != string::npos) {
firstName = name.substr(0, pos);
lastName = name.substr(pos + 2);
}
}
friend ostream &operator<<(ostream &os, const Author &author);
};
class Reference {
private:
vector<Author> authors;
string title;
string year;
string conference;
public:
explicit Reference(const string &line) {
istringstream iss(line);
string segment;
// parse authors
getline(iss, segment, ';');
size_t start = 0, end = 0;
while (end != string::npos) {
end = segment.find(" and ", start);
auto name = segment.substr(start, end - start);
authors.emplace_back(name);
start = end + 5;
}
// parse title and year
getline(iss, segment, ';');
start = segment.find_first_not_of(' ');
end = segment.find_last_of(" (");
title = segment.substr(start, end - start - 1);
if (end != string::npos) {
start = end + 1;
end = segment.find_last_of(')');
year = segment.substr(start, end - start);
}
// parse conference
getline(iss, segment, ';');
start = segment.find_first_not_of(' ');
conference = segment.substr(start);
}
friend ostream &operator<<(ostream &os, const Reference &ref);
};
ostream &operator<<(ostream &os, const Reference &ref) {
os << ref.title << " - ";
for (size_t i = 0; i < ref.authors.size(); i++) {
os << ref.authors[i];
if (i != ref.authors.size() - 1) {
os << "; ";
}
}
os << " - " << ref.conference << " " << ref.year;
return os;
}
ostream &operator<<(ostream &os, const Author &author) {
if (!author.lastName.empty()) {
os << author.lastName[0] << ". " << author.firstName;
}
return os;
}
int main() {
ifstream fin("1.txt");
string line;
while (getline(fin, line)) {
cout << Reference(line) << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment