python - 从同时包含印地语和英语的文件中仅提取印地语文本

标签 python file unicode

我有一个包含如下行的文件

 ted    1-1 1.0 politicians do not have permission to do what needs to be 
 done.  

 राजनीतिज्ञों के पास जो कार्य करना चाहिए, वह करने कि अनुमति नहीं है.

我必须编写一个程序,逐行读取文件并在仅包含印地语部分的文件中给出输出。这里的第一个词表示最后两段的来源。另外,最后两句是互译的。 基本上,我正在尝试从该文件创建一个平行语料库。

最佳答案

您可以通过检查 Unicode 字符来做到这一点。

import codecs,string
def detect_language(character):
    maxchar = max(character)
    if u'\u0900' <= maxchar <= u'\u097f':
        return 'hindi'

with codecs.open('letter.txt', encoding='utf-8') as f:
    input = f.read()
    for i in input:
        isEng = detect_language(i)
        if isEng == "hindi":
            #Hindi Character
            #add this to another file
            print(i,end="\t")
            print(isEng)

希望对你有帮助

关于python - 从同时包含印地语和英语的文件中仅提取印地语文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44474085/

相关文章:

python - 如何从python中通过heapq实现的优先级队列中删除log(n)中的元素?

python - 使用 Python 字典编写 Ansible playbook

linux - Cygwin 告诉我文件不存在

java - Itext PDF writer,有什么方法可以在pdf中允许unicode下标符号吗? (没有 setTextRise)

Python socket.close() 在脚本退出之前不起作用

python - 为什么我的简单 pygame 会滞后?

安卓编程 : Cannot read file from file picker Intent

c - 尝试读取宽字符会给出 EOF

C 统一码 : How do I apply C11 standard amendment DR488 fix to C11 standard function c16rtomb()?

python - 获取不以 'u' 为前缀的 python 字符串文字的警告