Shaokang's Blog

A user script designed to be injected into itestcloud.unipus.cn, helping individuals grade in their preferred, customized way by utilizing TensorFlow and bi-gram to simplify the grading process. The original project is intended to help my mom grade assignments more quickly and alleviate her stress in grading students’ assignments. The project uses Bi-gram and tf.keras.losses.CosineSimilarity of TensorFlow v2.14.0 to calculate similarity, taking into account word counts, and it reports the recommendation score. Users can choose to accept or adjust the recommended score.

Original script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// ==UserScript==
// @name New Userscript
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://itestcloud.unipus.cn/utest/itest/biz/kaoshi/rate?*
// @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant none
// @require https://cdn.jsdelivr.net/npm/@tensorflow/tfjs
// @require https://cdn.jsdelivr.net/npm/@tensorflow-models/universal-sentence-encoder
// ==/UserScript==


(function() {
'use strict';
function compareTwoStrings(first, second) {
first = first.replace(/\s+/g, '')
second = second.replace(/\s+/g, '')

if (first === second) return 1; // identical or empty
if (first.length < 2 || second.length < 2) return 0; // if either is a 0-letter or 1-letter string

let firstBigrams = new Map();
for (let i = 0; i < first.length - 1; i++) {
const bigram = first.substring(i, i + 2);
const count = firstBigrams.has(bigram)
? firstBigrams.get(bigram) + 1
: 1;

firstBigrams.set(bigram, count);
};

let intersectionSize = 0;
for (let i = 0; i < second.length - 1; i++) {
const bigram = second.substring(i, i + 2);
const count = firstBigrams.has(bigram)
? firstBigrams.get(bigram)
: 0;

if (count > 0) {
firstBigrams.set(bigram, count - 1);
intersectionSize++;
}
}

return (2.0 * intersectionSize) / (first.length + second.length - 2);
}
function word(sentence){
var length = sentence.length;

//去掉字符串前面的空格
for(var i=0;i<length;i++) {
if(sentence[i]!=' ') {
sentence = sentence.substring(i,length);
break;
}
}

//去掉字符串前面空格的字符串长度
length=sentence.length;
//去掉字符串后面的空格
for(i=length-1;i>=0;i--) {
if(sentence[i]!=' ') {
sentence = sentence.substring(0,i+1);
break;
}
}

length=sentence.length;
//去掉字符串中间的空格
for(i=0;i<length;i++) {
if(sentence[i]==' ') {
if(sentence[i+1]==' ') {
var buffer1 = sentence.substring(0,i);
var buffer2 = sentence.substring(i+1,sentence.length);
sentence = buffer1+buffer2;
i--;
}
}
}

return sentence;
}

function CountWord(sentence) {
let s = sentence;
s = s.replace(/(^\s*)|(\s*$)/gi,"");
s = s.replace(/[ ]{2,}/gi," ");
s = s.replace(/\n /,"\n");
return s.split(' ').length;
}

let stu = word(document.getElementsByTagName("pre")[2].innerText);
let ans = document.getElementsByTagName("pre")[3].innerText;
console.log(stu);
console.log(ans);
let wordbyword = compareTwoStrings(stu,ans);
// Your code here...
let similarity = 0;
let stuword = parseInt(document.getElementsByClassName("rateScoreInput unknown")[1].parentElement.nextElementSibling.nextElementSibling.innerText);
let wordScore = stuword/CountWord(ans);
if(wordScore>1) wordScore = 1;
console.log("word score: "+wordScore);
if(wordScore<0.5) alert("F文章字数少:"+(wordScore*100).toFixed(2));
console.log("wordbyword: " + wordbyword);
if(wordbyword<0.5) alert("F文章逐字少:"+(wordbyword*100).toFixed(2));
(async() => {
const model = await use.load();
const embeddings = (await model.embed([ans, stu])).unstack()
console.log(embeddings[0])
similarity = (1-parseFloat((await tf.losses.cosineDistance(embeddings[0], embeddings[1], 0).data()).toString()));
console.log("Tensorflow Similarity: "+ similarity);
let finalGrade1 = (wordScore*0.75 + wordbyword*0.05 + similarity*0.2)*20;
console.log("Final F: " + finalGrade1);
if(similarity<0.8) alert("F文章similarity低:"+(similarity*100).toFixed(2));
finalGrade1 = Math.floor(finalGrade1);
document.getElementsByClassName("rateScoreInput unknown")[1].value = finalGrade1;
document.getElementsByClassName("rateScoreInput unknown")[1].parentElement.nextElementSibling.nextElementSibling.innerText += "\n字数:" + stuword + "/" + CountWord(ans) + "=" + (wordScore*100).toFixed(2) + "\n逐字:" +(wordbyword*100).toFixed(2)+ "\nTensorFlow:" +(similarity*100).toFixed(2)+"\nFinal:"+finalGrade1;
})()
let a = document.createElement("div");
a.innerHTML = `<input type='button' value='Add One' onclick='document.getElementsByClassName("rateScoreInput unknown")[1].value = parseInt(document.getElementsByClassName("rateScoreInput unknown")[1].value) + 1'><input type='button' value='Dec One' onclick='document.getElementsByClassName("rateScoreInput unknown")[1].value = parseInt(document.getElementsByClassName("rateScoreInput unknown")[1].value) - 1'>`
document.getElementsByClassName("rateScoreInput unknown")[1].parentElement.appendChild(a)
})();
// document.getElementsByClassName("rateScoreInput unknown")[0].parentElement.appendChild()
//字数50%

 Comments