Occasionally, we want to do a rudimentary parsing on English text; we separate the text into separate words.
# INPUT STRING
istring = "she sat down beside the child and sang a melody like a wind in summer
blowing softly."
# OUTPUT LIST
olist = ["she", "sat", "down", "beside", "the", "child", "and", "sang", "a", "melody", "like", "a", "wind", "in", "summer", "blowing", "softly", "."]
I wrote some code, but the implementation is ugly.
I was hoping to make make_special_iter
more readable to human beings.
Code
class StringPartitioner:
def __init__(self, istring:str):
self._istring = istring
self._it = self.make_special_iter(istring, self._char_to_class)
def __next__(self):
return next(self._it)
def __iter__(self):
return self
@classmethod
def make_special_iter(cls, istring:str, char_to_class):
assert(isinstance(istring, str))
try:
it = iter(istring)
buffer = list()
everything = list()
ch = next(it)
prev_class = char_to_class(ch)
buffer.append(ch)
for ch in it:
everything.append(ch)
cur_class = char_to_class(ch)
if cur_class != prev_class:
word = "".join(buffer)
buffer = list()
# print("word".ljust(30), repr(word))
yield word
buffer.append(ch)
prev_class = cur_class
yield "".join(buffer)
except StopIteration:
return
def _char_to_class(self, ch:str):
big_letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
sml_letters = big_letters.lower()
letters = big_letters + sml_letters
if ch in letters:
return ord("A")
return ord(ch)
def __str__(self):
return type(self).__name__ + "(" + repr(self._istring) + ")"
A Test Case
# Input for test one
itest1 = """she sat down beside the child and sang a melody like a wind in summer blowing softly; a melody from wild woods and grassy plain; a melody of valleys loved by children. She sang a melody now lost, but for dreams; a melody
along the edges of oblivion; now flashing like stellars of the night,
a glimpse of some golden moment, now passing swiftly out of remembrance again.
Her melody danced upon the green like little shining feet. Then her song, faded into stillness."""
# correct output for test one
otest1 = [
'she',
' ',
'sat',
' ',
'down',
' ',
'beside',
' ',
'the',
' ',
'child',
' ',
'and',
' ',
'sang',
' ',
'a',
' ',
'melody',
' ',
'like',
' ',
'a',
' ',
'wind',
' ',
'in',
' ',
'summer',
' ',
'blowing',
' ',
'softly',
';',
' ',
'a',
' ',
'melody',
' ',
'from',
' ',
'wild',
' ',
'woods',
' ',
'and',
' ',
'grassy',
' ',
'plain',
';',
' ',
'a',
' ',
'melody',
' ',
'of',
' ',
'valleys',
' ',
'loved',
' ',
'by',
' ',
'children',
'.',
' ',
'She',
' ',
'sang',
' ',
'a',
' ',
'melody',
' ',
'now',
' ',
'lost',
',',
' ',
'but',
' ',
'for',
' ',
'dreams',
';',
' ',
'a',
' ',
'melody',
'\n',
'along',
' ',
'the',
' ',
'edges',
' ',
'of',
' ',
'oblivion',
';',
' ',
'now',
' ',
'flashing',
' ',
'like',
' ',
'stellars',
' ',
'of',
' ',
'the',
' ',
'night',
',',
'\n',
'a',
' ',
'glimpse',
' ',
'of',
' ',
'some',
' ',
'golden',
' ',
'moment',
',',
' ',
'now',
' ',
'passing',
' ',
'swiftly',
' ',
'out',
' ',
'of',
' ',
'remembrance',
' ',
'again',
'.',
'\n',
'Her',
' ',
'melody',
' ',
'danced',
' ',
'upon',
' ',
'the',
' ',
'green',
' ',
'like',
' ',
'little',
' ',
'shining',
' ',
'feet',
'.',
' ',
'Then',
' ',
'her',
' ',
'song',
',',
' ',
'faded',
' ',
'into',
' ',
'stillness',
'.'
]
NLTK
, but instead, you wrote the letters out of order asNTLK
. Note thatNLTK
is a sub-sequence formed from the string of textNATURAL LANGUAGE TOOL KIT
by deleting 21 letters. In general, the name of every python library every written in a sub-sequence of a fully-formed English phrase. Perhaps, some day, people can writeimport natural_language_tool_kit
or writeimport nltk
. People who are in a hurry write,import nltk
. However,import natural_language_tool_kit
is more self-documenting or self-explanatory. \$\endgroup\$