Comments (5)
Hmmm, looks easy to implement but tricky to test.
Do you have any example protected_patterns_file
and related text that contains those patterns that can be tested? If you do, then I could easy code it up and write the test =)
from sacremoses.
Sorry, I don't have an example of such files. My intention was to avoid URLs being tokenized and I was planning to use a regex like this one:
import re
regex = re.compile(
r'(?:http|ftp)s?://' # http:// or https://
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?))'
r'(?::\d+)?' # optional port
r'(?:/\w+)*'
r'(?:(?:\.[a-z]+)|/?)', re.IGNORECASE)
Then I found out that Moses tokenizer supported it and checked sacremoses for it because it's what we use.
P.S.: the regex above is used in our code but I don't know where I took it from; my past self wrote as a comment that it's loosely based on Django's URL validators but my present self can't see an evident connection with it.
from sacremoses.
@noe Django URL validators is a little too heavy to incorporate here.
I've tried incorporating the protected_patterns
feature from https://github.com/moses-smt/mosesdecoder/blob/master/scripts/tokenizer/basic-protected-patterns and a unittest case against the pattern you've listed in the previous comment in #46
On CLI:
$ pip install -U sacremoses>=0.0.19
$ wget https://raw.githubusercontent.com/moses-smt/mosesdecoder/master/scripts/tokenizer/basic-protected-patterns
$ sacremoses tokenize -j 4 -p basic-protected-patterns < big.txt > big.txt.tok
100%|██████████████████████████████████| 128457/128457 [00:05<00:00, 22183.94it/s
In Python:
from sacremoses import MosesTokenizer
moses = MosesTokenizer()
text = "this is a webpage https://stackoverflow.com/questions/6181381/how-to-print-variables-in-perl that kicks ass"
expected_tokens = ['this', 'is', 'a', 'webpage',
'https://stackoverflow.com/questions/6181381/how-to-print-variables-in-perl',
'that', 'kicks', 'ass']
assert moses.tokenize(text, protected_patterns=moses.BASIC_PROTECTED_PATTERNS) == expected_tokens
# Testing against pattern from https://github.com/alvations/sacremoses/issues/35
noe_patterns = [r'(?:http|ftp)s?://' # http:// or https://
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?))'
r'(?::\d+)?' # optional port
r'(?:/\w+)*'
r'(?:(?:\.[a-z]+)|/?)']
assert moses.tokenize(text, protected_patterns=noe_patterns) == expected_tokens
from sacremoses.
Added feature. Thanks again @noe ! c.f. #46
from sacremoses.
Hi @alvations , the above code snippet is just handling the case if there are string before and after the url and in fact it is taking the url only in specific format. So my question is if there's any way that it can handle any condition say like even if the text has only url it shouldn't tokenise or in some cases even if there's no text before or after the url?
Thanks in advance!
from sacremoses.
Related Issues (20)
- Bug: normalize prints extra newline HOT 1
- Is this normal to tokenize "His number is No.123." to ['His', 'number', 'is', 'No.123.']. Should it be ['His', 'number', 'is', 'No.123', '.']?
- bug report
- Truecaser seems do not process sentences beginning with quotation marks
- update release on github
- No detokenize_penn?
- can't tokenise the period properly
- deep detokenizer
- Chinese full stop “。” can't be split. HOT 1
- Trying to get in touch regarding a security issue HOT 2
- Is there a way to use sacremoses in java? HOT 1
- Cache downloaded tokenizer files HOT 1
- Which of the 100 languages used in mBERT are not supported by this tokenizer? HOT 1
- distutils is deprecated in Python 3.10 HOT 1
- New release? HOT 4
- Error with CLI `tokenize` using `click==8.1.3` HOT 10
- Is this package multi-threaded? HOT 1
- [Question] Why is <unk> token tokenized into 3 items? HOT 1
- Quiet flag has no effect on the detokenizer HOT 1
- add citation HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sacremoses.