统计指定文件中的蒙古文单词次数并从高到低排序
时间: 2024-03-14 09:42:43 浏览: 16
好的,您可以使用以下Python代码来实现:
```python
import re
# 指定文件路径
file_path = "path/to/file.txt"
# 读取文件内容
with open(file_path, "r", encoding="utf-8") as f:
content = f.read()
# 使用正则表达式匹配蒙古文单词
words = re.findall("[\u1820-\u1877]+", content)
# 统计单词出现次数
word_count = {}
for word in words:
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
# 按照单词出现次数从高到低排序
sorted_word_count = sorted(word_count.items(), key=lambda x: x[1], reverse=True)
# 输出结果
for word, count in sorted_word_count:
print(word, count)
```
这段代码使用了正则表达式匹配蒙古文单词,并统计了每个单词出现的次数,最后按照单词出现次数从高到低排序并输出结果。您只需要将`file_path`变量替换为您实际的文件路径即可。
相关问题
统计文本中的蒙古文单词出现的次数并从高到低排序,并将排序结果写到文件中python
好的,可以使用Python来实现这个任务。首先,需要读取文本文件中的蒙古文单词,可以使用正则表达式来匹配蒙古文单词,然后使用字典来统计每个单词出现的次数。最后,按照单词出现次数从高到低排序,将排序结果写入文件中。下面是一个简单的Python代码示例:
```python
import re
from collections import defaultdict
# 读取文本文件
with open('text.txt', 'r', encoding='utf-8') as f:
text = f.read()
# 使用正则表达式匹配蒙古文单词
pattern = re.compile('[\u1800-\u18AF]+')
words = pattern.findall(text)
# 统计单词出现次数
word_count = defaultdict(int)
for word in words:
word_count[word] += 1
# 按照单词出现次数从高到低排序
sorted_words = sorted(word_count.items(), key=lambda x: x[1], reverse=True)
# 将排序结果写入文件
with open('output.txt', 'w', encoding='utf-8') as f:
for word, count in sorted_words:
f.write(f'{word}: {count}\n')
```
其中,`text.txt`是待处理的文本文件,`output.txt`是输出文件,需要根据实际情况修改文件名和路径。运行代码之后,输出文件中将会按照单词出现次数从高到低的顺序列出每个单词及其出现次数。
统计指定文本中的蒙古文单词出现次数从高到底排序,并将结果放到文本文件中python
好的,以下是实现该功能的 Python 代码:
```python
import re
from collections import Counter
# 读取文本文件
with open('input.txt', 'r', encoding='utf-8') as f:
text = f.read()
# 提取蒙古文单词
words = re.findall(r'[\u1800-\u18AF]+', text)
# 统计单词出现次数,并按照出现次数从高到低排序
word_counts = Counter(words)
sorted_word_counts = sorted(word_counts.items(), key=lambda x: x[1], reverse=True)
# 将结果保存到文本文件
with open('output.txt', 'w', encoding='utf-8') as f:
for word, count in sorted_word_counts:
f.write(f'{word}: {count}\n')
```
解释一下代码:
首先,我们使用 `with open()` 语句读取文本文件,其中 `r` 表示只读模式,`encoding='utf-8'` 表示使用 UTF-8 编码。
然后,我们使用正则表达式 `[\u1800-\u18AF]+` 提取蒙古文单词。该正则表达式表示匹配 Unicode 编码范围为 1800 到 18AF 的字符,即蒙古文字符,`+` 表示匹配一个或多个字符。我们使用 `re.findall()` 函数提取文本中的蒙古文单词。
接下来,我们使用 `collections.Counter()` 函数统计单词出现次数,并使用 `sorted()` 函数按照出现次数从高到低排序。
最后,我们使用 `with open()` 语句将结果保存到文本文件中,其中 `w` 表示写入模式,`encoding='utf-8'` 表示使用 UTF-8 编码。
请将需要分析的文本文件保存为 `input.txt`,并将以上代码保存为 `count_mongolian_words.py`,然后运行该 Python 脚本,即可在当前目录下生成一个名为 `output.txt` 的文本文件,其中包含了蒙古文单词出现次数从高到低排序的结果。
相关推荐
![.pdf](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)