PgsFile 0.2.0__py3-none-any.whl → 0.2.2__py3-none-any.whl

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.

Potentially problematic release.


This version of PgsFile might be problematic. Click here for more details.

PgsFile/PgsFile.py CHANGED
@@ -62,53 +62,82 @@ def decimal_to_percent(decimal):
62
62
  percent_str="{:.2f}%".format(percent)
63
63
  return percent_str
64
64
 
65
+ try:
66
+ import chardet
67
+ except ImportError:
68
+ print("chardet library not found. Installing...")
69
+ install_package("chardet")
70
+ import chardet # Re-import after installation
71
+
65
72
  def get_data_text(path):
66
73
  '''
67
74
  Parameters
68
75
  ----------
69
- path : TYPE string
70
- DESCRIPTION.
71
- Using path to get data from a single txt file. eg. raw_text.txt
72
- Theoretically, it supports common text encoding formats, like utf-8, unicode, ansi, gbk etc.
76
+ path : str
77
+ The path to the text file to read.
78
+ Supports common text encoding formats, like utf-8, unicode, ansi, gbk etc.
73
79
 
74
80
  Returns
75
81
  -------
76
- text : TYPE string
77
- DESCRIPTION.
78
- It returns all the text content from the target file.
79
-
82
+ text : str
83
+ The text content from the target file.
84
+ Returns None if the file cannot be read with any of the specified encodings.
80
85
  '''
81
- for encoding in encodings:
82
- try:
83
- with open(path, 'r', encoding=encoding, errors='ignore') as f:
84
- return f.read()
85
- except UnicodeDecodeError:
86
- continue
87
- return None
88
-
86
+ # Get the size of the file
87
+ file_size = os.path.getsize(path)
88
+
89
+ # Read the file to detect encoding
90
+ with open(path, 'rb') as f:
91
+ if file_size < 1024:
92
+ raw_data = f.read() # Read the entire file if it's small
93
+ else:
94
+ raw_data = f.read(1024) # Read the first 1024 bytes
95
+
96
+ result = chardet.detect(raw_data)
97
+ encoding = result['encoding']
98
+
99
+ # Read the entire file using the detected encoding
100
+ if encoding:
101
+ with open(path, 'r', encoding=encoding, errors="ignore") as f:
102
+ return f.read()
103
+ else:
104
+ return None
105
+
89
106
  def get_data_lines(path):
90
107
  '''
91
108
  Parameters
92
109
  ----------
93
- path : TYPE string
94
- DESCRIPTION.
95
- Using path to get data from a single txt file. eg. raw_text.txt
96
- Theoretically, it supports common the text encoding formats, like utf-8, unicode, ansi, gbk etc.
110
+ path : str
111
+ The path to the text file to read.
112
+ Supports common text encoding formats, like utf-8, unicode, ansi, gbk etc.
97
113
 
98
114
  Returns
99
115
  -------
100
- lines : TYPE list
101
- DESCRIPTION.
102
- It returns a list containing all the paragraphs of the target file.
103
-
116
+ lines : list
117
+ A list containing all the non-empty paragraphs of the target file.
104
118
  '''
105
- for encoding in encodings:
106
- try:
107
- with open(path, 'r', encoding=encoding, errors='ignore') as f:
108
- return [l.strip() for l in f.readlines() if len(l.strip()) !=0]
109
- except UnicodeDecodeError:
110
- continue
111
- return None
119
+
120
+ # Get the size of the file
121
+ file_size = os.path.getsize(path)
122
+
123
+ # Read the file to detect encoding
124
+ with open(path, 'rb') as f:
125
+ if file_size < 1024:
126
+ raw_data = f.read() # Read the entire file if it's small
127
+ else:
128
+ raw_data = f.read(1024) # Read the first 1024 bytes
129
+
130
+ result = chardet.detect(raw_data)
131
+ encoding = result['encoding']
132
+
133
+ # Read the entire file using the detected encoding
134
+ if encoding:
135
+ with open(path, 'r', encoding=encoding, errors="ignore") as f:
136
+ lines = [l.strip() for l in f.readlines() if len(l.strip()) != 0]
137
+ return lines
138
+ else:
139
+ return None
140
+
112
141
 
113
142
  def write_to_txt(file_path,text,mode=None,encoding=None):
114
143
  '''
@@ -457,7 +486,7 @@ BigPunctuation="""!"#$&\'()*+,-/:;<=>?@[\\]^_`{|}.%~"#$%&'?。()
457
486
  StopTags="""◆: 、/ 。/ ---/ -/ --/ -- :/ ;/ ?/ ??/ ?┖ @/ [/ ]/ ^/ ‘/ ’/ "/ "/ 〈/ 〉/ 《/ 》/ 【/ 】/ >/ ∶/ ■/ ●/ ·/ …/ !/ #/ %,/ %/ \'/ (/ )/ */ +/ ,/ -/ // np v n w m a x t q j ni ns d i f u p g nz c r id s k h o e / #?/ --/""" #用来停用词性标注
458
487
  Special="""∶ ■ ● ① ② ③ × ℃ Ⅲ ④ ⑤ ◆ ⑥ ± ⑦ ⑧ → ⑨ ▲ ⑩ ─ ÷ μ γ β Ⅱ Ⅰ ‰ □ 〇 ○ Ⅴ Ⅳ ★ ﹐ ° ※ ︰ α ― ≠ █ о θ ω ⒈ ⒉ ⒊ н ≤ ì ǎ ≥ р т с к й а и Ⅵ é è ﹢ ﹝ ﹞  ā ⒋ ù π ◇ Ω Ф ы Я п К в у м ǒ ü á ǔ ⒌ ⒍ 䦆 Ⅹ Ⅶ ← """
459
488
  ZhStopWords="""——— 》), )÷(1- ”, )、 =( : → ℃ & * 一一 ~~~~ ’ . 『 .一 ./ -- 』 =″ 【 [*] }> [⑤]] [①D] c] ng昉 * // [ ] [②e] [②g] ={ } ,也 ‘ A [①⑥] [②B] [①a] [④a] [①③] [③h] ③] 1. -- [②b] ’‘ ××× [①⑧] 0:2 =[ [⑤b] [②c] [④b] [②③] [③a] [④c] [①⑤] [①⑦] [①g] ∈[ [①⑨] [①④] [①c] [②f] [②⑧] [②①] [①C] [③c] [③g] [②⑤] [②②] 一. [①h] .数 [] [①B] 数/ [①i] [③e] [①①] [④d] [④e] [③b] [⑤a] [①A] [②⑧] [②⑦] [①d] [②j] 〕〔 ][ :// ′∈ [②④ [⑤e] 12% b] ... ................... …………………………………………………③ ZXFITL [③F] 」 [①o] ]∧′=[ ∪φ∈ ′| {- ②c } [③①] R.L. [①E] Ψ -[*]- ↑ .日 [②d] [② [②⑦] [②②] [③e] [①i] [①B] [①h] [①d] [①g] [①②] [②a] f] [⑩] a] [①e] [②h] [②⑥] [③d] [②⑩] e] 〉 】 元/吨 [②⑩] 2.3% 5:0 [①] :: [②] [③] [④] [⑤] [⑥] [⑦] [⑧] [⑨] …… —— ? 、 。 “ ” 《 》 ! , : ; ? . , . ' ? · ——— ── ? — < > ( ) 〔 〕 [ ] ( ) - + ~ × / / ① ② ③ ④ ⑤ ⑥ ⑦ ⑧ ⑨ ⑩ Ⅲ В " ; # @ γ μ φ φ. × Δ ■ ▲ sub exp sup sub Lex # % & ' + +ξ ++ - -β < <± <Δ <λ <φ <<== =☆ =- > >λ _ ~± ~+ [⑤f] [⑤d] [②i] ≈ [②G] [①f] LI ㈧ [- ...... 〉 [③⑩] 第二 一番 一直 一个 一些 许多 种 有的是 也就是说 末##末 啊 阿 哎 哎呀 哎哟 唉 俺 俺们 按 按照 吧 吧哒 把 罢了 被 本 本着 比 比方 比如 鄙人 彼 彼此 边 别 别的 别说 并 并且 不比 不成 不单 不但 不独 不管 不光 不过 不仅 不拘 不论 不怕 不然 不如 不特 不惟 不问 不只 朝 朝着 趁 趁着 乘 冲 除 除此之外 除非 除了 此 此间 此外 从 从而 打 待 但 但是 当 当着 到 得 的 的话 等 等等 地 第 叮咚 对 对于 多 多少 而 而况 而且 而是 而外 而言 而已 尔后 反过来 反过来说 反之 非但 非徒 否则 嘎 嘎登 该 赶 个 各 各个 各位 各种 各自 给 根据 跟 故 故此 固然 关于 管 归 果然 果真 过 哈 哈哈 呵 和 何 何处 何况 何时 嘿 哼 哼唷 呼哧 乎 哗 还是 还有 换句话说 换言之 或 或是 或者 极了 及 及其 及至 即 即便 即或 即令 即若 即使 几 几时 己 既 既然 既是 继而 加之 假如 假若 假使 鉴于 将 较 较之 叫 接着 结果 借 紧接着 进而 尽 尽管 经 经过 就 就是 就是说 据 具体地说 具体说来 开始 开外 靠 咳 可 可见 可是 可以 况且 啦 来 来着 离 例如 哩 连 连同 两者 了 临 另 另外 另一方面 论 嘛 吗 慢说 漫说 冒 么 每 每当 们 莫若 某 某个 某些 拿 哪 哪边 哪儿 哪个 哪里 哪年 哪怕 哪天 哪些 哪样 那 那边 那儿 那个 那会儿 那里 那么 那么些 那么样 那时 那些 那样 乃 乃至 呢 能 你 你们 您 宁 宁可 宁肯 宁愿 哦 呕 啪达 旁人 呸 凭 凭借 其 其次 其二 其他 其它 其一 其余 其中 起 起见 起见 岂但 恰恰相反 前后 前者 且 然而 然后 然则 让 人家 任 任何 任凭 如 如此 如果 如何 如其 如若 如上所述 若 若非 若是 啥 上下 尚且 设若 设使 甚而 甚么 甚至 省得 时候 什么 什么样 使得 是 是的 首先 谁 谁知 顺 顺着 似的 虽 虽然 虽说 虽则 随 随着 所 所以 他 他们 他人 它 它们 她 她们 倘 倘或 倘然 倘若 倘使 腾 替 通过 同 同时 哇 万一 往 望 为 为何 为了 为什么 为着 喂 嗡嗡 我 我们 呜 呜呼 乌乎 无论 无宁 毋宁 嘻 吓 相对而言 像 向 向着 嘘 呀 焉 沿 沿着 要 要不 要不然 要不是 要么 要是 也 也罢 也好 一 一般 一旦 一方面 一来 一切 一样 一则 依 依照 矣 以 以便 以及 以免 以至 以至于 以致 抑或 因 因此 因而 因为 哟 用 由 由此可见 由于 有 有的 有关 有些 又 于 于是 于是乎 与 与此同时 与否 与其 越是 云云 哉 再说 再者 在 在下 咱 咱们 则 怎 怎么 怎么办 怎么样 怎样 咋 照 照着 者 这 这边 这儿 这个 这会儿 这就是说 这里 这么 这么点儿 这么些 这么样 这时 这些 这样 正如 吱 之 之类 之所以 之一 只是 只限 只要 只有 至 至于 诸位 着 着呢 自 自从 自个儿 自各儿 自己 自家 自身 综上所述 总的来看 总的来说 总的说来 总而言之 总之 纵 纵令 纵然 纵使 遵照 作为 兮 呃 呗 咚 咦 喏 啐 喔唷 嗬 嗯 嗳"""
460
- encodings = ['utf-8', 'utf-16', 'gbk', 'big5', 'shift_jis', 'euc-jp', 'windows-1252', 'iso-8859-1', 'koi8-r', 'macroman', 'utf-32', 'ascii']
489
+
461
490
  EnPunctuation="""!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~"""
462
491
  nltk_en_tags={'CC': '并列连词', 'CD': '基数词', 'DT': '限定符', 'EX': '存在词', 'FW': '外来词', 'IN': '介词或从属连词', 'JJ': '形容词', 'JJR': '比较级的形容词', 'JJS': '最高级的形容词', 'LS': '列表项标记', 'MD': '情态动词', 'NN': '名词单数', 'NNS': '名词复数', 'NNP': '专有名词', 'NNPS': '专有名词复数', 'PDT': '前置限定词', 'POS': '所有格结尾', 'PRP': '人称代词', 'PRP$': '所有格代词', 'RB': '副词', 'RBR': '副词比较级', 'RBS': '副词最高级', 'RP': '小品词', 'SYM': '符号', 'UH': '感叹词', 'VB': '动词原型', 'VBD': '动词过去式', 'VBG': '动名词或现在分词', 'VBN': '动词过去分词', 'VBP': '非第三人称单数的现在时', 'VBZ': '第三人称单数的现在时', 'WDT': '以wh开头的限定词', 'WP': '以wh开头的代词', 'WP$': '以wh开头的所有格代词', 'WRB': '以wh开头的副词', 'TO': 'to'}
463
492
  nltk_tag_mapping={'NN': 'Noun', 'NNS': 'Noun', 'NNP': 'Noun', 'NNPS': 'Noun', 'VB': 'Verb', 'VBD': 'Verb', 'VBG': 'Verb', 'VBN': 'Verb', 'VBP': 'Verb', 'VBZ': 'Verb', 'JJ': 'Adjective', 'JJR': 'Adjective', 'JJS': 'Adjective', 'RB': 'Adverb', 'RBR': 'Adverb', 'RBS': 'Adverb', 'IN': 'Preposition', 'PRP': 'Pronoun', 'PRP$': 'Pronoun', 'DT': 'Determiner', 'CC': 'Conjunction', 'CD': 'Numeral', 'UH': 'Interjection', 'FW': 'Foreign Word', 'TO': 'Particle', 'EX': 'Existential "there"', 'MD': 'Modal Auxiliary', 'WDT': 'Wh-determiner', 'WP': 'Wh-pronoun', 'WP$': 'Possessive wh-pronoun', 'WRB': 'Wh-adverb', 'SYM': 'Symbol', 'RP': 'Particle', 'POS': 'Possessive ending', 'PDT': 'Predeterminer', 'LS': 'List item marker', 'NIL': 'Missing tag'}
@@ -591,6 +620,24 @@ def remove_empty_folders(folder_path):
591
620
  print(delet_root)
592
621
  print("Folders removed: ",len(delet_root))
593
622
 
623
+ def concatenate_excel_files(directory_path, output_file):
624
+ # List to hold DataFrames
625
+ dataframes = []
626
+
627
+ # Loop through all files in the directory
628
+ for filename in os.listdir(directory_path):
629
+ if filename.endswith('.xlsx') or filename.endswith('.xls'):
630
+ file_path = os.path.join(directory_path, filename)
631
+ df = pd.read_excel(file_path)
632
+ dataframes.append(df)
633
+
634
+ # Concatenate all DataFrames into a single DataFrame
635
+ combined_df = pd.concat(dataframes, ignore_index=True)
636
+
637
+ # Write the combined DataFrame to a new Excel file
638
+ combined_df.to_excel(output_file, index=False)
639
+ print(f"Combined Excel file saved as {output_file}")
640
+
594
641
  def remove_empty_lines(folder_path):
595
642
  files=FilePath(folder_path)
596
643
  for file in files:
@@ -735,6 +782,46 @@ def cs1(text):
735
782
  sentences=sentences
736
783
  return sentences
737
784
 
785
+ def word_tokenize(text, pos_tagged=False):
786
+ '''
787
+ Parameters
788
+ ----------
789
+ text : TYPE, string like: "无独有偶,这个消息如晴天霹雳,霍尔姆斯听到后不知所措。中国电影家协会和中国作家协会,中国翻译协会是做慈善的。"
790
+ DESCRIPTION.
791
+ pos_tagged : TYPE, optional
792
+ DESCRIPTION. The default is False.
793
+
794
+ Returns
795
+ -------
796
+ words : TYPE, list like: ['无独有偶', ',', '这个', '消息', '如', '晴天霹雳', ',', '霍尔姆斯', '听到', '后', '不知所措', '。', '中国', '电影', '家', '协会', '和', '中国', '作家', '协会', ',', '中国', '翻译', '协会', '是', '做', '慈善', '的', '。', '']
797
+ DESCRIPTION.
798
+
799
+ '''
800
+ words=None
801
+ try:
802
+ try:
803
+ from nlpir import ictclas #调用中科院分词器ICTCLAS
804
+ except Exception as err:
805
+ print("installing nlpir/ICTCLAS...")
806
+ from PgsFile import install_package as ip
807
+ ip("nlpir-python")
808
+
809
+ from nlpir import ictclas
810
+ if pos_tagged is False:
811
+ words=ictclas.segment(text, pos_tagged=False)
812
+ else:
813
+ words=ictclas.segment(text, pos_tagged=True)
814
+ except Exception as err:
815
+ if "expired" in str(err):
816
+ try:
817
+ from nlpir import tools
818
+ tools.update_license()
819
+ except Exception as err2:
820
+ print("You need a VPN to try this service!", err2)
821
+ else:
822
+ print(err)
823
+ return words
824
+
738
825
  def pad_sequence(
739
826
  sequence,
740
827
  n,
PgsFile/__init__.py CHANGED
@@ -23,6 +23,7 @@ from .PgsFile import get_subfolder_path
23
23
  from .PgsFile import makedirec, makefile
24
24
  from .PgsFile import source_path, next_folder_names, get_directory_tree_with_meta, find_txt_files_with_keyword
25
25
  from .PgsFile import remove_empty_folders, remove_empty_txts, remove_empty_lines, remove_empty_last_line, move_file
26
+ from .PgsFile import concatenate_excel_files
26
27
 
27
28
  # 6. Data cleaning
28
29
  from .PgsFile import BigPunctuation, StopTags, Special, yhd
@@ -38,7 +39,7 @@ from .PgsFile import extract_chinese_punctuation, generate_password, sort_string
38
39
  from .PgsFile import strQ2B_raw, strQ2B_words
39
40
  from .PgsFile import ngrams, bigrams, trigrams, everygrams, compute_similarity
40
41
  from .PgsFile import word_list, batch_word_list
41
- from .PgsFile import cs, cs1, sent_tokenize
42
+ from .PgsFile import cs, cs1, sent_tokenize, word_tokenize
42
43
 
43
44
  # 8. Maths
44
45
  from .PgsFile import len_rows, check_empty_cells
@@ -1,7 +1,7 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: PgsFile
3
- Version: 0.2.0
4
- Summary: This module aims to simplify Python package management, script execution, file handling, web scraping, multimedia download, data cleaning, and word list generation for literary students, making it more accessible and convenient to use.
3
+ Version: 0.2.2
4
+ Summary: This module aims to simplify Python package management, script execution, file handling, web scraping, multimedia download, data cleaning, NLP tasks like Chinese word tokenization and POS tagging, and word list generation for literary students, making it more accessible and convenient to use.
5
5
  Home-page: https://mp.weixin.qq.com/s/12-KVLfaPszoZkCxuRd-nQ?token=1589547443&lang=zh_CN
6
6
  Author: Pan Guisheng
7
7
  Author-email: 895284504@qq.com
@@ -12,6 +12,7 @@ Classifier: Operating System :: OS Independent
12
12
  Requires-Python: >=3.8
13
13
  Description-Content-Type: text/markdown
14
14
  License-File: LICENSE
15
+ Requires-Dist: chardet
15
16
  Requires-Dist: pandas
16
17
  Requires-Dist: python-docx
17
18
  Requires-Dist: pip
@@ -20,6 +21,7 @@ Requires-Dist: fake-useragent
20
21
  Requires-Dist: lxml
21
22
  Requires-Dist: pimht
22
23
  Requires-Dist: pysbd
24
+ Requires-Dist: nlpir-python
23
25
 
24
26
  Purpose: This module aims to assist Python beginners, particularly instructors and students of foreign languages and literature, by providing a convenient way to manage Python packages, run Python scripts, and perform operations on various file types such as txt, xlsx, json, tsv, html, mhtml, and docx. It also includes functionality for data scraping, cleaning and generating word lists.
25
27
 
@@ -1,5 +1,5 @@
1
- PgsFile/PgsFile.py,sha256=1-PR4NO2FF7lO8_lQmSP_VLVs6pV8jC_5_nOdGvwuhk,80684
2
- PgsFile/__init__.py,sha256=Tbr3MaFP7ZqhwVaYAnBJx7UBJhM4c884F8sFMQjfzXU,2317
1
+ PgsFile/PgsFile.py,sha256=BQNZBrBYgyB_4TVxD0CJ6cMpiaaDzL9b7c7kcYtxmwQ,83682
2
+ PgsFile/__init__.py,sha256=-Vy1SIh-BYopiEan-EjBtwqZsNteNrOqkws7hUj1d2w,2378
3
3
  PgsFile/Corpora/Idioms/English_Idioms_8774.txt,sha256=qlsP0yI_XGECBRiPZuLkGZpdasc77sWSKexANu7v8_M,175905
4
4
  PgsFile/Corpora/Monolingual/Chinese/People's Daily 20130605/Raw/00000000.txt,sha256=SLGGSMSb7Ff1RoBstsTW3yX2wNZpqEUchFNpcI-mrR4,1513
5
5
  PgsFile/Corpora/Monolingual/Chinese/People's Daily 20130605/Raw/00000001.txt,sha256=imOa6UoCOIZoPXT4_HNHgCUJtd4FTIdk2FZNHNBgJyg,3372
@@ -2618,8 +2618,8 @@ PgsFile/models/slovene.pickle,sha256=faxlAhKzeHs5mWwBvSCEEVST5vbsOQurYfdnUlsIuOo
2618
2618
  PgsFile/models/spanish.pickle,sha256=Jx3GAnxKrgVvcqm_q1ZFz2fhmL9PlyiVhE5A9ZiczcM,597831
2619
2619
  PgsFile/models/swedish.pickle,sha256=QNUOva1sqodxXy4wCxIX7JLELeIFpUPMSlaQO9LJrPo,1034496
2620
2620
  PgsFile/models/turkish.pickle,sha256=065H12UB0CdpiAnRLnUpLJw5KRBIhUM0KAL5Xbl2XMw,1225013
2621
- PgsFile-0.2.0.dist-info/LICENSE,sha256=cE5c-QToSkG1KTUsU8drQXz1vG0EbJWuU4ybHTRb5SE,1138
2622
- PgsFile-0.2.0.dist-info/METADATA,sha256=u-nzDLhOIJYZ-nOp9FpE5EFWsjW3683viOehOfqQIvs,4959
2623
- PgsFile-0.2.0.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
2624
- PgsFile-0.2.0.dist-info/top_level.txt,sha256=028hCfwhF3UpfD6X0rwtWpXI1RKSTeZ1ALwagWaSmX8,8
2625
- PgsFile-0.2.0.dist-info/RECORD,,
2621
+ PgsFile-0.2.2.dist-info/LICENSE,sha256=cE5c-QToSkG1KTUsU8drQXz1vG0EbJWuU4ybHTRb5SE,1138
2622
+ PgsFile-0.2.2.dist-info/METADATA,sha256=1fm2uh-uYKgDe26DvUGCmj2LbMcjwDum113nbmW-MIA,5070
2623
+ PgsFile-0.2.2.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
2624
+ PgsFile-0.2.2.dist-info/top_level.txt,sha256=028hCfwhF3UpfD6X0rwtWpXI1RKSTeZ1ALwagWaSmX8,8
2625
+ PgsFile-0.2.2.dist-info/RECORD,,