Web Data Management(一)

  今天上了web数据管理,老师布置了一个简单的exercise,题目如下:

  Question : Consider the following documents:
    d1 = I like to watch the sun set with my friend.
    d2 = The Best Places To Watch The Sunset.
    d3 = My friend watches the sun come up.

  Task : write a program which can output the document ID given by an input word.

  解题思路(1):

    1. 使用Java的String类的constains()方法,这里不贴实现代码.

  解题思路(2):

    1. 将字符串分割为字符串数组

    2.判断字符串数组里是否包含给定key word.

public class Exercise1 {

	public static void main(String[] args) {

		// 1. 转换为数组
		StringBuilder d1 = new StringBuilder("I like to watch the sun set with my friend.");
		StringBuilder d2 = new StringBuilder("The Best Places To Watch The Sunset.");
		StringBuilder d3 = new StringBuilder("My friend watches the sun come up.");

		// 删除最后面的点,分割字符串
		String[] sd1 = d1.deleteCharAt(d1.length() - 1).toString().split(" ");
		String[] sd2 = d2.deleteCharAt(d2.length() - 1).toString().split(" ");
		String[] sd3 = d3.deleteCharAt(d3.length() - 1).toString().split(" ");

		// 2. 输入关键字
		Scanner scanner = new Scanner(System.in);
		System.out.print("Input key word :");
		String word = scanner.next();

		boolean isSd1 = search(sd1, word);
		boolean isSd2 = search(sd2, word);
		boolean isSd3 = search(sd3, word);
		// 3. 遍历寻找
		if (isSd1 || isSd2 || isSd3) {
			System.out.print("The key word " + word + " constains in ");
			if (isSd1) {
				System.out.print("d1 ");
			}
			if (isSd2) {
				System.out.print("d2 ");
			}
			if (isSd3) {
				System.out.print("d3 ");
			}
		} else {
			System.out.println("not found");
		}

	}

	private static boolean search(String[] str, String word) {
		boolean flag = false;
		for (String string : str) {
			// 不考虑大小写的相等
			if (string.equalsIgnoreCase(word)) {
				return true;
			}
		}
		return flag;
	}

}

   解题思路(3):

    1. 使用Java的正则表达式(这里是作者自己从网上找的reg代码)

import java.util.Scanner;
import java.util.regex.Pattern;

public class Exercise1 {

	public static void main(String[] args) {
		
		String d1 = "I like to watch the sun set with my friend.";
		String d2 = "The Best Places To Watch The Sunset.";
		String d3 = "My friend watches the sun come up.";
		
		// 1. 获取输入,可以是多个单词,空格分割
		Scanner scanner = new Scanner(System.in);
		System.out.print("Input key word :");
		StringBuffer word = new StringBuffer(scanner.nextLine());
		scanner.close();
		
		/**
		 * 构建正则表达式, 忽略大小写
		 * .*(keyword1|keyword2|...).*
		 */
		String words = word.toString().replace(' ', '|');
		StringBuilder reg = new StringBuilder(".*(").append(words).append(").*");
        Pattern pattern = Pattern.compile(reg.toString(), Pattern.CASE_INSENSITIVE);  
        
        boolean isSd1 = pattern.matcher(d1).matches();
		boolean isSd2 = pattern.matcher(d2).matches();
		boolean isSd3 = pattern.matcher(d3).matches();

		if (isSd1 || isSd2 || isSd3) {
			System.out.print("The key word " + word + " constains in ");
			if (isSd1) {
				System.out.print("d1 ");
			}
			if (isSd2) {
				System.out.print("d2 ");
			}
			if (isSd3) {
				System.out.print("d3 ");
			}
		} else {
			System.out.println("not found");
		}
	}
}

 

    

转载于:https://www.cnblogs.com/LeeAnkang/p/4498299.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值