Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion bin/demeuk.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@
becomes u, ç becomes c.
--trim Enables removing newlines representations from end and beginning. Newline
representations detected are '\\n', '\\r', '\n', '\r', '<br>', and '<br />'.
--transliterate <language> Transliterate a strings, for example "ipsum" becomes "իպսում". Language is iso

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hier ook -s bij "strings"

2 letter code. Examples: ru, sr, ua

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wellicht de volledige output van
>>> transliterate.get_available_language_codes() ['ka', 'sr', 'l1', 'ru', 'mn', 'uk', 'mk', 'el', 'hy', 'bg']

hier toevoegen?


Add modules (Modify a line, but keep the original as well):
--add-lower If a line contains a capital letter this will add the lower case variant
Expand Down Expand Up @@ -169,10 +171,11 @@
from nltk import str2tuple
from nltk.tokenize import WhitespaceTokenizer
from tqdm import tqdm
from transliterate import translit
from unidecode import unidecode


version = '4.5.1'
version = '4.6.0'

# Search from start to finish for the string $HEX[], with block of a-f0-9 with even number
# of hex chars. The first match group is repeated.
Expand Down Expand Up @@ -680,6 +683,23 @@ def clean_cut(line, delimiters, fields):
return False, line


def clean_transliterate(line, language):
"""Transliterate a string

Params:
line (Unicode)
Comment thread
zyronix marked this conversation as resolved.
language (str)

Returns:
line (Unicode)
"""
cleaned_line = translit(line, language, reversed=True)
if line != cleaned_line:
return True, cleaned_line
else:
return False, line


def clean_non_ascii(line):
"""Replace non ascii chars with there ascii representation.

Expand Down Expand Up @@ -1129,6 +1149,12 @@ def clean_up(lines):
if status and config['debug']:
log.append(f'Clean_umlaut; umlaut replaced; {line_decoded}{linesep}')

# Transliterate
if config.get('transliterate') and not stop:
status, line_decoded = clean_transliterate(line_decoded, config.get('transliterate'))
if status and config['debug']:
log.append(f'Clean_transliterate; translitatered; {line_decoded}{linesep}')

# Replace non-ascii
if config.get('non-ascii') and not stop:
status, line_decoded = clean_non_ascii(line_decoded)
Expand Down Expand Up @@ -1409,6 +1435,7 @@ def main():
'umlaut': False,
'non-ascii': False,
'title_case': False,
'transliterate': False,

# Check
'length': False,
Expand Down Expand Up @@ -1543,6 +1570,9 @@ def main():
if arguments.get('--trim'):
config['trim'] = True

if arguments.get('--transliterate'):
config['transliterate'] = arguments.get('--transliterate')
Comment thread
zyronix marked this conversation as resolved.

# Check modules
if arguments.get('--check-min-length'):
config['check-length'] = True
Expand Down
11 changes: 11 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,17 @@ https://pypi.org/project/Unidecode/

For example a line like 'kožušček' is replaced to kozuscek.

transliterate
~~~~~~~~~~~~~
Replaces Cyrillic characters with their Latin equivalents. For example, жута becomes Žuta. To take this even further,
combine it with --non-ascii to convert this to zuta.

The follow languages are supported: ka, sr, l1, ru, mn, uk, mk, el, hy and bg

--transliterate ru

Check https://pypi.org/project/transliterate/ for more details.

lowercase
~~~~~~~~~~
Replace lines like 'Test Test Test' to 'test test test'. Basically lowercasing all
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ nltk
ftfy
unidecode
tqdm
transliterate
4 changes: 4 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,3 +397,7 @@
with open('testdata/input54', 'w') as file:
file.write(f'Golf Trip{linesep}')
file.write(f'Sequences{linesep}')

with open('testdata/input55', 'w') as file:
file.write(f'здраво пријатељу{linesep}')
file.write(f'жута банана{linesep}')
16 changes: 16 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1003,3 +1003,19 @@ def test_infinite_loop():
assert 'Sequences' in filecontent
assert 'golf trip' in filecontent
assert 'sequences' in filecontent


def test_transliterate():
testargs = [
'demeuk', '-i', 'testdata/input55', '-o', 'testdata/output55', '-l', 'testdata/log55',
'--transliterate', 'sr', '--non-ascii'
]

with patch.object(sys, 'argv', testargs):
main()

with open('testdata/output55') as f:
filecontent = f.read()

assert 'zdravo prijatelju' in filecontent
assert 'zuta banana' in filecontent