cdactvm commited on
Commit
8811fa1
·
verified ·
1 Parent(s): 6f9ee50

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
+ # import nbimporter
2
+ import nbimporter
3
+ from Text2List import text_to_list
4
+ def convert_to_list(text, text_list):
5
+ matched_words = []
6
+ unmatched_text = '' # To accumulate unmatched characters
7
+
8
+ # Sort text_list by length in descending order to prioritize longest matches first
9
+ text_list_sorted = sorted(text_list, key=len, reverse=True)
10
+
11
+ while text:
12
+ matched = False
13
+ for word in text_list_sorted:
14
+ if text.startswith(word):
15
+ # Add any accumulated unmatched text before appending the matched word
16
+ if unmatched_text:
17
+ matched_words.append(unmatched_text)
18
+ unmatched_text = '' # Reset unmatched text accumulator
19
+
20
+ matched_words.append(word)
21
+ text = text[len(word):] # Remove the matched part from text
22
+ matched = True
23
+ break
24
+
25
+ if not matched:
26
+ # Accumulate unmatched characters
27
+ unmatched_text += text[0]
28
+ text = text[1:]
29
+
30
+ # If there's any remaining unmatched text, add it to the result
31
+ if unmatched_text:
32
+ matched_words.append(unmatched_text)
33
+
34
+ # Join matched words and unmatched text with a space
35
+ result = ' '.join(matched_words)
36
+ return result