aysan98 commited on
Commit
75779bf
·
verified ·
1 Parent(s): 81a0b1a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -7
app.py CHANGED
@@ -28,6 +28,28 @@ PERSIAN_STOP_WORDS = set([
28
  'this', 'by', 'an', 'be', 'from', 'or', 'but', 'not', 'has',
29
  ])
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  # الگوهای بازنشر خبر ایسنا
32
  ISNA_NEWS_PATTERNS = [
33
  r'/ایسنا',
@@ -71,19 +93,43 @@ def clean_text(text):
71
  def detect_text_language(text):
72
  if pd.isna(text):
73
  return 'سایر'
74
- text = re.sub(r'https?://\S+|@\S+', '', str(text))
 
 
 
 
 
75
  latin_count = len(re.findall(r'[A-Za-z]', text))
76
  arabic_script_count = len(re.findall(r'[\u0600-\u06FF]', text))
 
 
 
 
 
 
 
 
 
77
  persian_specific = len(re.findall(r'[پچژگکی]', text))
78
- arabic_specific = len(re.findall(r'[أإؤئءةىي]', text))
 
 
 
 
79
 
80
  if arabic_script_count == 0 and latin_count == 0:
81
  return 'سایر'
82
- if latin_count > arabic_script_count * 1.5:
 
 
 
 
 
 
 
 
83
  return 'انگلیسی'
84
- if arabic_script_count > 0:
85
- return 'عربی' if arabic_specific > persian_specific and arabic_specific >= 2 else 'فارسی'
86
- return 'انگلیسی'
87
 
88
  def is_valid_word(w):
89
  """کلمه معتبر: حداقل ۳ حرف و در لیست stop words نباشد"""
@@ -764,4 +810,4 @@ with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo:
764
  )
765
 
766
  if __name__ == "__main__":
767
- demo.launch()
 
28
  'this', 'by', 'an', 'be', 'from', 'or', 'but', 'not', 'has',
29
  ])
30
 
31
+ PERSIAN_LANGUAGE_WORDS = set([
32
+ 'و', 'در', 'به', 'از', 'که', 'این', 'را', 'با', 'است', 'برای',
33
+ 'آن', 'یک', 'خود', 'تا', 'کرد', 'بر', 'هم', 'نیز', 'اما', 'یا',
34
+ 'اگر', 'همه', 'بود', 'شد', 'می', 'هر', 'ما', 'او', 'ها', 'های',
35
+ 'شود', 'کرده', 'دارد', 'گفت', 'نمی', 'خواهد', 'کنیم', 'دارند',
36
+ 'ایران', 'خبر', 'گزارش', 'امروز', 'درباره', 'مردم', 'کشور'
37
+ ])
38
+
39
+ ARABIC_LANGUAGE_WORDS = set([
40
+ 'و', 'في', 'من', 'على', 'إلى', 'الى', 'عن', 'أن', 'ان', 'إن',
41
+ 'هذا', 'هذه', 'ذلك', 'تلك', 'الذي', 'التي', 'الذين', 'كان',
42
+ 'كانت', 'يكون', 'لا', 'ما', 'مع', 'كما', 'بعد', 'قبل', 'بين',
43
+ 'اليوم', 'قال', 'الله', 'الناس', 'الدولة', 'البلاد', 'خبر'
44
+ ])
45
+
46
+ ENGLISH_LANGUAGE_WORDS = set([
47
+ 'the', 'of', 'in', 'a', 'to', 'is', 'and', 'for', 'on', 'that',
48
+ 'are', 'was', 'it', 'with', 'he', 'she', 'they', 'we', 'i', 'you',
49
+ 'at', 'this', 'by', 'an', 'be', 'from', 'or', 'but', 'not', 'has',
50
+ 'have', 'as', 'will', 'can', 'about', 'news', 'today', 'people'
51
+ ])
52
+
53
  # الگوهای بازنشر خبر ایسنا
54
  ISNA_NEWS_PATTERNS = [
55
  r'/ایسنا',
 
93
  def detect_text_language(text):
94
  if pd.isna(text):
95
  return 'سایر'
96
+ text = re.sub(r'https?://\S+|@\S+|#', ' ', str(text).lower())
97
+ text = re.sub(r'\d+', ' ', text)
98
+ text = re.sub(r'\s+', ' ', text).strip()
99
+ if not text:
100
+ return 'سایر'
101
+
102
  latin_count = len(re.findall(r'[A-Za-z]', text))
103
  arabic_script_count = len(re.findall(r'[\u0600-\u06FF]', text))
104
+ useful_chars = latin_count + arabic_script_count
105
+ if useful_chars < 3:
106
+ return 'سایر'
107
+
108
+ tokens = re.findall(r'[A-Za-z]+|[\u0600-\u06FF]+', text)
109
+ english_hits = sum(1 for t in tokens if t in ENGLISH_LANGUAGE_WORDS)
110
+ persian_hits = sum(1 for t in tokens if t in PERSIAN_LANGUAGE_WORDS)
111
+ arabic_hits = sum(1 for t in tokens if t in ARABIC_LANGUAGE_WORDS)
112
+
113
  persian_specific = len(re.findall(r'[پچژگکی]', text))
114
+ arabic_specific = len(re.findall(r'[أإؤئءةىيىك]', text))
115
+
116
+ english_score = latin_count + english_hits * 8
117
+ persian_score = arabic_script_count + persian_specific * 6 + persian_hits * 8
118
+ arabic_score = arabic_script_count + arabic_specific * 6 + arabic_hits * 8
119
 
120
  if arabic_script_count == 0 and latin_count == 0:
121
  return 'سایر'
122
+ if latin_count >= 4 and english_score > max(persian_score, arabic_score) * 1.2:
123
+ return 'انگلیسی'
124
+ if arabic_script_count >= 4:
125
+ if arabic_score >= persian_score + 10 and arabic_hits >= 1:
126
+ return 'عربی'
127
+ if persian_score >= arabic_score + 4 or persian_specific > 0 or persian_hits >= 1:
128
+ return 'فارسی'
129
+ return 'سایر'
130
+ if latin_count >= 4:
131
  return 'انگلیسی'
132
+ return 'سایر'
 
 
133
 
134
  def is_valid_word(w):
135
  """کلمه معتبر: حداقل ۳ حرف و در لیست stop words نباشد"""
 
810
  )
811
 
812
  if __name__ == "__main__":
813
+ demo.launch()