Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
J
Julia-seminar
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Stamm, Benjamin
Julia-seminar
Commits
348a83fc
Commit
348a83fc
authored
1 year ago
by
Michele Nottoli
Browse files
Options
Downloads
Patches
Plain Diff
Wrote a script for localization.
parent
6465b8f1
Branches
Branches containing commit
No related tags found
1 merge request
!6
Localization
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/0_Installation.ipynb
+0
-0
0 additions, 0 deletions
src/0_Installation.ipynb
src/1_Basics.ipynb
+0
-0
0 additions, 0 deletions
src/1_Basics.ipynb
src/localize.py
+85
-0
85 additions, 0 deletions
src/localize.py
with
85 additions
and
0 deletions
0_Installation.ipynb
→
src/
0_Installation.ipynb
+
0
−
0
View file @
348a83fc
File moved
This diff is collapsed.
Click to expand it.
1_Basics.ipynb
→
src/
1_Basics.ipynb
+
0
−
0
View file @
348a83fc
File moved
This diff is collapsed.
Click to expand it.
src/localize.py
0 → 100755
+
85
−
0
View file @
348a83fc
#!/usr/bin/python3
import
copy
import
json
import
os
import
sys
import
langid
def
detect_language
(
text
):
return
langid
.
classify
(
text
)
def
load
(
path
):
with
open
(
path
,
encoding
=
"
utf-8
"
)
as
file
:
notebook
=
json
.
load
(
file
)
return
notebook
def
human_intervention
(
language
,
probability
,
text
):
print
(
f
"
I classified this text:
\n
`
{
text
}
`
\n
as
{
language
}
with a non normalized probability of
{
probability
}
.
\n
"
)
while
True
:
print
(
"
Can you help me identifying it? write one among
'
en
'
,
'
de
'
,
'
eq
'
(for equations), then press enter
"
)
language
=
input
()
if
language
in
[
"
en
"
,
"
de
"
,
"
eq
"
]:
print
(
"
\n\n
"
)
return
language
def
write
(
path
,
notebook
):
with
open
(
path
,
"
w
"
,
encoding
=
"
utf-8
"
)
as
file
:
json
.
dump
(
notebook
,
file
,
indent
=
4
)
def
assign_languages
(
notebook
):
cells
=
notebook
[
"
cells
"
]
for
i
,
cell
in
enumerate
(
cells
):
if
cell
[
"
cell_type
"
]
==
"
markdown
"
:
text
=
str
(
cell
[
"
source
"
]).
strip
(
"
\n
# []
'"
)
language
,
probability
=
detect_language
(
text
)
if
probability
>=
-
10
or
language
not
in
[
"
en
"
,
"
de
"
]:
# langid is doing something strange, we need to intervene
language
=
human_intervention
(
language
,
probability
,
text
)
notebook
[
"
cells
"
][
i
][
"
metadata
"
][
"
natural_language
"
]
=
language
def
select_language
(
notebook
,
language
):
cells
=
notebook
[
"
cells
"
]
selected_cells
=
[]
for
cell
in
cells
:
if
cell
[
"
cell_type
"
]
==
"
markdown
"
:
if
cell
[
"
metadata
"
][
"
natural_language
"
]
==
language
:
selected_cells
.
append
(
cell
)
elif
cell
[
"
metadata
"
][
"
natural_language
"
]
==
"
eq
"
:
selected_cells
.
append
(
cell
)
else
:
selected_cells
.
append
(
cell
)
new_notebook
=
copy
.
deepcopy
(
notebook
)
new_notebook
[
"
cells
"
]
=
selected_cells
return
new_notebook
if
__name__
==
"
__main__
"
:
PATH
=
sys
.
argv
[
1
]
BASENAME
=
os
.
path
.
splitext
(
os
.
path
.
basename
(
PATH
))[
0
]
NOTEBOOK
=
load
(
PATH
)
assign_languages
(
NOTEBOOK
)
NOTEBOOK_EN
=
select_language
(
NOTEBOOK
,
"
en
"
)
NOTEBOOK_DE
=
select_language
(
NOTEBOOK
,
"
de
"
)
write
(
f
"
{
BASENAME
}
_en.ipynb
"
,
NOTEBOOK_EN
)
write
(
f
"
{
BASENAME
}
_de.ipynb
"
,
NOTEBOOK_DE
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment