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
Show file tree
Hide file tree
Changes from all commits
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 @@ -475,8 +476,6 @@ def extract_keywords(self, sentence, span_info=False, max_cost=0):
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 @@ -486,6 +485,8 @@ def extract_keywords(self, sentence, span_info=False, max_cost=0):
curr_cost = max_cost
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 @@ -507,6 +508,8 @@ def extract_keywords(self, sentence, span_info=False, max_cost=0):
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 @@ -564,6 +567,8 @@ def extract_keywords(self, sentence, span_info=False, max_cost=0):
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 @@ -606,8 +611,6 @@ def replace_keywords(self, sentence, max_cost=0):
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 @@ -617,6 +620,8 @@ def replace_keywords(self, sentence, max_cost=0):
curr_cost = max_cost
while idx < sentence_len:
char = sentence[idx]
if not self.case_sensitive:
char = char.lower()
# when we reach whitespace
if char not in self.non_word_boundaries:
current_word += orig_sentence[idx]
Expand All @@ -639,6 +644,8 @@ def replace_keywords(self, sentence, max_cost=0):
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:
current_word_continued += orig_sentence[idy]
# update longest sequence found
Expand Down Expand Up @@ -712,6 +719,8 @@ def replace_keywords(self, sentence, max_cost=0):
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
10 changes: 10 additions & 0 deletions test/keyword_extractor_test_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -476,5 +476,15 @@
"explanation": "",
"keywords": ["spring framework"],
"keywords_case_sensitive": ["spring framework"]
},
{
"sentence": "İ love Big Apple and Bay Area.",
"keyword_dict": {
"İ love": ["İ love"],
"Big Apple": ["Big Apple"]
},
"explanation": "Lowering keywords per character for correct span_info",
"keywords": ["İ love", "Big Apple"],
"keywords_case_sensitive": ["İ love", "Big Apple"]
}
]
6 changes: 2 additions & 4 deletions test/test_kp_extract_span.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,22 @@ def test_extract_keywords(self):
"""For each of the test case initialize a new KeywordProcessor.
Add the keywords the test case to KeywordProcessor.
Extract keywords and check if they match the expected result for the test case.

"""
for test_id, test_case in enumerate(self.test_cases):
keyword_processor = KeywordProcessor()
for key in test_case['keyword_dict']:
keyword_processor.add_keywords_from_list(test_case['keyword_dict'][key])
keywords_extracted = keyword_processor.extract_keywords(test_case['sentence'], span_info=True)
for kwd in keywords_extracted:
# returned keyword lowered should match the sapn from sentence
# returned keyword lowered should match the span from sentence
self.assertEqual(
kwd[0].lower(), test_case['sentence'].lower()[kwd[1]:kwd[2]],
kwd[0].lower(), test_case['sentence'][kwd[1]:kwd[2]].lower(),
"keywords span don't match the expected results for test case: {}".format(test_id))

def test_extract_keywords_case_sensitive(self):
"""For each of the test case initialize a new KeywordProcessor.
Add the keywords the test case to KeywordProcessor.
Extract keywords and check if they match the expected result for the test case.

"""
for test_id, test_case in enumerate(self.test_cases):
keyword_processor = KeywordProcessor(case_sensitive=True)
Expand Down