# -*- coding: utf-8 -*-
"""
Created on Wed Jun 28 18:42:33 2017

"""
import re
import numpy as np

'''
该程序实现对giza++后的对齐双语平行语料抽取对齐词汇关系
建立源语言到目标语言的映射矩阵,编号从0开始,将对齐文件中的NULL当作第一个词语
如果词语之间存在对齐关系,则将对齐矩阵matrixST[s][t]位置值设置为1,其它为0

'''
def alig_pairs(filepath):
    matrixZeroOne = []
    pattern1 = re.compile(r' \(\{([0-9 ]*)\}\) ?')
    # print(pattern1)
    f = open(filepath,'r')#,encoding='utf-8')
    line=f.readline()
    #matrix = np.zeros()
    while(True):
        if not line:
            break   
        target = f.readline().strip().split()
        source = f.readline().strip()
        #match= pattern1.findall(source) # 使用Pattern匹配文本,获得匹配结果,无法匹配时将返回None
        source_word = pattern1.split(source)
        # print(source_word)
        s_l = len(source_word)//2-1#-1不考虑null
        t_l = len(target)
        #print(s_l)
        #print(t_l)
        matrixTS = np.zeros((t_l,s_l))
        #print(matrixST.shape)
        #从null开始对齐i=0。如果不考虑null,从第二位开始,i=2
        i=2
        while( i < len(source_word)-2):
            index = source_word[i+1]
            if index != '' and index !=' ':
                s = index.strip().split()
                # print(s)
                for s_ind in s:
                    #设置对齐矩阵
                    matrixTS[int(s_ind)-1][int((i-2))//2]=1
                    #print(i//2-1)
                    #该语句抽取对齐词语队
                    #print(source_word[int(i)],target[int(s_ind)-1])
            i+=2
        # print(matrixTS)
        matrixZeroOne.append(matrixTS)
        # print(matrixTS.shape)
        #因为对齐这个矩阵是动态生成的,所以在这里进行矩阵的合并

#print(i)            
        #print(source_word)
        #if match:
            #print (match)
            #print ('yes')
        line=f.readline()
        #print(target)
        #print(source)
        
   
    f.close()
    return matrixZeroOne
#alig_pairs('test.txt')
#alig_pairs('117-06-28.183340.lmt.A3.final')
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

# -*- coding: utf-8 -*-
import codecs
def get_matrix():
        #print('程序进入process')
    Chinese = codecs.open("result/result_cn",'r',encoding = 'utf-8')
    English = codecs.open('result/result_en', 'r', encoding = 'utf-8')
    # result_eng = codecs.open('result/swap_en', 'w', encoding = 'utf-8')
    # result_chi = codecs.open('result/swap_cn', 'w', encoding = 'utf-8')
    # eng_chi = codecs.open('result/en_to_cn','w',encoding = 'utf-8')

english_sentence_count = 0
    chinese_sentence_count = 0
        
    chinese_word = []
    chinese_sentence = []

for line in Chinese.readlines():
        pair = line.strip().split()
        if len(pair) == 4:
            swap = pair[1]
            pair[1] = pair[2]
            pair[2] = swap
            s = pair[0] + " " + pair[1] + " " + pair[2] + " " + pair[3]
            chinese_word.append(s)
            # result_chi.write(pair[0] + "    " + pair[1] + "    " + pair[2] + "    " + pair[3] + "\n")
        if len(pair) == 0:
            chinese_sentence.append(chinese_word)
            chinese_word = []
            # result_chi.write("\n")
            chinese_sentence_count += 1

english_word = []
    english_sentence = []
    for line in English.readlines():
        pair = line.strip().split()
        if len(pair) == 4:
            swap = pair[1]
            pair[1] = pair[2]
            pair[2] = swap    
            s = pair[0] + " " + pair[1] + " " + pair[2] + " " + pair[3]
            english_word.append(s)
            # result_eng.write(pair[0] + "    " + pair[1] + "    " + pair[2] + "    " + pair[3] + "\n")
        if len(pair) == 0:
            english_sentence.append(english_word)
            english_word = []
            # result_eng.write("\n")
            english_sentence_count += 1

if english_sentence_count < chinese_sentence_count:
        min_count = english_sentence_count
    else:
        min_count = chinese_sentence_count

matrix = []
    if len(english_sentence) == len(chinese_sentence):
        i = 0
        while i < len(english_sentence):
            chinese_sentence_length = len(chinese_sentence[i])
            english_sentence_length = len(english_sentence[i])#获得当前句子的行列值

english_chinese = [["0" for col in range(english_sentence_length + 1)] for row in range(chinese_sentence_length + 1)]
            
            col = 1
            while col <= english_sentence_length:
                english_chinese[0][col] = english_sentence[i][col - 1]
                col += 1
            
            row = 1
            while row <= chinese_sentence_length:
                english_chinese[row][0] = chinese_sentence[i][row - 1]
                row += 1
            # for row in range(chinese_sentence_length):
            #     for col in range(english_sentence_length):
            #         eng_chi.write(english_chinese[row][col] + "    ")
            #     eng_chi.write("\n")
            # eng_chi.write("\n")
            #每次放进去的矩阵,其实规模是不一样大的
            matrix.append(english_chinese)
            i = i + 1
    else:
        print('error')

# for j in range(len(matrix)):
    #     for row in range(len(matrix[j])):
    #         s = ""
    #         for col in range(len(matrix[j][row])):
    #             s += matrix[j][row][col]
    #             s += "    "
    #         print(s)
        
    return matrix, chinese_sentence
#matrix,_ = get_matrix()            
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

#-*-coding:utf-8-*-
import os  
import string  
   
def count(filepath):  
    total = 0 #总行数  
    countPound = 0 #注释行数  
    countBlank = 0 #空行数  
    line = open(filepath,'r')#,encoding='utf-8')
    for li in line.readlines(): #readlines()一次性读完整个文件  
        total += 1  
        if not li.split(): #判断是否为空行  
            countBlank +=1  
        li.strip()  
        if li.startswith('#'):  
            countPound += 1  
    print(file)  
    print("countBlank:%d" % countBlank)  
    print("countPound:%d" % countPound)  
    print("total:%d" % total)  
 
 
count('result_cn')

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

#-*-coding:utf-8-*-
def bijiao():  
     f1=open('lmt.txt','r')  
     f2=open('lh.txt','r')  
     count=0     #统计行数  
     dif=[]      #统计不同的数量序列  
     for a in f1:  
          b=f2.readline()  
          count+=1  
          if a!=b:  
              dif.append(count)  
     f1.close()  
     f2.close()  
     return dif    
c=bijiao()  
if c==0:  
     print('两个文件一样!')  
else:  
     print('有%d处不同'% len(c))  
     for each in d:  
          print('%d行不一样'% each)

转载于:https://www.cnblogs.com/maowuyu-xb/p/7236769.html

常用Python文件相关推荐

  1. python对文件的读操作方法有哪些-Python文件常用操作方法

    Python文件常用操作方法 一.对File对象常用操作方法: file= open(file, mode='r', buffering=-1, encoding=None, errors=None, ...

  2. python文件操作的方法_Python文件常用操作方法

    Python文件常用操作方法 一.对File对象常用操作方法: file= open(file, mode='r', buffering=-1, encoding=None, errors=None, ...

  3. cfile清空文件内容_体育老师学编程(第11天)python常用的文件读写操作

    学习内容:python文件处理 一.什么是文件: 前边学习了计算机的存储设备分为内存和硬盘两种,内存容量小,断电就丢失,我们若想长期存储一段内容,就需要存到硬盘中,那么存入的方式就是以文件形式存入的. ...

  4. python复制文件shutil_Python常用模块——文件复制模块shutil

    Python常用模块--文件复制模块shutil shutil模块 高级的文件.文件夹.压缩包处理模块 shutil.copyfileobj(fsrc, fdst) 将文件内容拷贝到另一个文件中 im ...

  5. Python文件操作-文本文件、二进制文件、csv文件的读取写入、OS、shutil、CSV模块、常用字符编码

    Python文件操作 文本文件和二进制文件 文件操作相关模块 open()创建文件对象 文件对象的常用属性和方法 pickle 序列化 文本文件读取和写入 文本文件写入步骤 write()/write ...

  6. Python包下载常用whl文件汇总:最全的Python whl集合

    Python包下载常用whl文件汇总:最全的Python whl集合 对于Python开发者来说,我们经常需要下载各种第三方库或包,而这些包往往需要我们手动下载并安装.在Python中,我们通常使用p ...

  7. Python文件的读写及常用文件的打开方式

    编码格式 常见的编码格式 Python的解释器使用的是Unicode(内存) .py文件在磁盘上使用UTF-8(外存) 更改编码格式 一般形式为在程序开头写 # coding:编码格式.# codin ...

  8. Python基础 文本控制 文件编码格式 使用python读写文件 常用的文件打开方式

    编码格式: 常见的字符编码格式 Python的解释器使用的是Unicode(内存) .py文件在磁盘上使用UTF-8存储(外存) 文件的读写原理: 1.文件的读写俗称"IO操作"  ...

  9. linux 怎么用命令行运行python文件_Linux命令行常用命令及python应用

    通常用户和电脑交互是通过图形用户界面(GUI), 更快捷的方式是通过命令行界面(Command line interface), 通过在终端输入命令来实现文件夹和程序间的切换.Linux 和OS X都 ...

最新文章

  1. Linux内核之于红黑树and AVL树
  2. raid磁盘阵列OFFLINE后的应急方案
  3. 机器学习面试知识点汇总(Machine Learning Core Concepts Collection)
  4. oracle ora 08103,ORA-08103: 对象不再存在
  5. 匹配特殊字符的正则表达式
  6. php数组转化js数组格式化,php数组转换成js数组
  7. android sudio提示快捷键冲突解决
  8. table-layout:fixed; 表格比例固定
  9. showModalDialog窗体滚动条只显示竖向
  10. JavaScript在线解压 ZIP 文件 JavaScript 怎样在线解压 ZIP,jszip实现解压压缩包,并下载压缩包内文件
  11. SoC,SiP,IP和Chiplet的区别
  12. 【前端兼容性】常见的浏览器兼容问题及解决方案
  13. 技术解密之百度搜索中台低代码的探索与实践
  14. Python学习之文件13
  15. 在线付费问诊互联网医院智慧医疗系统包含哪些功能
  16. 文献分析-对3个重要数据库的认识
  17. Greenplum 6.9 资源组中文文档
  18. 腾讯大数据Hermes爱马仕的系统
  19. QT + FFmpeg 5.x + x264 + x265 + SDL2 音视频播放器
  20. 9.3.2 从 ZIP 文件中解压缩

热门文章

  1. app应用需要怎么测试
  2. Linux万兆网络配置
  3. 业务异常 java_java – 具有业务异常的Hystrix断路器
  4. linux fsck命令,Linux中fsck命令起什么作用呢?
  5. pmp是什么意思?pmp值得学吗?
  6. 素数类型C语言题目总结
  7. python 输出文件中返回码为200的接口的平均响应时间_Django查看响应时间问题
  8. js 生成二维码_js 生成二维码
  9. php 自动处理小图的代码,php对图像的各种处理函数代码小结
  10. 新rust怎么拆除围墙_“问题围挡”拆除 街道变漂亮了