IndexError: string index out of range
See original GitHub issueThere seems to be a bug when running the following test code:
import contractions
test_str = "He continued his studies at Maltepe Military High School in İzmir and then at the Turkish Military Academy, graduating in 2000, after which he returned to Azerbaijan."
contractions.fix(test_str)
Here is the Traceback:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "{...}\site-packages\contractions\__init__.py", line 239, in fix
return ts.replace(s)
File "{...}\site-packages\textsearch\__init__.py", line 561, in replace
start, stop, result = handler(text, start, stop, norm)
File "{...}\site-packages\textsearch\__init__.py", line 371, in bounds_check
if len(text) != stop and text[stop] in self.right_bound_chars:
IndexError: string index out of range
Could it be due to the special characters in test_str
, and if so, how we deal with it?
Thanks!
Edit: Also fails for the following examples:
contractions.fix("Imishli (İmişli) is a rayon of Azerbaijan.")
contractions.fix("Fenerbahçe have several supporter organisations, including Genç Fenerbahçeliler (GFB), Kill For You (KFY), Ultras Fener, Antu/Fenerlist, EuroFeb, Group CK (Cefakâr Kanaryalar), 1907 ÜNİFEB, Vamos Bien, and SUADFEB.")
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (3 by maintainers)
Top Results From Across the Web
String Index Out Of Range Python - MindMajix Community
The string index out of range indicates that the index you are attempting to reach does not exist. Within a string, that implies...
Read more >Python error: "IndexError: string index out of range"
You are iterating over one string ( word ), but then using the index into that to look up a character in so_far...
Read more >IndexError: string index out of range - Net-Informations.Com
The string index out of range means that the index you are trying to access does not exist. In a string, that means...
Read more >Python TypeError: string index out of range Solution
The “TypeError: string index out of range” error is raised when you try to access an item at an index position that does...
Read more >IndexError: string index out of range - STechies
To access a specific element, you have to mention its index value. But in some cases you may encounter an error called “IndexError...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@kootenpv
The bug at display is definitely tricky - but it boils down to the following:
You may wonder where exactly this produces problems. Well, first,
fix
ofcontradictions
gets called. This function is awfully simple, and just calls thereplace
method of aTextSearch
object. (For reference:) https://github.com/kootenpv/textsearch/blob/92b6584513e9b8afc98268e91a8a434f3495b5f5/textsearch/__init__.py#L540-L584In particular, on line 553 the input text is converted to lowercase. With the example of
contractions.fix("İ jan.")
, the length of_text
is one larger than the length oftext
. This length is then extracted on line 555 byself.automaton.iter
. This length is used inhandler
, which isself.bounds_check
at one point. Thisself.bounds_check
then uses the originaltext
, and the increased length of_text
.Then, in
self.bounds_check
, this increased length is used alongside the originaltext
, causing the Index Error. This issue is thrown on line 371 here: https://github.com/kootenpv/textsearch/blob/92b6584513e9b8afc98268e91a8a434f3495b5f5/textsearch/__init__.py#L370-L371This is more of an issue with
TextSearch
from https://github.com/kootenpv/textsearch.What’s the example?