1. 程式人生 > >外交部發言人發言語料抓取及簡單分析

外交部發言人發言語料抓取及簡單分析

花了點時間,把外交部網站上的發言人表態一欄中的資料全部抓取下來,按照一定的格式保存於檔案中,時間範圍是2010-09-14~2014-06-18,如果跑在伺服器上的話,可以做增量更新,即若有更新每天下載一篇新的文章。我國的外交部發言人制度是在1983年3月1日開始設立的,但是外交部網站上公佈的資料好像只有我拿到的這些。

檔案格式如圖:


(宣告:本人只是想把這些答記者問的對話當作自然語言處理的語料,進行學習。)

從這些文字中統計出如下資訊;

發言人出場的次數

姓名次數
洪磊458
華春瑩175
劉為民117
秦剛98
姜瑜90
馬朝旭62

發言長度最長和最短

2011年4月19日外交部發言人洪磊舉行例行記者會1580
2014年2月27日外交部發言人華春瑩主持例行記者會1277


發言中出現的詞數,由多到少,前50位(去除了一些停用詞):

中方10074
中國5248
問4992
答4350
對此2772
合作2727
發展2261
和平2203
評論2127
各方1944
希望1891
穩定1883
對話1720
國家1643
敘利亞1623
日本1557
中1536
維護1523
地區1436
推動1415

外交部1284
據報道1281
兩國1198
國際1171
方1100
應1089
日方1030
國際社會1014
願1014
解決996
關係986
局勢978
支援971
介紹952
立場902
釣魚島901
戰略898
情況894
美國890
總理889
已889
努力883
訪問881
稱876
請872
總統866
領導人865
相關838
主權807
外長803

可以看出,日本、敘利亞、美國出現的次數在地名上拍名最前,說明這些國家與我國的關係密切(糾葛?)。提到主席的次數沒有總統多,總理的次數與總統相當。可以看出,我國的外交目標主要是合作發展、維護世界和平!不過,這是外交部的發言,可能記者多來自國外,關心中國政府對世界的聲音,所以大多提到了整個世界。

問題數(每出現一個?統計一次):

951個問題

就這麼簡單統計了一下,當然我並沒有明確的目的,所以也不能夠做過多的深入分析。我覺得,從語言學上、邏輯學上都可以進行分析,因為這些句子肯定都是經過仔細推敲的,可能話中有話,但是會避免出現歧義。“學習說話者”,可以從中找到一些技巧,比如不管多麼複雜刁難的問題,都可以用幾個字就回答了提問,還可能讓人啞口無言,重要的是抓住重點。

語料下載地址:http://download.csdn.net/detail/ozhaohuafei/7526553 。

使用Jsoup進行抓取及頁面解析的程式碼。

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Spider {
	public static void main(String[] args) throws IOException {
//			ArrayList<String> all = getAll();
			String file = "c://users//logic//desktop//fayan.list";
//			write2File(file, all);
//			System.out.println("---------------------------------");
		 readFromFile(file);
	}

	public static ArrayList<String> getAll() throws IOException {
		ArrayList<String> list = new ArrayList<String>();
		for (int i = 0; i < 27; i++) {
			String baseurl = "http://www.fmprc.gov.cn/mfa_chn/wjdt_611265/fyrbt_611275/default";
			if (i == 0)
				baseurl = baseurl + ".shtml";
			else
				baseurl = baseurl + "_" + i + ".shtml";
			Document doc = Jsoup
					.connect(baseurl)
					.timeout(50000)
					.userAgent(
							"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 "
									+ "(KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36")
					.header("X-Requested-With", "XMLHttpRequest").get();
			Elements element = doc.getElementsByTag("li");
			for (Element ele : element) {
				if (!ele.html().contains("target=\"_blank\""))
					continue;
				String content_url = ele.select("a[href]").attr("abs:href");
				String text = ele.text();
				String title = text.substring(0, text.indexOf('('));
				//首先查詢,再增量更新
//				if (isContains("c://users//logic//desktop//fayan.list", title))
//					continue;
				String date = text.substring(text.indexOf('(') + 1,
						text.indexOf(')'));
				String content = getContent(content_url);
				StringBuffer buf = new StringBuffer();
				buf.append(title.trim() + "||" + date.trim() + "||" + content
						+ "\n");
				list.add(buf.toString());
				System.out.println(title + "\t" + date);
			}
			System.out.println("---------------------------------");
		}
		return list;
	}

	public static String getContent(String contentUrl) throws IOException {
		Document doc = Jsoup
				.connect(contentUrl)
				.userAgent(
						"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 "
								+ "(KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36")
				.header("X-Requested-With", "XMLHttpRequest").timeout(50000)
				.get();
		Element element = doc.getElementById("doccontent");
		return element.text().trim().replaceAll("\\s+", "");
	}

	public static void write2File(String file, ArrayList<String> lines,boolean isAppend)
			throws IOException {
		FileUtils.writeLines(new File(file), lines, isAppend);
	}

	public static boolean isContains(String file, String find)
			throws IOException {
		List<String> lines = FileUtils.readLines(new File(file));
		for (String str : lines) {
			if (str.contains(find))
				return true;
		}
		return false;
	}
	
	public static void readFromFile(String file)
			throws IOException {
		List<String> lines = FileUtils.readLines(new File(file));
		int i=0;
		ArrayList<String> list = new ArrayList<String>();
		for (String str : lines) {
			 StringTokenizer tokenizer = new StringTokenizer(str,"||");
			 String s ="";
			 while(tokenizer.hasMoreTokens()){
//				 String s = tokenizer.nextToken();
				 s = tokenizer.nextToken();
				 String s1 = tokenizer.nextToken();
				 String s2 = tokenizer.nextToken();
				
			 }
			 System.out.println(s);
			 list.add(s);
			 write2File("c://users//logic//desktop//fayan_name.list", list,false);
//			 String temp[] = str.split("||");
//			 System.out.println(temp[0] + "\t" + temp[1] + "\t" + temp[2]);
//			 if(i++ == 5)
//				 break;
		}
	}
}