-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.js
100 lines (87 loc) · 2.52 KB
/
converter.js
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
const { converter, irregularMatcher } = require('./utils/prompt')
const { generateResponse } = require('./utils/proxy')
const { dirToSave } = require('./config')
const { german, english } = require('./bookFragment')
const fs = require('fs')
const path = require('path')
async function matchContent (german, english) {
let response
try {
response = await generateResponse(converter(german, english), ['[', ']'])
} catch (error) {
throw error
}
const irregArr = extractContent(german, 1)
if (Array.isArray(irregArr) && irregArr.length > 0) {
let map = await generateResponse(
irregularMatcher(
irregArr,
response.map(arr => arr[0])
),
['[', ']']
)
for (let i = 0; i < map.length; i++) {
if (response[map[i]]) {
response[map[i]][4] = irregArr[i]
}
}
}
return response
.map(formatLine)
.join('\n')
.replace(/,\|/g, '.|')
.replace(/daß |muß |muß,|muß\./g, match => {
switch (match) {
case 'daß ':
return 'dass '
case 'muß ':
return 'muss '
case 'muß,':
return 'muss,'
case 'muß.':
return 'muss.'
default:
return match
}
})
}
function formatLine (line) {
const [word, example_sentence, meaning, translation, irr = ''] = line
return `${word}|${meaning}|${example_sentence}|${translation}|${irr}|`
}
async function processWords (german, english) {
try {
const responses = await matchContent(german, english)
let content = '#html:false\n#separator:|\n'
content = content + responses + '\n' + extractContent(english, 0).join('\n')
const filePath = path.join(dirToSave, 'words.txt')
fs.writeFileSync(filePath, content)
console.log('Responses written to', filePath, 'successfully.')
} catch (error) {
console.log('Error occurred while processing words:', error)
}
}
function extractContent (str, mode) {
const results = []
let regex
if (mode === 0) {
regex = /\(([^)]+)\)/g // 匹配括号 ()
} else if (mode === 1) {
regex = /<([^>]+)>| c([^>]+)>/g // 匹配尖括号 <>
} else {
throw new Error(
'Invalid mode. Use 0 for parentheses and 1 for angle brackets.'
)
}
let match
while ((match = regex.exec(str))) {
match[0] = match[0].replace(/-\n/g, '')
if (match[0][0] == ' ') {
results.push('<' + match[0].slice(2))
} else {
results.push(match[0])
}
}
return results
}
processWords(german.replace(/\[.*?\]/g, '').replace(/ \[.*?\]/g, ''), english)