PowerPoint Presentation

Similar documents
C++11概要 ライブラリ編

新版 明解C++入門編

Microsoft PowerPoint - string_kruse [兼容模式]

C/C++语言 - C/C++数据

untitled

ebook39-5

第十一章 流类库与输入/输出

Microsoft Word - 11.doc

FY.DOC

CC213

新竹高中105學年度第1學期第2次教師甄選簡章

酒 神 (长篇小说)

Microsoft Word - 103上簡章-招生計畫-0709.doc

臺北市立教育大學師資培育暨就業輔導中心

C++ 程序设计 OJ9 - 参考答案 MASTER 2019 年 6 月 7 日 1

epub 33-8

法務部廉政署新聞稿

Windows XP

c_cpp

Microsoft Word - template.doc

untitled

Chn 116 Neh.d.01.nis

CC213

(Load Project) (Save Project) (OffLine Mode) (Help) Intel Hex Motor

Microsoft Word doc

C 1 # include <stdio.h> 2 int main ( void ) { 4 int cases, i; 5 long long a, b; 6 scanf ("%d", & cases ); 7 for (i = 0;i < cases ;i ++) 8 { 9

Microsoft Word - CPE考生使用手冊 docx

1 Project New Project 1 2 Windows 1 3 N C test Windows uv2 KEIL uvision2 1 2 New Project Ateml AT89C AT89C51 3 KEIL Demo C C File

Microsoft Word - SupplyIT manual 3_cn_david.doc

Improved Preimage Attacks on AES-like Hash Functions: Applications to Whirlpool and Grøstl

ICD ICD ICD ICD ICD

Paratune用户手册

第3章.doc

2 2 3 DLight CPU I/O DLight Oracle Solaris (DTrace) C/C++ Solaris DLight DTrace DLight DLight DLight C C++ Fortran CPU I/O DLight AM

蔡 氏 族 譜 序 2

INTRODUCTION TO COM.DOC

2013 C 1 #include <stdio.h> 2 int main(void) 3 { 4 int cases, i; 5 long long a, b; 6 scanf("%d", &cases); 7 for (i = 0; i < cases; i++) 8 { 9 scanf("%

科学计算的语言-FORTRAN95

6-1 Table Column Data Type Row Record 1. DBMS 2. DBMS MySQL Microsoft Access SQL Server Oracle 3. ODBC SQL 1. Structured Query Language 2. IBM

Microsoft Word - 檢討報告_ _ IMC建議已修訂_tiffany網上版.doc

<ADB6ADB1C25EA8FAA6DB2D4D56432E706466>

Outline USB Application Requirements Variable Definition Communications Code for VB Code for Keil C Practice

untitled

C语言的应用.PDF

2013 C 1 # include <stdio.h> 2 int main ( void ) 3 { 4 int cases, a, b, i; 5 scanf ("%d", & cases ); 6 for (i = 0;i < cases ;i ++) 7 { 8 scanf ("%d %d

Bus Hound 5

四川省普通高等学校

Oracle Solaris Studio makefile C C++ Fortran IDE Solaris Linux C/C++/Fortran IDE "Project Properties" IDE makefile 1.

3.1 num = 3 ch = 'C' 2

《计算概论》课程 第十九讲 C 程序设计语言应用

Guava学习之Resources

(Microsoft Word \275\322\245~\254\241\260\312[1]...doc)

Microsoft PowerPoint - Lecture7II.ppt

C++ 程序设计 实验 2 - 参考答案 MASTER 2017 年 5 月 21 日 1

引 言 基 督 徒 的 第 一 次 退 休 会 ( 马 太 福 音 4:23-5:1) 许 多 甚 至 不 去 假 装 成 为 基 督 徒 的 人 也 都 会 开 口 赞 同 耶 稣 在 其 登 山 宝 训 里 的 教 导 十 几 个 世 纪 以 来, 有 多 少 学 者 政 治 家 和 诗 人 在

Microsoft PowerPoint - 4. 数组和字符串Arrays and Strings.ppt [兼容模式]

C/C++ 语言 - 循环

Epson

1 Framework.NET Framework Microsoft Windows.NET Framework.NET Framework NOTE.NET NET Framework.NET Framework 2.0 ( 3 ).NET Framework 2.0.NET F

目录

全国计算机技术与软件专业技术资格(水平)考试

1 C++ 2 Bjarne Stroustrup C++ (system programming) 6 (infrastructure) C++ 7 Herb Sutter 8 C++ (efficiency) (flexibility) 9 (abstraction) (productivity

Important Notice SUNPLUS TECHNOLOGY CO. reserves the right to change this documentation without prior notice. Information provided by SUNPLUS TECHNOLO

C 1

X 广 州 澳 希 亚 实 业 有 限 公 司 广 州 市 荔 湾 区 国 家 税 务 局 第 二 税 务 分 局 广 州 市 运 输 有 限 公 司 广 州 市 荔 湾 区 国 家 税 务 局 第 二 税 务 分

RUN_PC連載_12_.doc

TX-NR3030_BAS_Cs_ indd

概述

(Geographic data or geodata ) 30 (Buelher, K and L. Mckee1996) (Open GIS Consortium OGC) OGC GIS Open GIS OGC (Geography Markup Langu

国信证券股份有限公司

第一章

93年度推薦甄選考古題

「電子檔案統一命名原則之研究」計畫

目 錄 一 研 討 會 活 動 主 題... 1 二 參 訪 名 單 及 分 房 資 訊... 2 三 參 訪 行 程 表... 3 四 會 議 地 點 及 歡 送 晚 宴 地 點... 6 五 研 討 會 議 程 表... 7 六 接 待 人 員 名 單... 8 七 參 訪 景 點 簡 介...

AN INTRODUCTION TO PHYSICAL COMPUTING USING ARDUINO, GRASSHOPPER, AND FIREFLY (CHINESE EDITION ) INTERACTIVE PROTOTYPING

Microsoft Word - 44雙月刊

audiogram3 Owners Manual

2.1 n ~á CPU q ˆ ~ µƒã î ˆ C++ à nù eô 1. (edit) 2. Í (preprocess) 3. (compile) 4. (link) 5. (load) 6. ˆ (execute) µl ô Ãs nù (editor) Í (preprocessor

nooog

Sophos Central 快速安裝手冊

Microsoft Word - 三峽鎮衛生所_3_-張家宸.李永繁.doc

Transcription:

USTC Chapter 23 Text Processing 王子磊 (Zilei Wang) Email: zlwang@ustc.edu.cn http://vim.ustc.edu.cn

Overview 应用领域 字符串 I/O Maps 规则表达式

现在, 你已经知道了基础知识 确实如此! 恭喜你! 不要继续关注编程语言特性了! 开始使用吧! 程序和应用是什么样的? 通过编程能够做好什么? 文本处理 数值处理 嵌入式系统编程 银行 医学应用 科学可视化 卡通制作 路径规划 物理设计

文本处理 我们表达的都是文本 往往都是的 书籍 论文 事务日志 (email, phone, bank, sales, ) Web 网页 ( 甚至是布局指令 ) 图表 ( 数字 ) 邮件 程序 测量 历史数据 医疗记录 Amendment I Congress shall make no law respecting an establishment of religion, or prohibiting the free exercise thereof; or abridging the freedom of speech, or of the press; or the right of the people peaceably to assemble, and to petition the government for a redress of grievances.

字符串概览 http://staff.ustc.edu.cn/~zlwang/ 字符串 std::string <string> s.size() s1==s2 C 风格字符串 (0 结尾的字符数组 ) <cstring> 或 <string.h> strlen(s) strcmp(s1,s2)==0 std::basic_string<ch>, e.g. unicode 字符串 typedef std::basic_string<char> string; 其它私有字符串类

字符串转换 简单的 to_string 例如 : template<class T> string to_string(const T& t) { ostringstream os; os << t; return os.str(); } string s1 = to_string(12.333); string s2 = to_string(1+5*6-99/7);

字符串转换 简单地从字符串提取 template<class T> T from_string(const string& s) { istringstream is(s); T t; if (!(is >> t)) throw bad_from_string(); return t; } 例如 : 比 to_string 处理更复杂 double d = from_string<double>("12.333"); Matrix<int,2> m = from_string< Matrix<int,2> >("{ {1,2}, {3,4} }");

一般化的字符串转换 template<typename Target, typename Source> Target lexical_cast(source arg) { std::stringstream ss; Target result; 参考 boost::lexical_cast if (!(ss << arg) // read arg into stream!(ss >> result) // read result from stream!(ss >> std::ws).eof()) // stuff left in stream? throw bad_lexical_cast(); } return result; string s = lexical cast<string>(lexical_cast<double>(" 12.7 ")); // ok // works for any type that can be streamed into and/or out of a string: XX xx = lexical_cast<xx>(lexical_cast<yy>(xx(whatever))); //!!!

I/O 概览 http://staff.ustc.edu.cn/~zlwang/ in >> x out << x in.get(c) getline(in,s) Stream I/O 根据 x 的格式从 in 读取到 x 中根据 x 的格式将 x 写入 out 中从 in 中读取一个字符到 c 中从 in 中读取一行到字符串 s 中 istream ostream istringstream ifstream iostream ofstream ostringstrea m stringstream fstream

Map 概览 关联容器 <map>, <set>, <unordered_map>, <unordered_set> map multimap set multiset unordered_map unordered_multimap unordered_set unordered_multiset 文本操作的支柱 找出一个单词 看看是否已经有一个单词 获取给定单词的相关信息 具体示例参见 Chapter 23

Map 概览 http://staff.ustc.edu.cn/~zlwang/ multimap<string,messag e*> John Doe 使用 vector 作为一般存储, 采用其它数据结构用于特殊处理 John Doe John Q. Public Mail_file: vector<message>

问题 : 读取一个邮政编码 美国州缩写和 ZIP 码 两个字母紧接着是 5 个数字 string s; while (cin>>s) { if (s.size()==7 && isletter(s[0]) && isletter(s[1]) && isdigit(s[2]) && isdigit(s[3]) && isdigit(s[4]) && isdigit(s[5]) && isdigit(s[6])) cout << "found " << s << '\n'; } 脆弱的 凌乱的 绑定的 ( 不易维护 )

问题 : 读取一个邮政编码 上述简单解决方案的问题 它太冗长了 (4 行代码 8 个函数调用 ) 我们忽略了 ( 有意的?) 所有没有用空白符将自身与上下文分开的邮政编码 TX77845 TX77845-1234 和 ATM77845 我们忽略了 ( 有意的?) 所有用空白符分割州名缩写和数字的邮政编码 TX 77845 我们接受 ( 有意的?) 所有使用小写字母的邮政编码 tx77845 如果我们确定查找一个不同格式的邮政编码, 我们将不得不完全重新代码 CB3 0DS, DK-8000 Arhus

TX77845-1234 1 st 尝试 : 2 nd ( 记得 -1234): 如何表达 特殊字符? 3 rd: 4 th ( 显示计数 ): 5 th ( 转义 ): 然而 -1234 是可选的? 6 th: 我们想在 TX 后有一个可选的空格 7 th ( 看不到的空格 ): 8 th ( 空格可视化 ): 9 th (0 到多个空格 ): wwddddd wwddddd-dddd \w\w\d\d\d\d\d-\d\d\d\d \w2\d5-\d4 \w{2}\d{5}-\d{4} \w{2}\d{5}(-\d{4})? \w{2}?\d{5}(-\d{4})? \w{2}\s?\d{5}(-\d{4})? \w{2}\s*\d{5}(-\d{4})?

Regex 库 可用 它不属于 C++98 标准内 Technical Report 1 2004 的一部分 将纳入 C++0x 标准中 使用 VS 9.0 C++, 使用 <regex>, std::tr1::regex GCC 4.3.0, 使用 <tr1/regex>, std::tr1::regex www.boost.org, 使用 <boost/regex>, std::boost::regex

#include <boost/regex.hpp> #include <iostream> #include <string> #include <fstream> using namespace std; int main() { ifstream in("file.txt"); if (!in) cerr << "no file\n"; // input file regex pat ("\\w{2}\\s*\\d{5}(-\\d{4})?"); cout << "pattern: " << pat << '\n'; // ZIP code pattern } //

int lineno = 0; string line; // input buffer while (getline(in,line)) { ++lineno; smatch matches; // matched strings go here if (regex_search(line, matches, pat)) { cout << lineno << ": " << matches[0] << '\n'; // whole match if (1<matches.size() && matches[1].matched) cout << "\t: " << matches[1] << '\n ; // sub-match } }

结果 Input: address TX77845 1 ffff tx 77843 asasasaa 2 ggg TX3456-23456 3 howdy 4 zzz TX23456-3456sss ggg TX33456-1234 5 cvzcv TX77845-1234 sdsas 6 xxxtx77845xxx 7 TX12345-123456 8 Output: pattern: "\w{2}\s*\d{5}(-\d{4})?" 1: TX77845 2: tx 77843 5: TX23456-3456 : -3456 6: TX77845-1234 7: Tx77845 : -1234 8: TX12345-1234 : -1234

正则表达式语法 正则表达式的完整理论基础是基于状态机的高效实现 你能够干预语法, 但不要修改语义 语法是简明 神秘和烦人的, 但很有用 学习它吧 例如 : Xa{2,3} // Xaa Xaaa Xb{2} // Xbb Xc{2,} // Xcc Xccc Xcccc Xccccc \w{2}-\d{4,5} // \w is letter \d is digit (\d*:)?(\d+) // 124:1232321 :123 123 Subject: (FW: Re:)?(.*) //. (dot) matches any character [a-za-z] [a-za-z_0-9]* // identifier [^aeiouy] // not an English vowel

搜索 vs. 匹配 搜索 (Searching ) 是在任意数据流中搜索与正则表达式匹配的字符串 regex_search() 在流中找出该模式作为子字符串 匹配 (Matching) 是在一个给定大小的字符串中匹配字符串 regex_match() 找出模式的完整匹配及其字符串

从 web 上获取的表格 http://staff.ustc.edu.cn/~zlwang/ KLASSE ANTAL DRENGE ANTAL PIGER ELEVER IALT 0A 12 11 23 1A 7 8 15 1B 4 11 15 2A 10 13 23 3A 10 12 22 4A 7 7 14 4B 10 5 15 5A 19 8 27 6A 10 9 19 6B 9 10 19 7A 7 19 26 7G 3 5 8 7I 7 3 10 8A 10 16 26 9A 12 15 27 0MO 3 2 5 0P1 1 1 2 0P2 0 5 5 10B 4 4 8 10CE 0 1 1 1MO 8 5 13 2CE 8 5 13 3DCE 3 3 6 4MO 4 1 5 6CE 3 4 7 8CE 4 4 8 9CE 4 9 13 REST 5 6 11 Alle klasser 184 202 386 数值域 文本域 不可见的分隔符域 语义独立性 i.e. 数字实际上表示某种含义 first row + second row == third row 最后一行是该列的总和

描述行 第一行 正则表达式 : ^[\w ]+( [\w ]+)*$ 字符串常量 : "^[\\w ]+( [\\w ]+)*$" 其他行 正则表达式 : ^([\w ]+)( \d+)( \d+)( \d+)$ 字符串常量 "^([\\w ]+)( \\d+)( \\d+)( \\d+)$" 那些不可见的 tab 符号不讨厌吗? 定义一个 tab 符号类 那些不可见的 space 符号不讨厌吗? 使用 \s

简单的布局检查 int main() { ifstream in("table.txt"); // input file if (!in) error("no input file\n"); string line; int lineno = 0; // input buffer } regex header( "^[\\w ]+( [\\w ]+)*$"); // header line regex row( "^([\\w ]+)( \\d+)( \\d+)( \\d+)$"); // data line // check layout

简单的布局检查 int main() { // open files, define patterns } if (getline(in,line)) { // check header line smatch matches; if (!regex_match(line, matches, header)) error("no header"); } while (getline(in,line)) { // check data line ++lineno; smatch matches; if (!regex_match(line, matches, row)) error("bad line", to_string(lineno)); }

验证该表 int boys = 0; int girls = 0; // column totals while (getline(in,line)) { smatch matches; // extract and check data if (!regex_match(line, matches, row)) error("bad line"); int curr_boy = from_string<int>(matches[2]); int curr_girl = from_string<int>(matches[3]); int curr_total = from_string<int>(matches[4]); // check row if (curr_boy+curr_girl!= curr_total) error("bad row sum"); if (matches[1]== Alle klasser ) { } // last line; check columns: if (curr_boy!= boys) error("boys don t add up"); if (curr_girl!= girls) error("girls don t add up"); return 0; } boys += curr_boy; girls += curr_girl;

应用领域 文本处理仅仅是众多领域中的一个 或者说其中的若干领域 ( 看你怎么算了 ) Browsers, Word, Acrobat, Visual Studio, 图形处理 声音处理 数据基础 医学 科学 商业 数值计算 金融