L1-8 估值一億的AI核心代碼 (20分) java

AI.jpg

以上圖片來自新浪微博。

本題要求你實現一個稍微更值錢一點的 AI 英文問答程序,規則是:

  • 無論用戶說什麼,首先把對方說的話在一行中原樣打印出來;
  • 消除原文中多餘空格:把相鄰單詞間的多個空格換成 1 個空格,把行首尾的空格全部刪掉,把標點符號前面的空格刪掉;
  • 把原文中所有大寫英文字母變成小寫,除了 I
  • 把原文中所有獨立的 can youcould you 對應地換成 I canI could—— 這裏“獨立”是指被空格或標點符號分隔開的單詞;
  • 把原文中所有獨立的 Ime 換成 you
  • 把原文中所有的問號 ? 換成驚歎號 !
  • 在一行中輸出替換後的句子作爲 AI 的回答。

輸入格式:

輸入首先在第一行給出不超過 10 的正整數 N,隨後 N 行,每行給出一句不超過 1000 個字符的、以回車結尾的用戶的對話,對話爲非空字符串,僅包括字母、數字、空格、可見的半角標點符號。

輸出格式:

按題面要求輸出,每個 AI 的回答前要加上 AI: 和一個空格。

輸入樣例:

6
Hello ?
 Good to chat   with you
can   you speak Chinese?
Really?
Could you show me 5
What Is this prime? I,don 't know

輸出樣例:

Hello ?
AI: hello!
 Good to chat   with you
AI: good to chat with you
can   you speak Chinese?
AI: I can speak chinese!
Really?
AI: really!
Could you show me 5
AI: I could show you 5
What Is this prime? I,don 't know
AI: what Is this prime! you,don't know

正則表達式,和python的有一點區別,參考了這篇博客

import java.io.IOException;
import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

//** Class for buffered reading int and double values *//*
class Reader {
	static BufferedReader reader;
	static StringTokenizer tokenizer;

	// ** call this method to initialize reader for InputStream *//*
	static void init(InputStream input) {
		reader = new BufferedReader(new InputStreamReader(input));
		tokenizer = new StringTokenizer("");
	}

	// ** get next word *//*
	static String next() throws IOException {
		while (!tokenizer.hasMoreTokens()) {
			// TODO add check for eof if necessary
			tokenizer = new StringTokenizer(reader.readLine());
		}
		return tokenizer.nextToken();
	}

	static String nextLine() throws IOException {
		return reader.readLine();
	}
	static char nextChar() throws IOException {
		return next().charAt(0);
	}
	static int nextInt() throws IOException {
		return Integer.parseInt(next());
	}

	static long nextLong() throws IOException {
		return Long.parseLong(next());
	}

	static double nextDouble() throws IOException {
		return Double.parseDouble(next());
	}
}

public class Main {

	public static void main(String[] args) throws IOException {
		Reader.init(System.in);
		int n = Reader.nextInt();
		String[]s = new String[10];
		for (int i = 0; i < n; i++) {
			s[i] = Reader.nextLine();
		}
		for (int i = 0; i < n; i++) {
			System.out.println(s[i]);
			ai(s[i]);
		}
	}

	private static void ai(String s) {
		System.out.print("AI: ");
		//$1表示匹配的第一個括號中的內容,這裏要把兩個字母中間的多個空格改成一個,所以空格前是一個字母([a-zA-Z]{1}),空格後也是一個字母([a-zA-Z]{1}),中間多個空格變成一個
		String ans = s.replaceAll("([a-zA-Z]{1})\\s+([a-zA-Z]{1})", "$1 $2").trim().replaceAll("\\s+(\\W)", "$1");
		char[]temp = ans.toCharArray();
		for (int i = 0; i < temp.length; i++) {
			if (temp[i]>='A'&&temp[i]<='Z'&&temp[i]!='I') {
				temp[i] = (char) (temp[i]+32);
			}
		}
		ans = new String(temp);
		//因爲存在"can you"轉換爲"I can"之後又將"I"轉換爲"you"的情況,
        //然後題目要求是輸出I can",這樣纔可以AC
        //所以“can you、could you、I、me”應當是在分割線之上的字符串前提下進行操作。
        //暫時將它們使用ABC代替
		ans = ans.replaceAll("\\bcan you\\b", "A").replaceAll("\\bcould you\\b", "B").replaceAll("\\b(me|I)\\b", "C").replaceAll("[?]", "!").replaceAll("A","I can").replaceAll("B","I could").replaceAll("C","you");
		System.out.println(ans);
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章