Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,51 +1,58 @@
|
|
1 |
-
|
2 |
import nltk
|
3 |
-
from nltk.tokenize import word_tokenize,
|
4 |
from nltk.tokenize.treebank import TreebankWordDetokenizer
|
|
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
st.write('Regression Tests: NLTKWordTokenizer\nTokenizing some test strings.')
|
9 |
-
|
10 |
s1 = "On a $50,000 mortgage of 30 years at 8 percent, the monthly payment would be $366.88."
|
11 |
-
|
12 |
|
13 |
s2 = "\"We beat some pretty good teams to get here,\" Slocum said."
|
14 |
-
|
15 |
|
16 |
s3 = "Well, we couldn't have this predictable, cliche-ridden, \"Touched by an Angel\" (a show creator John Masius worked on) wanna-be if she didn't."
|
17 |
-
|
18 |
|
19 |
s4 = "I cannot cannot work under these conditions!"
|
20 |
-
|
21 |
|
22 |
s5 = "The company spent $30,000,000 last year."
|
23 |
-
|
24 |
|
25 |
s6 = "The company spent 40.75% of its income last year."
|
26 |
-
|
27 |
|
28 |
s7 = "He arrived at 3:00 pm."
|
29 |
-
|
30 |
|
31 |
s8 = "I bought these items: books, pencils, and pens."
|
32 |
-
|
33 |
|
34 |
s9 = "Though there were 150, 100 of them were old."
|
35 |
-
|
36 |
|
37 |
s10 = "There were 300,000, but that wasn't enough."
|
38 |
-
|
39 |
|
40 |
s11 = "It's more'n enough."
|
41 |
-
|
42 |
|
43 |
s = '''Good muffins cost $3.88\nin New (York). Please (buy) me\ntwo of them.\n(Thanks).'''
|
44 |
-
expected = [(0, 4), (5, 12), (13, 17), (18, 19), (19, 23),
|
45 |
-
|
|
|
|
|
|
|
46 |
|
47 |
-
expected = ['Good', 'muffins', 'cost', '$', '3.88', 'in',
|
48 |
-
|
|
|
|
|
49 |
|
50 |
sx1 = '\xabNow that I can do.\xbb'
|
51 |
-
expected = ['\xab', 'Now', 'that
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import required libraries
|
2 |
import nltk
|
3 |
+
from nltk.tokenize import word_tokenize, sent_tokenize, regexp_tokenize, TweetTokenizer
|
4 |
from nltk.tokenize.treebank import TreebankWordDetokenizer
|
5 |
+
from nltk.tokenize import NLTKWordTokenizer
|
6 |
|
7 |
+
# Test cases for NLTKWordTokenizer
|
|
|
|
|
|
|
8 |
s1 = "On a $50,000 mortgage of 30 years at 8 percent, the monthly payment would be $366.88."
|
9 |
+
print(word_tokenize(s1))
|
10 |
|
11 |
s2 = "\"We beat some pretty good teams to get here,\" Slocum said."
|
12 |
+
print(word_tokenize(s2))
|
13 |
|
14 |
s3 = "Well, we couldn't have this predictable, cliche-ridden, \"Touched by an Angel\" (a show creator John Masius worked on) wanna-be if she didn't."
|
15 |
+
print(word_tokenize(s3))
|
16 |
|
17 |
s4 = "I cannot cannot work under these conditions!"
|
18 |
+
print(word_tokenize(s4))
|
19 |
|
20 |
s5 = "The company spent $30,000,000 last year."
|
21 |
+
print(word_tokenize(s5))
|
22 |
|
23 |
s6 = "The company spent 40.75% of its income last year."
|
24 |
+
print(word_tokenize(s6))
|
25 |
|
26 |
s7 = "He arrived at 3:00 pm."
|
27 |
+
print(word_tokenize(s7))
|
28 |
|
29 |
s8 = "I bought these items: books, pencils, and pens."
|
30 |
+
print(word_tokenize(s8))
|
31 |
|
32 |
s9 = "Though there were 150, 100 of them were old."
|
33 |
+
print(word_tokenize(s9))
|
34 |
|
35 |
s10 = "There were 300,000, but that wasn't enough."
|
36 |
+
print(word_tokenize(s10))
|
37 |
|
38 |
s11 = "It's more'n enough."
|
39 |
+
print(word_tokenize(s11))
|
40 |
|
41 |
s = '''Good muffins cost $3.88\nin New (York). Please (buy) me\ntwo of them.\n(Thanks).'''
|
42 |
+
expected = [(0, 4), (5, 12), (13, 17), (18, 19), (19, 23),
|
43 |
+
(24, 26), (27, 30), (31, 32), (32, 36), (36, 37), (37, 38),
|
44 |
+
(40, 46), (47, 48), (48, 51), (51, 52), (53, 55), (56, 59),
|
45 |
+
(60, 62), (63, 68), (69, 70), (70, 76), (76, 77), (77, 78)]
|
46 |
+
print(list(NLTKWordTokenizer().span_tokenize(s)) == expected)
|
47 |
|
48 |
+
expected = ['Good', 'muffins', 'cost', '$', '3.88', 'in',
|
49 |
+
'New', '(', 'York', ')', '.', 'Please', '(', 'buy', ')',
|
50 |
+
'me', 'two', 'of', 'them.', '(', 'Thanks', ')', '.']
|
51 |
+
print([s[start:end] for start, end in NLTKWordTokenizer().span_tokenize(s)] == expected)
|
52 |
|
53 |
sx1 = '\xabNow that I can do.\xbb'
|
54 |
+
expected = ['\xab', 'Now', 'that', 'I', 'can', 'do', '.', '\xbb']
|
55 |
+
print(word_tokenize(sx1) == expected)
|
56 |
+
|
57 |
+
sx2 = 'The unicode 201C and 201D \u201cLEFT(RIGHT) DOUBLE QUOTATION MARK\u201d is also OPEN_PUNCT and CLOSE_PUNCT.'
|
58 |
+
expected = ['The', 'unicode', '201C', 'and', '201D', '\u201c', 'LEFT', '(', 'RIGHT', ')', 'DOUBLE', 'QUOTATION', 'MARK', '\u201d', 'is', 'also', 'OPEN_PUNCT
|