问题描述
提取word文档中的缩略语,并将文档内的缩略语替换为全称
解决方案
首先观察数据,缩小问题范围
发现文档内缩略语基本都以表格方式呈现,一般是两列,有时也有四列,所以只考虑解决缩略语为表格且为2、4列的情况,其他特殊情况(段落等非表格形式,列数不为2、4)不考虑
缩略语判断逻辑主要就是单元格内文本是大写字符串的比例
代码里我写了两种逻辑,V1是判断大写单元格占总的单元格比例,V2是,奇数列大写与偶数列非大写的比例
判断缩略语提取缩略语
def extract_abbreviation(file, cfd=0.1, judgement='V1'):
start_time = time.time()
all_abrvt = []
for table in file.tables:
try:
width, length = len(table.columns), len(table.rows)
if not (width == 2 or width ==4):
continue
if judgement == 'V1': # 判断英文大写占单元格文本比例
cell_text = [cell.text for row in table.rows for cell in row.cells if cell.text]
cell_upper = [i for i in cell_text if i.isupper()]
if 0.4 < (len(cell_upper)+1) / (len(cell