我没学过JAVA,现在跟这个老师在做信息检索的课题,今天给了个题目,让我做,我都愁死了,才学了2个星期的JAVA,都不知道怎么入手.题目如下:Write a program to extract the following from a piece of text.submit a design document outlining the issues you have considered and resolved.Your code will be executed on texts that i will proveide.Dates: 2005;Oct 10,2005; 10/10/2005; 10/10/05
Acronyms: USA,CNN,BBC
Digit.digit: 8.00
Digit,digit: 8,000
Digits: 8000
Currency symbol: $, ...
Email address: [email protected]
URLs: http://www.cnn.com
IP address: 123.67.65.870
Names: Rekha;India;Hyderabad;New York; Los Angles
Acronyms: USA,CNN,BBC
Digit.digit: 8.00
Digit,digit: 8,000
Digits: 8000
Currency symbol: $, ...
Email address: [email protected]
URLs: http://www.cnn.com
IP address: 123.67.65.870
Names: Rekha;India;Hyderabad;New York; Los Angles
所谓信息检索就是利用网络将不懂的题目完成。先把所有的数组都定义好
读行,用BufferedReader.readLine
每行解析为一个数组填充到某个定义好的数组中去
每行要去的数组的名称用aLine.subString(0, aLine.indexOf(":"))得到
冒号后面的内容(也用subString得到)用split分解为字符串数组
字符串数组中的元素逐一转换为值填到定义好的数组中
FileReader类,读取文本文件用的。
BufferedReader类,成行读取文件中文本用的。BufferedReader br = new BufferedReader(new FileReader(new File("文件全名")));
String aLine = br.readLine(); //读取文件中下一行,无下一行则返回null
File累+FileReader+BufferedReader(Scanner)可以完成在文本中读取内容,但是具体要读取什么内容~看题目要求了~~代码例子的话楼上已经提供了!
Acronyms: USA,CNN,BBC
Digit.digit: 8.00
Digit,digit: 8,000
Digits: 8000
Currency symbol: $, ...
Email address: [email protected]
URLs: http://www.cnn.com
IP address: 123.67.65.870
Names: Rekha;India;Hyderabad;New York; Los Angles这些分类的数据就是最终提取出来的结果,它们存在于个文本文档中,通过数据筛选和提取,最后得到我们要想的数据(信息).希望有高手能写随便一个提取日期或者IP地址的 代码, 也让我这小菜鸟少走点弯路呢
假设我有个文本的内容如下:
javaaa java1,000
fjiaojfdoa3,1000
dfjiaojfdoa3,1000
dfajkldsf;kj5,000测试代码如下:import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileNotFoundException;class regex {
public static void main ( String args[]) throws IOException {
try {
FileReader fr = new FileReader("D:/data.txt");
BufferedReader br = new BufferedReader(fr);
String str;
String Dight = "[0-9],[0-9]{3}";
String[] str2 = new String[100];
int i = 0;
Pattern pat = Pattern.compile(Dight);
System.out.print("Dight,digth:\t");
while ( (str = br.readLine()) != null) {
Matcher mat = pat.matcher(str);
while ( mat.find()) {
str2[i] = mat.group();
i++;
}
}
for (int j = 0; j < str2.length; j++) {
if ( str2[j] != null) {
System.out.print(str2[j] + " ");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
输入结果为:Dight,digth: 1,000 3,100 3,100 5,100
基本实现提取的功能,其他的格式我们只需要变换正则表达就可以了,还请高手拍砖!
所谓信息检索就是利用网络将不懂的题目完成。先把所有的数组都定义好