forked from answerquest/answerquest.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwords-replacer.html
132 lines (115 loc) · 3.75 KB
/
words-replacer.html
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
127
128
129
130
131
132
<!DOCTYPE html>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<head><title>Words Replacer : CTRL+H on Steroids</title>
<style>
pre {
width:50%;
border: dotted 1px black;
white-space: pre-wrap;
background-color: silver;
}
@media screen and (max-width: 1020px) {
pre { width:100%; }
}
</style>
<script type="text/javascript">
function ImproveEnglish()
{
var array1 = document.getElementById('badenglish').value.split('\n');
var array2 = document.getElementById('goodenglish').value.split('\n');
var count = 0;
var totalcount = 0;
document.getElementById("log").value = "";
var LENGTH = array1.length;
if ( LENGTH != array2.length ) {
document.getElementById("output_text").value = "Fix the substitution list! \nTarget: " + LENGTH + " , Replacements: " + array2.length;
return;
}
document.getElementById("log").value += "Length of substitution list: " + LENGTH + "\n";
var OUTPUT=document.getElementById("input_text").value;
for ( i=0; i < LENGTH; i++ ) // for loop 1
{
//counting how many replacements happening :
count = OUTPUT.split(array1[i]).length - 1;
totalcount += count;
//str.split(search).join(replacement)
OUTPUT = OUTPUT.split(array1[i]).join(array2[i]);
document.getElementById("log").value += count + " times : \"" + array1[i] + "\" >> \"" + array2[i] + "\"\n";
} //end of for loop 1
document.getElementById("log").value += "\nFound and Replaced "+ totalcount + " occurrences total.\n\n";
document.getElementById("output_text").value = OUTPUT;
} // end of function ImproveEnglish()
function toTitleCase()
{
var str = document.getElementById("output_text").value;
var TitleStr = str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1);});
document.getElementById("output_text").value = TitleStr;
}
</script>
<body>
<h3>Words Replacer : Ctrl+H on Steroids!</h3>
<table width="100%">
<tr width="100%">
<td width="50%">
Paste the original text in the box below. <br>
<textarea id="input_text" cols="60" rows="30"></textarea>
<br>
<input type = "button" value="Replace!" onClick="ImproveEnglish()" style="width: 15em; height: 3em;">
</td>
<td>
Output box:<br>
<textarea id="output_text" cols="60" rows="30"></textarea>
<br>
<input type = "button" value="Title Case but preserve abbreviations" onClick="toTitleCase()">
<br>
</td>
</tr>
</table>
<hr>
<h3>Substitution list: </h3>
<table width="100%">
<tr width="100%">
<td width="50%">
Words to replace:<br>
<textarea id="badenglish" cols="50" rows="30">
</textarea>
</td>
<td width="50%">
Replace with..<small>(please maintain order! Even blank lines are counted)</small><br>
<textarea id="goodenglish" cols="50" rows="30">
</textarea>
</td>
</tr>
</table>
<br>
<input type = "button" value="Clear replacements" onClick="document.getElementById('badenglish').value=''; document.getElementById('goodenglish').value='';">
<br><br>
<h3>Log:</h3>
<textarea id="log"></textarea>
<h3>Sample Usage</h3>
<pre>
Original Text:
Paste the original text in the box below.
---
Words to replace:
in
text of
text from
---
Replace with:
of
text from
MUHAHAHAA
----
// notice the space before "in" and "of". This is to disambiguate from the letters "in" inside a word like "original".
Output:
Paste the original MUHAHAHAA the box below.
==========
Explanation:
The script makes its way sequentially down the list, and uses the replaced text for every subsequent replacement. Hence, it's possible to chain the replacements:
original text in the >> original text of the >> original text from the >> original MUHAHAHAA the
</pre>
<p>By Nikhil VJ, nikhil.js [at] gmail.com. Created during a task to fix a large amount of transliteration mis-spellings.
</body>
</html>