Skip to content

Commit f3a4017

Browse files
authored
Merge pull request #36 from kWeglinski/develop
0.14.3
2 parents d726ae5 + cc7b8c0 commit f3a4017

File tree

8 files changed

+77
-76
lines changed

8 files changed

+77
-76
lines changed

.env.sample

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ OLLAMA_MODEL=model_name
66
# pick one of: 'pole' | 'light' | 'dark'
77
THEME='dark'
88
LIBRETRANSLATE_LANGUAGES=["pl","en"]
9-
LANGUAGE_TOOL_LANGUAGES=["pl-PL","en-GB"]
9+
LANGUAGE_TOOL_LANGUAGES=["pl-PL","en-GB"]
10+
LANGUAGE_TOOL_PICKY=true
11+
DEBUG=false

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ services:
4343
restart: unless-stopped
4444
environment:
4545
LANGUAGE_TOOL: https://your.languagetool.instance
46+
# if you want to enable picky mode in languagetool
47+
LANGUAGE_TOOL_PICKY: true
4648
LIBRETRANSLATE: https://your.libretranslate.instance
4749
LIBRETRANSLATE_API_KEY: 'your_API_key' # use if your instance requires API key
4850
OLLAMA: https://your.ollama.instance
@@ -52,9 +54,10 @@ services:
5254
# To limit language options for translations provide an array of ISO 639 language codes
5355
LIBRETRANSLATE_LANGUAGES: ["pl","en"] # optional
5456
# To limit language options for text check provide an array of long tags (ISO 639/ISO-3166) also known as language-Region code.
55-
LANGUAGE_TOOL_LANGUAGES=["pl-PL","en-GB"] # optional
57+
LANGUAGE_TOOL_LANGUAGES: ["pl-PL","en-GB"] # optional
5658
# To disable "add word" to dicationary
57-
DISABLE_DICTIONARY=true # optional
59+
DISABLE_DICTIONARY: true # optional
60+
DEBUG: false # set to true to log text sent to tools and it's raw responses
5861
ports:
5962
- 80:80
6063
image: kweg/omnipoly:latest

changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
### 0.14.3
2+
3+
- Fix: during the creation of improved fix tooltip I've introduced a bug that completely broke position where text is fixed. This is fixed now.
4+
- Feat: added picky level in language tool (use an ENV to enable it)
5+
- Feat: added DEBUG mode to log all requests
6+
17
### 0.14.2
28

39
- Chore: suggest different backend for languagetool. Apparently the one in readme wasn't the best.

index.js

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,22 @@ const getLanguages = (langs) => {
2323
}
2424
};
2525

26-
const DEV = process.env.DEV
26+
const DEV = process.env.DEV;
2727
const LANGUAGE_TOOL = process.env.LANGUAGE_TOOL;
28+
const LANGUAGE_TOOL_PICKY = process.env.LANGUAGE_TOOL_PICKY;
2829
const LIBRETRANSLATE = process.env.LIBRETRANSLATE;
2930
const OLLAMA = process.env.OLLAMA;
3031
const OLLAMA_MODEL = process.env.OLLAMA_MODEL;
3132
const THEME = process.env.THEME;
33+
const DEBUG = process.env.DEBUG;
3234
const LIBRETRANSLATE_API_KEY = process.env.LIBRETRANSLATE_API_KEY;
3335
const LIBRETRANSLATE_LANGUAGES = getLanguages(
3436
process.env.LIBRETRANSLATE_LANGUAGES
3537
);
3638
const LANGUAGE_TOOL_LANGUAGES = getLanguages(
3739
process.env.LANGUAGE_TOOL_LANGUAGES
3840
);
39-
const DISABLE_DICTIONARY = process.env.DISABLE_DICTIONARY === 'true'
41+
const DISABLE_DICTIONARY = process.env.DISABLE_DICTIONARY === "true";
4042

4143
const maskString = (str) => {
4244
if (!str || str.length <= 3) {
@@ -60,23 +62,35 @@ LANGUAGE_TOOL_LANGUAGES: ${JSON.stringify(LANGUAGE_TOOL_LANGUAGES)}
6062
LIBRETRANSLATE_LANGUAGES: ${JSON.stringify(LIBRETRANSLATE_LANGUAGES)}
6163
DICTIONARY_DISABLED: ${!!DISABLE_DICTIONARY}
6264
DEV_MODE: ${!!DEV}
65+
DEBUG_MODE: ${!!DEBUG}
6366
========================
6467
`);
6568

6669
app.use(bodyParser.urlencoded({ extended: false }));
6770
app.use(bodyParser.json());
6871

6972
const handleProxyGET = (url, res, filter) => {
73+
if (DEBUG) {
74+
console.log(`Calling: ${url}`);
75+
}
7076
fetch(url)
7177
.then((data) => data.json())
72-
.then((data) => res.send(filter ? filter(data) : data))
78+
.then((data) => {
79+
if (DEBUG) {
80+
console.log(data);
81+
}
82+
return res.send(filter ? filter(data) : data);
83+
})
7384
.catch((error) => {
7485
console.error("[PROXY GET ERROR]", error);
75-
res.status(500).send('Internal server error');
86+
res.status(500).send("Internal server error");
7687
});
7788
};
7889

7990
const handleProxyPost = (url, req, res) => {
91+
if (DEBUG) {
92+
console.log(`Calling: ${url}`);
93+
}
8094
fetch(url, {
8195
method: "POST",
8296
body: JSON.stringify(req.body),
@@ -86,15 +100,18 @@ const handleProxyPost = (url, req, res) => {
86100
return data.json();
87101
})
88102
.then((data) => {
103+
if (DEBUG) {
104+
console.log(data);
105+
}
89106
res.send(data);
90107
})
91108
.catch((error) => {
92109
console.log({ error, url });
93-
res.status(500).send('Internal server error');
110+
res.status(500).send("Internal server error");
94111
});
95112
};
96113

97-
const handleFormDataPost = (url, req, res, filter) => {
114+
const handleFormDataPost = (url, req, res, filter, additionalData) => {
98115
const headers = {
99116
"Content-Type": "application/x-www-form-urlencoded",
100117
Accept: "application/json",
@@ -105,18 +122,30 @@ const handleFormDataPost = (url, req, res, filter) => {
105122
keys.forEach((key) => {
106123
formData.append(key, req.body[key]);
107124
});
125+
if (additionalData) {
126+
Object.keys(additionalData).forEach((key) => {
127+
formData.append(key, additionalData[key]);
128+
});
129+
}
130+
if (DEBUG) {
131+
console.log(`Calling: ${url}`);
132+
console.log({ formData });
133+
}
108134
return fetch(url, {
109135
method: "POST",
110136
headers: headers,
111137
body: formData.toString(),
112138
})
113139
.then((data) => data.json())
114140
.then((data) => {
141+
if (DEBUG) {
142+
console.log(data);
143+
}
115144
res.send(filter ? filter(data) : data);
116145
})
117146
.catch((error) => {
118147
console.log({ error: error.message, url });
119-
res.status(500).send('Internal server error');
148+
res.status(500).send("Internal server error");
120149
});
121150
};
122151

@@ -127,7 +156,7 @@ app.get("/api/status", (req, res) => {
127156
OLLAMA,
128157
OLLAMA_MODEL,
129158
THEME,
130-
DISABLE_DICTIONARY
159+
DISABLE_DICTIONARY,
131160
});
132161
});
133162

@@ -183,14 +212,24 @@ app.post("/api/languagetool/check", (req, res) => {
183212
};
184213

185214
const targetFilter = DISABLE_DICTIONARY ? filter : false;
186-
handleFormDataPost(`${LANGUAGE_TOOL}/v2/check`, req, res, targetFilter);
215+
handleFormDataPost(
216+
`${LANGUAGE_TOOL}/v2/check`,
217+
req,
218+
res,
219+
targetFilter,
220+
LANGUAGE_TOOL_PICKY
221+
? {
222+
level: "picky",
223+
}
224+
: {}
225+
);
187226
});
188227

189228
app.post("/api/languagetool/add", (req, res) => {
190229
if (DISABLE_DICTIONARY) {
191-
res.status(403).send()
230+
res.status(403).send();
192231
return;
193-
}
232+
}
194233
try {
195234
addWord(`${req.body.word}`.toLowerCase());
196235
console.log("added:", `${req.body.word}`.toLowerCase());

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "sth-libretranslate",
33
"private": true,
4-
"version": "0.14.2",
4+
"version": "0.14.3",
55
"type": "module",
66
"scripts": {
77
"dev": "concurrently \"npm run node\" \"vite\"",

src/languagecheck/App.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ function App() {
7272
>
7373
<div>
7474
<Content />
75-
{/* <TextBox /> */}
7675
</div>
7776
<Divider orientation="vertical" flexItem />
7877
<div style={{ position: "relative" }}>

src/languagecheck/Resolution.tsx

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,21 @@ import { API, Match } from "./API";
1818
import { actions, useGrammar } from "../store/grammar";
1919
import { useSystemStatus } from "../store/status";
2020

21-
type Fix = (start: number, length: number, value: string, idx: number) => void;
22-
2321
const DisplayMatch = ({
2422
message,
2523
context,
2624
replacements,
2725
shortMessage,
28-
fix,
2926
selected,
3027
setSelection,
3128
idx,
3229
rule,
3330
}: Match & {
34-
fix: Fix;
3531
selected: boolean;
3632
setSelection: () => void;
3733
idx: number;
3834
}) => {
3935
const { disableDictionary } = useSystemStatus();
40-
const { question, answer, selection } = useGrammar();
4136

4237
return (
4338
<Accordion expanded={selected}>
@@ -68,25 +63,7 @@ const DisplayMatch = ({
6863
size="small"
6964
label={repl.value}
7065
onClick={() => {
71-
if (!answer || selection === null) {
72-
return;
73-
}
74-
75-
const match = answer.matches[selection];
76-
if (!match) {
77-
return;
78-
}
79-
80-
// Count newline-like characters (\n, \t, etc.) before the offset
81-
const nCount = match.context.text.substring(0, match.context.offset).match(/\\[ntrvf]/g)?.length ?? 0;
82-
const adjustedStart = match.context.offset + nCount;
83-
84-
// Apply the fix to the question text
85-
const fixedQuestion = `${question.substring(0, adjustedStart)}${repl.value}${question.substring(adjustedStart + match.context.length)}`;
86-
87-
// Update the state
88-
actions.setQuestion(fixedQuestion);
89-
actions.popAnswer(selection);
66+
actions.fixPos(repl.value, idx)
9067
}}
9168
sx={{ marginRight: 1, marginBottom: 0.5, marginTop: 0.5 }}
9269
/>
@@ -101,7 +78,6 @@ const DisplayMatch = ({
10178
variant="outlined"
10279
sx={{ margin: "auto" }}
10380
onClick={() => {
104-
fix(0, 0, "", idx);
10581
API().addWord(
10682
context.text.substr(context.offset, context.length)
10783
);
@@ -119,7 +95,6 @@ const DisplayMatch = ({
11995

12096
export const Resolution = () => {
12197
const {
122-
question: original,
12398
loading,
12499
error,
125100
selection,
@@ -140,13 +115,6 @@ export const Resolution = () => {
140115
</div>
141116
);
142117
}
143-
const fix: Fix = (start, length, value, idx) => {
144-
const fixed = `${original.substring(0, start)}${value}${original.substring(
145-
start + length
146-
)}`;
147-
actions.setQuestion(fixed);
148-
actions.popAnswer(idx);
149-
};
150118
return (
151119
<div style={{ paddingTop: "20px" }}>
152120
{error?.length > 0 && (
@@ -163,7 +131,6 @@ export const Resolution = () => {
163131
<DisplayMatch
164132
key={i}
165133
{...data}
166-
fix={fix}
167134
selected={selection === i}
168135
setSelection={() => actions.setSelection(i)}
169136
idx={i}

src/store/grammar.tsx

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -87,32 +87,17 @@ export const actions = {
8787
};
8888
useGrammar.setState({ answer: newAnswer });
8989
},
90-
fixPos: (value: string, index?: number) => {
91-
const { question, answer, selection } = useGrammar.getState();
92-
const target = index ?? selection;
93-
if (!answer || target === null) {
94-
return;
95-
}
96-
97-
const match = answer.matches[target];
98-
if (!match) {
99-
return;
100-
}
101-
102-
// Count newline-like characters (\n, \t, \r, \v, \f) before the offset
90+
fixPos: (value: string, index: number) => {
91+
const { question, answer } = useGrammar.getState();
92+
const match = answer?.matches[index];
93+
const offset = match?.offset || 0;
10394
const nCount =
104-
question.substring(0, match.context.offset).match(/\\[ntrvf]/g)?.length ??
105-
0;
106-
const adjustedStart = match.context.offset + nCount;
107-
108-
// Apply the fix to the question text
109-
const fixedQuestion = `${question.substring(
110-
0,
111-
adjustedStart
112-
)}${value}${question.substring(adjustedStart + match.context.length)}`;
113-
114-
// Update the state
115-
actions.setQuestion(fixedQuestion);
116-
actions.popAnswer(target);
95+
question.substring(0, offset).match(/\\[ntrvf]/g)?.length ?? 0;
96+
const start = offset + nCount;
97+
const fixed = `${question.substring(0, start)}${value}${question.substring(
98+
start + (match?.length || 0)
99+
)}`;
100+
actions.setQuestion(fixed);
101+
actions.popAnswer(index);
117102
},
118103
};

0 commit comments

Comments
 (0)