cdactvm commited on
Commit
1029b3d
·
verified ·
1 Parent(s): 2b75669

Update convert2list.py

Browse files
Files changed (1) hide show
  1. convert2list.py +36 -55
convert2list.py CHANGED
@@ -1,55 +1,36 @@
1
- #!/usr/bin/env python
2
- # coding: utf-8
3
-
4
- # In[2]:
5
-
6
-
7
- # import nbimporter
8
- import nbimporter
9
- from Text2List import text_to_list
10
- def convert_to_list(text, text_list):
11
- matched_words = []
12
- unmatched_text = '' # To accumulate unmatched characters
13
-
14
- # Sort text_list by length in descending order to prioritize longest matches first
15
- text_list_sorted = sorted(text_list, key=len, reverse=True)
16
-
17
- while text:
18
- matched = False
19
- for word in text_list_sorted:
20
- if text.startswith(word):
21
- # Add any accumulated unmatched text before appending the matched word
22
- if unmatched_text:
23
- matched_words.append(unmatched_text)
24
- unmatched_text = '' # Reset unmatched text accumulator
25
-
26
- matched_words.append(word)
27
- text = text[len(word):] # Remove the matched part from text
28
- matched = True
29
- break
30
-
31
- if not matched:
32
- # Accumulate unmatched characters
33
- unmatched_text += text[0]
34
- text = text[1:]
35
-
36
- # If there's any remaining unmatched text, add it to the result
37
- if unmatched_text:
38
- matched_words.append(unmatched_text)
39
-
40
- # Join matched words and unmatched text with a space
41
- result = ' '.join(matched_words)
42
- return result
43
-
44
- # text = "जीरोएकदोतीनचारपांचछहसातआठनौदसजीरोएकदोतीनचारपांच"
45
-
46
- # if __name__=="__main__":
47
- # converted=convert_to_list(text, text_to_list())
48
- # print(converted)
49
-
50
-
51
- # In[ ]:
52
-
53
-
54
-
55
-
 
1
+
2
+ from Text2List import text_to_list
3
+ def convert_to_list(text, text_list):
4
+ matched_words = []
5
+ unmatched_text = '' # To accumulate unmatched characters
6
+
7
+ # Sort text_list by length in descending order to prioritize longest matches first
8
+ text_list_sorted = sorted(text_list, key=len, reverse=True)
9
+
10
+ while text:
11
+ matched = False
12
+ for word in text_list_sorted:
13
+ if text.startswith(word):
14
+ # Add any accumulated unmatched text before appending the matched word
15
+ if unmatched_text:
16
+ matched_words.append(unmatched_text)
17
+ unmatched_text = '' # Reset unmatched text accumulator
18
+
19
+ matched_words.append(word)
20
+ text = text[len(word):] # Remove the matched part from text
21
+ matched = True
22
+ break
23
+
24
+ if not matched:
25
+ # Accumulate unmatched characters
26
+ unmatched_text += text[0]
27
+ text = text[1:]
28
+
29
+ # If there's any remaining unmatched text, add it to the result
30
+ if unmatched_text:
31
+ matched_words.append(unmatched_text)
32
+
33
+ # Join matched words and unmatched text with a space
34
+ result = ' '.join(matched_words)
35
+ return result
36
+