LUA实现单词替换功能
背景描述
编程或者文档处理过程, 经常遇到需要将一个单词修改为另外一个单词的情况, 例如 命名为 shall 修改 为 should。
使用工具实现, 则比较方便,不容易出错, 解放双手。
需求规格
对于某个文件夹中的所有文本文件(txt), 将某个单词替换为目标单词。
实现思路
对于替换的单词映射, 在配置文件config.lua进行设置, 存储一个表,表中每一行 对应 src vocanbulary 和 dest vocanbulary
对应工具的主题逻辑代码在 replace.lua中实现,
待替换的文本文件存储在 replaceFiles文件夹下。
总体目录结果如下:
│ config.lua
│ replace.lua
│
└─replaceFiles
test.txt
代码说明
代码实现路径:
https://github.com/fanqingsong/code-snippet/tree/master/lua/replace
config.lua
-- ttanslating table, in every line first word is source, second word is destination
trans_table_string = [[
you lucy
]]
待替换文件 test.txt
I love you
replace.lua工具逻辑代码实现
--[[
/*******************************************************************************
* Author:
* Date:
* Description: set config for replace, replace by config in files of target path
* Changes:
*******************************************************************************/
]]local require = require
local io = io
local ipairs = ipairs
local assert = assert
local print = print
local string = string
local lfs = require"lfs"local transFilePath = "./replaceFiles"
string.split = function(str, pat, max, regex)
pat = pat or "\n"
max = max or #strlocal t = {}
local c = 1if #str == 0 then
return {""}
endif #pat == 0 then
return nil
endif max == 0 then
return str
endrepeat
local s, e = str:find(pat, c, not regex)
max = max - 1
if s and max < 0 then
t[#t+1] = str:sub(c)
else
t[#t+1] = str:sub(c, s and s - 1)
end
c = e and e + 1 or #str + 1
until not s or max < 0return t
end------------------------------------------ parse start ------------------------------------------
local trans_vocabulary_table = {
--["sourcevocabulary"] = "destvocabulary"
}local function construct_vocabulary_table()
require("config")
print("parse trans_table starting .....")
-- 禄帽募镁?拢卢 禄禄?路没 \n 潞?\r\n
local lineSep = "\r\n"
if string.find(trans_table_string, "\r\n") then
lineSep = "\r\n"
elseif string.find(trans_table_string, "\n") then
lineSep = "\n"
elseif string.find(trans_table_string, "\r") then
lineSep = "\r"
endlocal lines = trans_table_string:split(lineSep)
for _,line in ipairs(lines) do
print("line="..line)local src, dest = string.match(line, "([%w_]+)%s+([%w_]+)")
if src then
print("well formed line="..line)
trans_vocabulary_table[src] = dest
end
end
print("parse trans_table ending .....")
end-- parse table
construct_vocabulary_table()------------------------------------------ parse end ------------------------------------------
------------------------------------------ read file list start ------------------------------------------
local targetFiles = {}local function infilter(file, filters)
if filters == nil or filters == "*" then
return true
endfor _, v in pairs(filters) do
if string.find(file, "%."..v.."$") then
return true
end
end
return false
endlocal function splitonlast (path, sep)
local dir, file = string.match(path,"^(.-)([^:/\\]*)$")
return dir, file
endfunction readdir(dir, filelist, filters)
for file in lfs.dir(dir) do
if file ~= ".." and file ~= "." then
local f = dir.."/"..file
if lfs.attributes(f).mode == "directory" then
readdir(f, filelist, filters)
else
if infilter(file, filters) then
table.insert(filelist, f)
end
end
end
end
endreaddir(transFilePath, targetFiles, {"*"})
for _,file in ipairs(targetFiles) do
--print("c file =".. file)
end------------------------------------------ read file list end ------------------------------------------
------------------------------------------ handle file start ------------------------------------------
local function handle_file(file)
local lineBuff = {}-- ?赂?搂?
local fh = assert(io.open (file, "rb"))
local contents = fh:read("*a")
fh:close()
--print(contents)
for src,dest in pairs(trans_vocabulary_table) do
print(src.."==>"..dest)
contents = string.gsub(contents, src, dest)
end--[[
-- 禄帽募镁?拢卢 禄禄?路没 \n 潞?\r\n
local lineSep = "\r\n"
if string.find(contents, "\r\n") then
lineSep = "\r\n"
elseif string.find(contents, "\n") then
lineSep = "\n"
elseif string.find(contents, "\r") then
lineSep = "\r"
endlocal fileLines = string.split(contents, lineSep)
for _,line in ipairs(fileLines) do
--print(" handle_file line= "..line)
local gotPattern = false
for src,dest in pairs(trans_vocabulary_table) do
--print("src="..src.."----")
local s, e = string.find(line, "%s-%(%s-"..src.."%s-,%s-%\"")
if s then
print("!!!! ------- gotPattern ------- src ="..src)gotPattern = true
-- the part before OssUsersrc
local head = string.sub(line, 1, s-1)
-- tail part = now");
--print(head)
local tail = string.sub(line, e+1)
--print("tail="..tail)
--print("tail[1]="..string.sub(tail, 1,1))-- OssUserLogType(LOG_LEVEL_NOTICE, LOG_TYPE_SYSTEM, "the system will reboot now");
local level = dest["level"]
local types = dest["types"]
local msg = dest["msg"]local sep = " "
if msg == "" then
sep = ""
elseif string.sub(tail, 1,1) == "\"" then
sep = ""
end
--print("msg="..msg.."sep="..sep.."--")
local transLine = head .. "OssUserLogType(" .. level ..", " .. types .. ", \"" .. msg .. sep .. tailtable.insert(lineBuff, transLine)
if gotPattern then
break
endend
endif not gotPattern then
table.insert(lineBuff, line)
end
end
]]
--write buff to orig file
local fh = assert(io.open(file, "wb"))
fh:write(contents)
fh:close()
endfor _,file in ipairs(targetFiles) do
print("handling file =".. file)
handle_file(file)
end------------------------------------------ handle file end ------------------------------------------
运行结果
root@fqs:/home/share/luascript/replace# cat ./replaceFiles/test.txt
I love you
root@fqs:/home/share/luascript/replace# lua replace.lua
parse trans_table starting .....
line= you lucy
well formed line= you lucy
line=
parse trans_table ending .....
handling file =./replaceFiles/test.txt
you==>lucy
root@fqs:/home/share/luascript/replace#
root@fqs:/home/share/luascript/replace#
root@fqs:/home/share/luascript/replace# cat ./replaceFiles/test.txtI love lucy
root@fqs:/home/share/luascript/replace#
LUA实现单词替换功能的更多相关文章
- Python3.5 day3作业一:实现简单的shell sed替换功能
需求: 1.使python具有shell中sed替换功能. #!/usr/bin/env python #_*_conding:utf-8_*_ #sys模块用于传递参数,os模块用于与系统交互. i ...
- [word]用Word2007查找和替换功能批量设置图片位置
Word2007的"查找和替换"功能并不仅仅可以对文字进行批量的查找替换,还有很多神奇的功能,比如对插入的图片位置进行批量的查找和调整等等. 今天我们就来试试Word2007的&q ...
- 【九度OJ】题目1111:单词替换
题目1111:单词替换 题目描述: 输入一个字符串,以回车结束(字符串长度<=100).该字符串由若干个单词组成,单词之间用一个空格隔开,所有单词区分大小写.现需要将其中的某个单词替换成另一个单 ...
- Visual Studio 2010 实用功能:使用web.config发布文件替换功能
当建立ASP.NET Web应用程序项目后,默认除了生成web.config外,还生成了web.debug.config与Web.Release.config.顾名思义,根据它们的命名我可以推测到他们 ...
- AC日记——单词替换 1.7 21
21:单词替换 总时间限制: 1000ms 内存限制: 65536kB 描述 输入一个字符串,以回车结束(字符串长度<=100).该字符串由若干个单词组成,单词之间用一个空格隔开,所有单词区 ...
- OpenJudge计算概论-单词替换
/*====================================================================== 单词替换 总时间限制: 1000ms 内存限制: 65 ...
- UltraEdit (Ctrl + F) 查找、(Ctrl + R)替换功能失效
环境: Windows 7 Service Pack 1 X64 工具: UltraEdit Version 21 症状: UltraEdit (Ctrl + F) 查找.(Ctrl + R)替换功能 ...
- Java基础知识强化76:正则表达式之替换功能
1. 替换功能: String类的replaceAll方法,如下: public String replaceAll(String regex, String replacement): 使用给定的r ...
- Java基础知识强化40:StringBuffer类之StringBuffer的替换功能
1. StringBuffer的替换功能: public StringBuffer replace(int start, int end, String str): 2. 案例演示: p ...
随机推荐
- Mac下android_sdk配置环境变量
1.启动Terminal终端工具 2.输入cd ~/ 进入当前用户的home目录 3. 创建: touch .bash_profile 4.打开并编辑: open .bash_profile 5.在文 ...
- ios retain 与 copy 的区别
.retain 与copy区别 retain 的仅仅是引用计数加1,但是并没有创建新的对象.它们的指针是指向相同的内存地址. copy 是创建一个新的对象作为原来对象的副本,新创建出来的引用计数并没有 ...
- 洛谷 P1198 [JSOI2008]最大数 Label:线段树
题目描述 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值. 限制:L不超过当前数列的长度. 2. 插入操作 ...
- [Leetcode] Interleaving String
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- HDU-统计难题
DescriptionIgnatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Inp ...
- BZOJ4554: [Tjoi2016&Heoi2016]游戏
Description 在2016年,佳缘姐姐喜欢上了一款游戏,叫做泡泡堂.简单的说,这个游戏就是在一张地图上放上若干个炸弹,看 是否能炸到对手,或者躲开对手的炸弹.在玩游戏的过程中,小H想到了这样一 ...
- Crystal Reports 2008(水晶报表) 安装
这篇blog主要是介绍Crystal Reports2008(水晶报表)的安装. 首先我们应该知道Crystal Reports 有什么作用? 从这里Crystal Reports 你可以了解到它的一 ...
- 深入浅出 - Android系统移植与平台开发(二) - 准备Android开发环境
作者:唐老师,华清远见嵌入式学院讲师. 编译Android源码 关于android系统的编译,Android的官方网站上也给出了详细的说明.http://source.android.com/sour ...
- Go语言练习:网络编程实例——简易图片上传网站
1.代码结构 2.运行实例 1.代码结构 $ tree . ├── photoweb.go ├── public │ ├── css │ ├── images │ └── js ├── u ...
- 李洪强iOS经典面试题上
李洪强iOS经典面试题上 1. 风格纠错题 修改完的代码: 修改方法有很多种,现给出一种做示例: // .h文件 // http://weibo.com/luohanchenyilong/ / ...