imbajin commented on code in PR #282:
URL: 
https://github.com/apache/incubator-hugegraph-ai/pull/282#discussion_r2444789487


##########
hugegraph-llm/src/hugegraph_llm/operators/common_op/nltk_helper.py:
##########
@@ -47,11 +50,64 @@ def stopwords(self, lang: str = "chinese") -> List[str]:
             try:
                 nltk.data.find("corpora/stopwords")
             except LookupError:
-                nltk.download("stopwords", download_dir=nltk_data_dir)
+                try:
+                    log.info("Start download nltk package stopwords")
+                    nltk.download("stopwords", download_dir=nltk_data_dir, 
quiet=False)
+                    log.debug("NLTK package stopwords is already downloaded")
+                except (URLError, HTTPError, PermissionError) as e:
+                    log.warning("Can't download package stopwords as error: 
%s", e)
+        try:
             self._stopwords[lang] = stopwords.words(lang)
+        except LookupError as e:
+            log.warning("NLTK stopwords for lang=%s not found: %s; using empty 
list", lang, e)

Review Comment:
   ‼️ **Critical Issue: Silent failure in stopwords handling**
   
   The current error handling allows the system to continue with an empty 
stopwords list when NLTK data is unavailable, which could significantly degrade 
keyword extraction quality without clear user notification.
   
   **Problem:**
   ```python
   except LookupError as e:
       log.warning("NLTK stopwords for lang=%s not found: %s; using empty 
list", lang, e)
       self._stopwords[lang] = []
   ```
   
   **Recommendation:**
   Consider throwing an exception or providing a more prominent error (e.g., 
`log.error`) since operating without stopwords fundamentally changes extraction 
behavior. At minimum, this warning should be surfaced to the user through the 
API response.



##########
hugegraph-llm/src/hugegraph_llm/operators/document_op/textrank_word_extract.py:
##########
@@ -0,0 +1,151 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import re
+from collections import defaultdict
+from typing import Dict
+
+import igraph as ig
+import jieba.posseg as pseg
+import nltk
+import regex
+
+from hugegraph_llm.operators.common_op.nltk_helper import NLTKHelper
+from hugegraph_llm.utils.log import log
+
+
+class MultiLingualTextRank:
+    def __init__(self, keyword_num: int = 5, window_size: int = 3):
+        self.top_k = keyword_num
+        self.window = window_size if 0 < window_size <= 10 else 3
+        self.graph = None
+        self.max_len = 100
+
+        self.pos_filter = {
+            'chinese': ('n', 'nr', 'ns', 'nt', 'nrt', 'nz', 'v', 'vd', 'vn', 
"eng", "j", "l"),
+            'english': ('NN', 'NNS', 'NNP', 'NNPS', 'VB', 'VBG', 'VBN', 'VBZ')
+        }
+        self.rules = [r"https?://\S+|www\.\S+",
+                      r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b",
+                      r"\b\w+(?:[-’\']\w+)+\b",
+                      r"\b\d+[,.]\d+\b"]
+
+    def _word_mask(self, text):
+
+        placeholder_id_counter = 0
+        placeholder_map = {}
+
+        def _create_placeholder(match_obj):
+            nonlocal placeholder_id_counter
+            original_word = match_obj.group(0)
+            _placeholder = f" __shieldword_{placeholder_id_counter}__ "

Review Comment:
   ‼️ **Critical Issue: Placeholder collision vulnerability**
   
   The placeholder format `__shieldword_{counter}__` could collide with actual 
text content containing similar patterns, causing incorrect word 
masking/unmasking.
   
   **Example failure case:**
   If the input text already contains "__shieldword_0__", the system would 
incorrectly treat it as a placeholder.
   
   **Recommendation:**
   Use UUID-based placeholders or add a unique session prefix:
   ```python
   import uuid
   session_id = uuid.uuid4().hex[:8]
   _placeholder = f" __shield_{session_id}_{placeholder_id_counter}__ "
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to