mammothb / symspellpy

Python port of SymSpell: 1 million times faster spelling correction & fuzzy search through Symmetric Delete spelling correction algorithm
MIT License
800 stars 122 forks source link

How do I access the values in <symspellpy.symspellpy.SuggestItem object at 0x7f4857ac8080>? #89

Closed yudhiesh closed 3 years ago

yudhiesh commented 3 years ago

I am looping over a list of words to make corrections on their spelling but I am facing this issue where all the outputs are in the form of <symspellpy.symspellpy.SuggestItem object at 0x7f4857ac8080>.

I am using version 6.7.0.

Sample list of words:

['NEW COVER Once Covid passes will the 2020s roar the way the 1920s did Its not impossible By copying what was done right in the Roaring Twenties and avoiding what went wrong Americans can make the 2020s a successby toda What Leon Black got for paying Jeffrey Epstein 158 million Robinhood traders fell in love with stocks in 2020]

Code:

sample = [words for segments in sample for words in segments.split()]
suggestions = [sym_spell.lookup(word, Verbosity.CLOSEST,
                               max_edit_distance=2) for word in sample]

# display suggestion term, term frequency, and edit distance
for suggestion in suggestions:
    print(suggestion)

Output:

[<symspellpy.symspellpy.SuggestItem object at 0x7f4857ac8630>, <symspellpy.symspellpy.SuggestItem object at 0x7f4857ac85f8>, <symspellpy.symspellpy.SuggestItem object at 0x7f4857ac8668>, <symspellpy.symspellpy.SuggestItem object at 0x7f4857ac8710>, <symspellpy.symspellpy.SuggestItem object at 0x7f4857ac86a0>, <symspellpy.symspellpy.SuggestItem object at 0x7f4857ac8978>, <symspellpy.symspellpy.SuggestItem object at 0x7f4857ac86d8>, <symspellpy.symspellpy.SuggestItem object at 0x7f4857ac8518>, <symspellpy.symspellpy.SuggestItem object at 0x7f4857ac89b0>, <symspellpy.symspellpy.SuggestItem object at 0x7f4857ac89e8>, <symspellpy.symspellpy.SuggestItem object at 0x7f4857ac84a8>, <symspellpy.symspellpy.SuggestItem object at 0x7f4857ac8748>, <symspellpy.symspellpy.SuggestItem object at 0x7f4857ac8a20>, <symspellpy.symspellpy.SuggestItem object at 0x7f4857ac87b8>, <symspellpy.symspellpy.SuggestItem object at 0x7f4857ac8a58>, <symspellpy.symspellpy.SuggestItem object at 0x7f4857ac8a90>, <symspellpy.symspellpy.SuggestItem object at 0x7f4857ac8780>, <symspellpy.symspellpy.SuggestItem object at 0x7f4857ac87f0>, <symspellpy.symspellpy.SuggestItem object at 0x7f4857ac8ac8>, <symspellpy.symspellpy.SuggestItem object at 0x7f4857ac8b00>, <symspellpy.symspellpy.SuggestItem object at 0x7f4857ac8b38>, <symspellpy.symspellpy.SuggestItem object at 0x7f4857ac8898>, <symspellpy.symspellpy.SuggestItem object at 0x7f4857ac84e0>, <symspellpy.symspellpy.SuggestItem object at 0x7f4857ac8b70>, <symspellpy.symspellpy.SuggestItem object at 0x7f4857ac88d0>, <symspellpy.symspellpy.SuggestItem object at 0x7f4857ac8908>, <symspellpy.symspellpy.SuggestItem object at 0x7f4857ac8940>, <symspellpy.symspellpy.SuggestItem object at 0x7f4857ac8ba8>]
mammothb commented 3 years ago

I think you can use term, distance and count to access the data https://symspellpy.readthedocs.io/en/latest/api/symspellpy.html#data-classes

yudhiesh commented 3 years ago

I think you can use term, distance and count to access the data https://symspellpy.readthedocs.io/en/latest/api/symspellpy.html#data-classes

Do I unpack these from the returned SuggestItem?

mammothb commented 3 years ago

Yea, so something like

for suggestion in suggestions:
    print(suggestion.term, suggestion.distance, suggestion.count)

should work

yudhiesh commented 3 years ago

Thanks that worked just needed to alter it as each suggestion is a list in itself

for suggestion in suggestions:
  for sug in suggestion:
    print(sug.term, sug.distance, sug.count)