Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

span_info on combined unicode characters #82

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions flashtext/keyword.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
import os
import string
import io
Expand Down Expand Up @@ -80,11 +81,11 @@ def __contains__(self, word):
>>> # True

"""
if not self.case_sensitive:
word = word.lower()
current_dict = self.keyword_trie_dict
len_covered = 0
for char in word:
if not self.case_sensitive:
char = char.lower()
if char in current_dict:
current_dict = current_dict[char]
len_covered += 1
Expand All @@ -108,11 +109,11 @@ def __getitem__(self, word):
>>> keyword_processor['Big Apple']
>>> # New York
"""
if not self.case_sensitive:
word = word.lower()
current_dict = self.keyword_trie_dict
len_covered = 0
for char in word:
if not self.case_sensitive:
char = char.lower()
if char in current_dict:
current_dict = current_dict[char]
len_covered += 1
Expand Down Expand Up @@ -141,10 +142,10 @@ def __setitem__(self, keyword, clean_name=None):
clean_name = keyword

if keyword and clean_name:
if not self.case_sensitive:
keyword = keyword.lower()
current_dict = self.keyword_trie_dict
for letter in keyword:
if not self.case_sensitive:
letter = letter.lower()
current_dict = current_dict.setdefault(letter, {})
if self._keyword not in current_dict:
status = True
Expand All @@ -166,11 +167,11 @@ def __delitem__(self, keyword):
"""
status = False
if keyword:
if not self.case_sensitive:
keyword = keyword.lower()
current_dict = self.keyword_trie_dict
character_trie_list = []
for letter in keyword:
if not self.case_sensitive:
letter = letter.lower()
if letter in current_dict:
character_trie_list.append((letter, current_dict))
current_dict = current_dict[letter]
Expand Down Expand Up @@ -471,8 +472,6 @@ def extract_keywords(self, sentence, span_info=False):
if not sentence:
# if sentence is empty or none just return empty list
return keywords_extracted
if not self.case_sensitive:
sentence = sentence.lower()
current_dict = self.keyword_trie_dict
sequence_start_pos = 0
sequence_end_pos = 0
Expand All @@ -481,6 +480,8 @@ def extract_keywords(self, sentence, span_info=False):
sentence_len = len(sentence)
while idx < sentence_len:
char = sentence[idx]
if not self.case_sensitive:
char = char.lower()
# when we reach a character that might denote word end
if char not in self.non_word_boundaries:

Expand All @@ -502,6 +503,8 @@ def extract_keywords(self, sentence, span_info=False):
idy = idx + 1
while idy < sentence_len:
inner_char = sentence[idy]
if not self.case_sensitive:
inner_char = inner_char.lower()
if inner_char not in self.non_word_boundaries and self._keyword in current_dict_continued:
# update longest sequence found
longest_sequence_found = current_dict_continued[self._keyword]
Expand Down Expand Up @@ -540,6 +543,8 @@ def extract_keywords(self, sentence, span_info=False):
idy = idx + 1
while idy < sentence_len:
char = sentence[idy]
if not self.case_sensitive:
char = char.lower()
if char not in self.non_word_boundaries:
break
idy += 1
Expand Down Expand Up @@ -582,8 +587,6 @@ def replace_keywords(self, sentence):
return sentence
new_sentence = []
orig_sentence = sentence
if not self.case_sensitive:
sentence = sentence.lower()
current_word = ''
current_dict = self.keyword_trie_dict
current_white_space = ''
Expand All @@ -592,6 +595,8 @@ def replace_keywords(self, sentence):
sentence_len = len(sentence)
while idx < sentence_len:
char = sentence[idx]
if not self.case_sensitive:
char = char.lower()
current_word += orig_sentence[idx]
# when we reach whitespace
if char not in self.non_word_boundaries:
Expand All @@ -614,6 +619,8 @@ def replace_keywords(self, sentence):
idy = idx + 1
while idy < sentence_len:
inner_char = sentence[idy]
if not self.case_sensitive:
inner_char = inner_char.lower()
current_word_continued += orig_sentence[idy]
if inner_char not in self.non_word_boundaries and self._keyword in current_dict_continued:
# update longest sequence found
Expand Down Expand Up @@ -662,6 +669,8 @@ def replace_keywords(self, sentence):
idy = idx + 1
while idy < sentence_len:
char = sentence[idy]
if not self.case_sensitive:
char = char.lower()
current_word += orig_sentence[idy]
if char not in self.non_word_boundaries:
break
Expand Down