À l'origine deux fois auparavant, l'article précédent [Python] Traduisons automatiquement le PDF anglais (mais sans s'y limiter) avec la traduction DeepL ou Google pour en faire un fichier texte. Suite [Python] Traduisons automatiquement le PDF anglais (mais sans s'y limiter) avec la traduction DeepL ou Google dans un fichier texte, pas de HTML.
Je l'ai écrit pour l'utiliser dans, mais cela semble utile, je vais donc le présenter séparément.
Je n'ai pas de connaissances détaillées sur le PDF, Il semble que le texte est divisé en petites parties et écrit dans le PDF, et le texte copié contient également le code de saut de ligne à la position affichée dans le PDF.
Par exemple, en PDF
Dans le cas d'un affichage comme, le texte copié est
Etc. (L'exemple ci-dessus est pour Windows)
Ensuite, j'ai pensé que je devais effacer le code de saut de ligne et relier les phrases.
De cette façon, dans cet exemple, il y a un point pour que chaque phrase ne soit pas mélangée.
Qu'en est-il des cas suivants?
Si vous effacez simplement le code de saut de ligne,
Il est devenu impossible de faire la distinction entre la première ligne et la deuxième ligne sans point.
Donc le problème est ** Les parties qui n'ont pas toujours de marques de rupture telles que des points, tels que des titres Comment déduire et décomposer du texte copié qui ne contient que des indices pour les phrases et les codes de saut de ligne. ** ** C'est le but.
Nous avons adopté une méthode telle que. C'est assez simple, mais la plupart des phrases
・ Cap ・ Paragraphe · Phrase
Vous avez maintenant une fonction qui peut être divisée en n'importe quelle unité.
import re
import unicodedata
def len_(text):
cnt = 0
for t in text:
if unicodedata.east_asian_width(t) in "FWA":
cnt += 2
else:
cnt += 1
return cnt
def textParser(text, n=30, bracketDetect=True):
text = text.splitlines()
sentences = []
t = ""
bra_cnt = ket_cnt = bra_cnt_jp = ket_cnt_jp = 0
for i in range(len(text)):
if not bool(re.search("\S", text[i])): continue
if bracketDetect:
bra_cnt += len(re.findall("[\((]", text[i]))
ket_cnt += len(re.findall("[\))]", text[i]))
bra_cnt_jp += len(re.findall("[""]", text[i]))
ket_cnt_jp += len(re.findall("["" "]", text[i]))
if i != len(text) - 1:
if bool(re.fullmatch(r"[A-Z\s]+", text[i])):
if t != "": sentences.append(t)
t = ""
sentences.append(text[i])
elif bool(
re.match(
"(\d{1,2}[\.,、.]\s?(\d{1,2}[\.,、.]*)*\s?|I{1,3}V{0,1}X{0,1}[\.,、.]|V{0,1}X{0,1}I{1,3}[\.,、.]|[・ • ●])+\s",
text[i])) or re.match("\d{1,2}.\w", text[i]) or (
bool(re.match("[A-Z]", text[i][0]))
and abs(len_(text[i]) - len_(text[i + 1])) > n
and len_(text[i]) < n):
if t != "": sentences.append(t)
t = ""
sentences.append(text[i])
elif (
text[i][-1] not in ("。", ".", ".") and
(abs(len_(text[i]) - len_(text[i + 1])) < n or
(len_(t + text[i]) > len_(text[i + 1]) and bool(
re.search("[。\..]\s\d|..[。\..]|.[。\..]", text[i + 1][-3:])
or bool(re.match("[A-Z]", text[i + 1][:1]))))
or bool(re.match("\s?[a-z,\)]", text[i + 1]))
or bra_cnt > ket_cnt or bra_cnt_jp > ket_cnt_jp)):
t += text[i]
else:
sentences.append(t + text[i])
t = ""
else:
sentences.append(t + text[i])
return sentences
Si le résultat n'est pas bon, essayez d'ajuster la valeur de «n» (plus grand est plus ensemble, plus petit est plus disjoint). Si le nombre de parenthèses est mal aligné pour une raison quelconque et que le texte est étrangement figé, définissez «bracketDetect» sur «False».
Python 3.8.5 Documentation PDF (format papier US-Letter) \ tutorial.pdf À partir de la page numéro 5 (p.11)
Une copie de l'original
CHAPTER
TWO
USING THE PYTHON INTERPRETER
2.1 Invoking the Interpreter
The Python interpreter is usually installed as /usr/local/bin/python3.8 on those machines where it is available;
putting /usr/local/bin in your Unix shell’s search path makes it possible to start it by typing the command:
python3.8
to the shell.1 Since the choice of the directory where the interpreter lives is an installation option, other places are possible;
check with your local Python guru or system administrator. (E.g., /usr/local/python is a popular alternative
location.)
On Windows machines where you have installed Python from the Microsoft Store, the python3.8 command will be
available. If you have the py.exe launcher installed, you can use the py command. See setting-envvars for other ways to
launch Python.
Typing an end-of-file character (Control-D on Unix, Control-Z on Windows) at the primary prompt causes the
interpreter to exit with a zero exit status. If that doesn’t work, you can exit the interpreter by typing the following command:
quit().
The interpreter’s line-editing features include interactive editing, history substitution and code completion on systems that
support the GNU Readline library. Perhaps the quickest check to see whether command line editing is supported is typing
Control-P to the first Python prompt you get. If it beeps, you have command line editing; see Appendix Interactive
Input Editing and History Substitution for an introduction to the keys. If nothing appears to happen, or if ^P is echoed,
command line editing isn’t available; you’ll only be able to use backspace to remove characters from the current line.
The interpreter operates somewhat like the Unix shell: when called with standard input connected to a tty device, it reads
and executes commands interactively; when called with a file name argument or with a file as standard input, it reads and
executes a script from that file.
A second way of starting the interpreter is python -c command [arg] ..., which executes the statement(s) in
command, analogous to the shell’s -c option. Since Python statements often contain spaces or other characters that are
special to the shell, it is usually advised to quote command in its entirety with single quotes.
Some Python modules are also useful as scripts. These can be invoked using python -m module [arg] ...,
which executes the source file for module as if you had spelled out its full name on the command line.
When a script file is used, it is sometimes useful to be able to run the script and enter interactive mode afterwards. This
can be done by passing -i before the script.
All command line options are described in using-on-general.
1 On Unix, the Python 3.x interpreter is by default not installed with the executable named python, so that it does not conflict with a simultaneously
installed Python 2.x executable.
Il y a un code de saut de ligne à la fin de chaque ligne. Si vous affichez "comme code de saut de ligne" pour une meilleure compréhension
CHAPTER\r\nTWO\r\nUSING THE PYTHON INTERPRETER\r\n2.1 Invoking the Interpreter\r\nThe Python interpreter is usually installed as /usr/local/bin/python3.8 on those machines where it is available;\r\nputting /usr/local/bin in your Unix shell’s search path makes it possible to start it by typing the command:\r\npython3.8\r\nto the shell.1 Since the choice of the directory where the interpreter lives is an installation option, other places are possible;\r\ncheck with your local Python guru or system administrator. (E.g., /usr/local/python is a popular alternative\r\nlocation.)\r\nOn Windows machines where you have installed Python from the Microsoft Store, the python3.8 command will be\r\navailable. If you have the py.exe launcher installed, you can use the py command. See setting-envvars for other ways to\r\nlaunch Python.\r\nTyping an end-of-file character (Control-D on Unix, Control-Z on Windows) at the primary prompt causes the\r\ninterpreter to exit with a zero exit status. If that doesn’t work, you can exit the interpreter by typing the following command:\r\nquit().\r\nThe interpreter’s line-editing features include interactive editing, history substitution and code completion on systems that\r\nsupport the GNU Readline library. Perhaps the quickest check to see whether command line editing is supported is typing\r\nControl-P to the first Python prompt you get. If it beeps, you have command line editing; see Appendix Interactive\r\nInput Editing and History Substitution for an introduction to the keys. If nothing appears to happen, or if ^P is echoed,\r\ncommand line editing isn’t available; you’ll only be able to use backspace to remove characters from the current line.\r\nThe interpreter operates somewhat like the Unix shell: when called with standard input connected to a tty device, it reads\r\nand executes commands interactively; when called with a file name argument or with a file as standard input, it reads and\r\nexecutes a script from that file.\r\nA second way of starting the interpreter is python -c command [arg] ..., which executes the statement(s) in\r\ncommand, analogous to the shell’s -c option. Since Python statements often contain spaces or other characters that are\r\nspecial to the shell, it is usually advised to quote command in its entirety with single quotes.\r\nSome Python modules are also useful as scripts. These can be invoked using python -m module [arg] ...,\r\nwhich executes the source file for module as if you had spelled out its full name on the command line.\r\nWhen a script file is used, it is sometimes useful to be able to run the script and enter interactive mode afterwards. This\r\ncan be done by passing -i before the script.\r\nAll command line options are described in using-on-general.\r\n1 On Unix, the Python 3.x interpreter is by default not installed with the executable named python, so that it does not conflict with a simultaneously\r\ninstalled Python 2.x executable.
Ça ressemble à ça.
Je vais le jeter dans la fonction que j'ai faite cette fois. En supposant que la phrase ci-dessus est copiée dans le presse-papiers
from pyperclip import paste #Fonction pour obtenir la valeur (texte) du presse-papiers
print("\n".join(textParser(paste())))
out
CHAPTER
TWO
USING THE PYTHON INTERPRETER
2.1 Invoking the Interpreter
The Python interpreter is usually installed as /usr/local/bin/python3.8 on those machines where it is available;putting /usr/local/bin in your Unix shell’s search path makes it possible to start it by typing the command:python3.8to the shell.1 Since the choice of the directory where the interpreter lives is an installation option, other places are possible;check with your local Python guru or system administrator. (E.g., /usr/local/python is a popular alternativelocation.)On Windows machines where you have installed Python from the Microsoft Store, the python3.8 command will beavailable. If you have the py.exe launcher installed, you can use the py command. See setting-envvars for other ways tolaunch Python.
Typing an end-of-file character (Control-D on Unix, Control-Z on Windows) at the primary prompt causes theinterpreter to exit with a zero exit status. If that doesn’t work, you can exit the interpreter by typing the following command:quit().
The interpreter’s line-editing features include interactive editing, history substitution and code completion on systems thatsupport the GNU Readline library. Perhaps the quickest check to see whether command line editing is supported is typingControl-P to the first Python prompt you get. If it beeps, you have command line editing; see Appendix InteractiveInput Editing and History Substitution for an introduction to the keys. If nothing appears to happen, or if ^P is echoed,command line editing isn’t available; you’ll only be able to use backspace to remove characters from the current line.
The interpreter operates somewhat like the Unix shell: when called with standard input connected to a tty device, it readsand executes commands interactively; when called with a file name argument or with a file as standard input, it reads andexecutes a script from that file.
A second way of starting the interpreter is python -c command [arg] ..., which executes the statement(s) incommand, analogous to the shell’s -c option. Since Python statements often contain spaces or other characters that arespecial to the shell, it is usually advised to quote command in its entirety with single quotes.
Some Python modules are also useful as scripts. These can be invoked using python -m module [arg] ...,which executes the source file for module as if you had spelled out its full name on the command line.
When a script file is used, it is sometimes useful to be able to run the script and enter interactive mode afterwards. Thiscan be done by passing -i before the script.
installed Python 2.x executable.
Pretty good!
Cela dit, il est naturel de pouvoir diviser proprement un si beau PDF. Vous pouvez également l'utiliser pour des PDF plus complexes qui basculent entre 1 et 2 colonnes, alors essayez-le.
Ce n'est en aucun cas parfait pour chaque PDF, mais c'est un bon.
Le PDF japonais est également pris en charge, mais le PDF japonais contient beaucoup de texte dans l'OCR, vous pouvez donc effacer l'espace avec .replace (" "," ")
Je pense que ce sera propre (bien que cette méthode ne puisse pas être utilisée si le texte anglais est inclus).
Comme mentionné ci-dessus, lorsque vous souhaitez traduire un PDF, les utilisations sont nombreuses, veuillez donc l'utiliser.
Recommended Posts