python-negar 1.2.15__tar.gz → 1.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,6 @@
1
+ 1.3 - 2025-01-01
2
+ -- Enhanced the handling of immutable words to achieve greater accuracy in results.
3
+
1
4
  1.2.15 - 2024-12-31
2
5
  -- Fixed issue #24 and added several new immutable words.
3
6
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-negar
3
- Version: 1.2.15
3
+ Version: 1.3
4
4
  Summary: Negar is a spell corrector and Persian text editor
5
5
  Home-page: http://github.com/shahinism/python-negar
6
6
  Author: Shahin Azad
@@ -13,7 +13,7 @@ Classifier: Environment :: Console
13
13
  Classifier: License :: OSI Approved :: GNU General Public License v3 (GPLv3)
14
14
  Classifier: Programming Language :: Python
15
15
  Classifier: Programming Language :: Python :: 3
16
- Requires-Python: >=3.6
16
+ Requires-Python: >=3.8
17
17
  Description-Content-Type: text/markdown
18
18
  License-File: LICENSE
19
19
  License-File: AUTHORS
@@ -1,7 +1,7 @@
1
1
  import platform
2
2
  from pathlib import Path
3
3
 
4
- __version__ = "1.2.15"
4
+ __version__ = "1.3"
5
5
 
6
6
  DATAFILE = Path(__file__).parent.absolute() / "data/immutable.words"
7
7
  APPDATA = "AppData/Roaming/" if platform.system() == "Windows" else "."
@@ -92,6 +92,7 @@
92
92
  الشان
93
93
  الکتر
94
94
  الکترومتر
95
+ الکترومتری
95
96
  الکتری
96
97
  الماس‌نشان
97
98
  المان
@@ -331,6 +332,7 @@
331
332
  بیایی
332
333
  بیایید
333
334
  بیاییم
335
+ بیاییم
334
336
  بیبسکو
335
337
  بیبل
336
338
  بی‌بها
@@ -1169,6 +1171,7 @@
1169
1171
  کودکستان
1170
1172
  کوهستان
1171
1173
  کیلومتر
1174
+ کیلومتری
1172
1175
  کیها
1173
1176
  گالوانومتر
1174
1177
  گانگستر
@@ -1200,6 +1203,8 @@
1200
1203
  لیموعمان
1201
1204
  ماتریس
1202
1205
  متان
1206
+ متر
1207
+ متری
1203
1208
  متشابها
1204
1209
  متکلمان
1205
1210
  متها
@@ -57,7 +57,7 @@ class PersianEditor:
57
57
  self.cleanup()
58
58
 
59
59
  def cleanup(self):
60
- self._handle_urls(State.save)
60
+ self.__handle_urls__(State.save)
61
61
  # fix punctuation spaces at first
62
62
  # : ; , ! ? and their Persian counterparts should have one space after and no space before
63
63
  # An exception is triple dots, which should be handled first
@@ -77,10 +77,12 @@ class PersianEditor:
77
77
  self.char_validator() if self._fix_misc_non_persian_chars else None
78
78
  self.fix_arabic_numbers() if self._fix_arabic_numbers else None
79
79
  self.fix_english_numbers() if self._fix_english_numbers else None
80
+ self.__handle_immutable_words__(State.save)
80
81
  self.fix_prefix_spacing() if self._fix_prefix_spacing else None
81
82
  self.fix_prefix_separate() if self._fix_prefix_separate else None
82
83
  self.fix_suffix_spacing() if self._fix_suffix_spacing else None
83
84
  self.fix_suffix_separate() if self._fix_suffix_separate else None
85
+ self.__handle_immutable_words__(State.restore)
84
86
  self.fix_spacing_for_braces_and_quotes() \
85
87
  if self._fix_spacing_for_braces_and_quotes else None
86
88
  self.cleanup_redundant_zwnj()
@@ -92,7 +94,7 @@ class PersianEditor:
92
94
  )
93
95
  if self._trim_leading_trailing_whitespaces:
94
96
  self.text = "\n".join([line.strip() for line in self.text.split("\n")])
95
- self._handle_urls(State.restore)
97
+ self.__handle_urls__(State.restore)
96
98
  return self.text
97
99
 
98
100
  def __repr__(self):
@@ -100,7 +102,7 @@ class PersianEditor:
100
102
 
101
103
  __str__ = __repr__
102
104
 
103
- def _handle_urls(self, state):
105
+ def __handle_urls__(self, state):
104
106
  """Remove URLs and putting them back at the end of the process."""
105
107
  if state == State.save:
106
108
  self.urls = list(set(re.findall(URLREGX, self.text, re.M | re.I | re.X)))
@@ -111,6 +113,19 @@ class PersianEditor:
111
113
  for i, url in enumerate(self.urls):
112
114
  self.text = re.sub(f"__URL__#{i}__", url, self.text)
113
115
 
116
+ def __handle_immutable_words__(self, state):
117
+ """Remove URLs and putting them back at the end of the process."""
118
+ if state == State.save:
119
+ self.immutable_words = {}
120
+ words = self.text.split()
121
+ for i, word in enumerate(words):
122
+ if (word:=word.strip()) in ImmutableWords.get():
123
+ self.immutable_words[i] = word
124
+ self.text = regex.sub(rf"\b{word}\b", rf"__IMM__#{i}__", self.text)
125
+ if state == State.restore:
126
+ for imm_key, imm_word in self.immutable_words.items():
127
+ self.text = re.sub(f"__IMM__#{imm_key}__", imm_word, self.text)
128
+
114
129
  def fix_dashes(self):
115
130
  """Replace double and triple dashes with `ndash` and `mdash`, respectively."""
116
131
  self.text = re.sub(r"-{3}", r"—", self.text)
@@ -193,24 +208,8 @@ class PersianEditor:
193
208
  """,
194
209
  re.VERBOSE,
195
210
  ) # \b for words like سهمیه
196
-
197
- # Replace backslashes with a temporary placeholder
198
- text = self.text.replace("\\", "PN__BACKSLASH__PN")
199
- wlist = text.split()
200
- for word in wlist:
201
- if word.strip() in ImmutableWords.get():
202
- continue
203
- regx_iter = regx.finditer(word)
204
- for p in regx_iter:
205
- # Checks that the prefix (mi* nemi* bi*) is part a a word or not, like میلاد.
206
- if p.group() not in ImmutableWords.get():
207
- text = re.sub(
208
- re.escape(p.group()),
209
- p.group(1) + "\u200c" + p.group(2),
210
- text,
211
- )
212
- # Restore the original backslashes
213
- self.text = text.replace("PN__BACKSLASH__PN", "\\")
211
+ # Checks that the prefix (mi*, nemi*, and bi*) is part a a word or not, like میلاد.
212
+ self.__immutability_check__(regx)
214
213
 
215
214
  def fix_suffix_spacing(self):
216
215
  """Put ZWNJ between a word and its suffix (*ha[ye] *tar[in])."""
@@ -258,21 +257,22 @@ class PersianEditor:
258
257
  )\b""",
259
258
  re.VERBOSE,
260
259
  )
260
+ # Checks that the suffix (tar*, haye*) is part of a word or not, like بهتر.
261
+ self.__immutability_check__(regx)
262
+
263
+ def __immutability_check__(self, regx: re.Pattern):
261
264
  # Replace backslashes with a temporary placeholder
262
265
  text = self.text.replace("\\", "PN__BACKSLASH__PN")
263
266
  wlist = text.split()
264
267
  for word in wlist:
265
- if word.strip() in ImmutableWords.get():
266
- continue
267
268
  regx_iter = regx.finditer(word)
268
269
  for p in regx_iter:
269
- # Checks that the suffix (tar* haye*) is part of a word or not, like بهتر.
270
- if p.group() not in ImmutableWords.get():
271
- text = re.sub(
272
- re.escape(p.group()),
273
- p.group(1) + "\u200c" + p.group(2),
274
- text,
275
- )
270
+ # if p.group() not in ImmutableWords.get():
271
+ text = re.sub(
272
+ rf"\b{re.escape(p.group())}\b",
273
+ p.group(1) + "\u200c" + p.group(2),
274
+ text,
275
+ )
276
276
  # Restore the original backslashes
277
277
  self.text = text.replace("PN__BACKSLASH__PN", "\\")
278
278
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-negar
3
- Version: 1.2.15
3
+ Version: 1.3
4
4
  Summary: Negar is a spell corrector and Persian text editor
5
5
  Home-page: http://github.com/shahinism/python-negar
6
6
  Author: Shahin Azad
@@ -13,7 +13,7 @@ Classifier: Environment :: Console
13
13
  Classifier: License :: OSI Approved :: GNU General Public License v3 (GPLv3)
14
14
  Classifier: Programming Language :: Python
15
15
  Classifier: Programming Language :: Python :: 3
16
- Requires-Python: >=3.6
16
+ Requires-Python: >=3.8
17
17
  Description-Content-Type: text/markdown
18
18
  License-File: LICENSE
19
19
  License-File: AUTHORS
@@ -33,7 +33,7 @@ setup(
33
33
  },
34
34
  long_description_content_type="text/markdown",
35
35
  long_description=open("README.md", encoding="utf8").read(),
36
- python_requires=">=3.6",
36
+ python_requires=">=3.8",
37
37
  classifiers=[
38
38
  "Environment :: Console",
39
39
  "License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes