Skip to content

Instantly share code, notes, and snippets.

@myfavouritekk
Last active November 9, 2015 05:02
Show Gist options
  • Save myfavouritekk/a576337c33dca6ff04cc to your computer and use it in GitHub Desktop.
Save myfavouritekk/a576337c33dca6ff04cc to your computer and use it in GitHub Desktop.
ELEG 3101 Final Project Judging Program

ELEG 3101 (2015-2016) Final Project Judging Program

Installation or Dependencies

  • edit_dist.cpp:
$ g++ edit_dist.cpp -o edit_dist
  • edit_dist.py:
$ pip install python-Levenshtein

Usage

  • edit_dist.cpp:
$ ./edit_dist gt.txt sub.txt
  • edit_dist.py:
$ python edit_dist.py gt.txt sub.txt

Output Example

File: sub.txt.
Edit distance 88
# correct 1150
# add 1
# sub 6
# del 81
Total score: 710

The Total score is calculated by #correct - 5 * (#add + #sub + $del).

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstring>
#include <cstdio>
using namespace std;
int main(int argc, char const *argv[])
{
ifstream gt_file, sub_file;
gt_file.open(argv[1]);
sub_file.open(argv[2]);
string a((std::istreambuf_iterator<char>(gt_file)),
std::istreambuf_iterator<char>());
string b((std::istreambuf_iterator<char>(sub_file)),
std::istreambuf_iterator<char>());
const int n = a.size();
const int m = b.size();
vector<vector<int> > f(n+1, vector<int>(m+1, 0));
for (int i = 0; i <= n; ++i) f[i][0] = i;
for (int j = 0; j <= m; ++j) f[0][j] = j;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
int w = (a[i - 1] == b[j - 1]) ? 0 : 1;
f[i][j] = f[i - 1][j - 1] + w;
f[i][j] = min(f[i][j], f[i][j - 1] + 1);
f[i][j] = min(f[i][j], f[i - 1][j] + 1);
}
}
int correct_count = 0;
int add_count = 0;
int sub_count = 0;
int del_count = 0;
int i = n, j = m;
while (i > 0 && j > 0) {
// cout << a.substr(0, i) << " " << b.substr(0, j) << " ";
if (a[i - 1] == b[j - 1] && f[i][j] == f[i - 1][j - 1]) {
// cout << "correct" << endl;
--i; --j;
++correct_count;
} else if (a[i - 1] != b[j - 1] && f[i][j] == f[i - 1][j - 1] + 1) {
// cout << "sub" << endl;
--i; --j;
++sub_count;
} else if (f[i][j] == f[i - 1][j] + 1) {
// cout << "del" << endl;
--i;
++del_count;
} else {
// cout << "add" << endl;
--j;
++add_count;
}
}
if (i > 0) del_count += i;
if (j > 0) add_count += j;
cout << "File: " << argv[2] << endl;
cout << "Edit distance " << f[n][m] << endl;
cout << "# correct " << correct_count << endl;
cout << "# add " << add_count << endl;
cout << "# sub " << sub_count << endl;
cout << "# del " << del_count << endl;
cout << "Total score: " <<
correct_count - 5 * (add_count + sub_count + del_count) << endl;
gt_file.close();
sub_file.close();
return 0;
}
#!/usr/bin/env python
import editdistance as ed
import Levenshtein
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('gt_file')
parser.add_argument('sub_file')
args = parser.parse_args()
with open(args.gt_file) as f:
gt_str = f.read()
with open(args.sub_file) as f:
sub_str = f.read()
ops = Levenshtein.editops(gt_str, sub_str)
num_d = len([op for op in ops if op[0] == 'delete'])
num_a = len([op for op in ops if op[0] == 'insert'])
num_r = len([op for op in ops if op[0] == 'replace'])
num_right = len(gt_str) - num_r - num_d
score = num_right - (num_d + num_a + num_r) * 5
print "File: {}.".format(args.sub_file)
print "Edit distance {}".format(Levenshtein.distance(gt_str, sub_str))
print "# correct {}".format(num_right)
print "# add {}".format(num_a)
print "# sub {}".format(num_r)
print "# del {}".format(num_d)
print "Total score: {}".format(score)
Welcome to the home of Biomedical Engineering at CUHK. We advance cutting-edge engineering for health enhancement and patient care.
You can find at this site information about our BEng major programme in BME and its minor, our double degree programme with business administration, as well as our MSc and articulated MPhil or PhD programmes in BME.
All our BME programmes have been launched by the Faculty of Engineering in close collaboration with the Faculty of Medicine as co-owned interdisciplinary programmes. Academic contributions are provided by various departments across the two Faculties. Administrative supports are provided by the Department of Electronic Engineering for our undergraduate and MPhil/PhD programmes, and by the Department of Mechanical & Automation Engineering for our MSc programme.
Please meet our BME professors in the Faculties of Engineering and Medicine and learn about our interests and expertise in different BME areas - biosensors & medical devices, functional tissue engineering, informatics in biomedicine, medical imaging, etc.
If you need more information about BME at CUHK, please feel free to contact
Professor Arthur Mak:arthurmak@cuhk.edu.hk.
Hope you have an enjoyable visit to our website.
Welcome to the of Biomedical Engineering at CUHK. We advance cutting-edge engineering for health enhancement and patient care.
You can find at this site information about our BEng major programme in BME and its minor, our double degree programme with business administration, as well as our msc and articulated MPhil and PhD programmes in BME.
All our BME programmes have been launched by the Faculty of Engineering in close collaboration with the Faculty of Medicine as co-owned interdisciplinary programmes. Academic contributions are provided by various departments across the two Faculties. Administrative supports are provided by the Department of Electronic Engineering for our undergraduate and by the Department of Mechanical & Automation Engineering for our MSc programme.
Please meet our BME professors in the Faculties of Engineering and Medicine and learn about our interests and expertise in different BME areas - biosensors & medical devices, functional tissue engineering, information in biomedicine, medical imaging
If you need more information about BME at CUHK, please feel free to contact
Hope you have an enjoyable visit to our website.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment