从一句话中提取地点信息

采用字符串匹配的方法提取句子中包含的地点信息(省市)

直接看代码:

private static final String[][] LOCATION = { { "上海", "浦东" }, { "北京 ", "朝阳" }, { "浙江", "杭州", "宁波" }, { "天津" },
		{ "四川", "成都" }, { "广东", "深圳", "广州" }, { "江苏", "南京", "苏州", "无锡" }, { "安徽", "合肥", "芜湖" }, { "湖北", "武汉" },
		{ "湖南", "长沙" }, { "福建", "厦门" } };

	public static String parseLocation(String str) {
		String result = "other";
		for (String[] l : LOCATION) {
			if ("other".equals(result)) {
				for (int i = 0; i < l.length; i++) {
					if (str.matches(".*" + l[i] + ".*")) {
						result = l[i];

					}
				}
			}
		}
		return result;
	}


单元测试:

@Test
	public void psrsertest() {
		System.out.println("Test begin");
		 List <String> strs=new ArrayList<String>(10);
		 strs.add("杭州市西苑");
		 strs.add("浙江省杭州市");
		 strs.add("广东省广州市");
		 strs.add("浙江杭州");
		 strs.add("上海市");
		 strs.add("北京上海");
		 strs.add("上海浦东区");
		 strs.add("上海南京路");
		 strs.add("安徽阜阳");
		 strs.add("南京");
		 strs.add("苏州市工业园区");
		 
		 
		 for(Iterator itor=strs.iterator();itor.hasNext();){
			 String st=(String)itor.next();
			 System.out.println(Localtion.parseLocation(st));
		 }
		
	}


转载于:https://my.oschina.net/PagodaTree/blog/598932

Python中实现一句话信息提取有多种方法,以下为你介绍几种常见的方式: ### 基于字符串处理的方法 如果信息提取规则较为简单,可以直接使用Python的字符串处理方法,如`find()`、`split()`、`strip()`等。 ```python sentence = "今天的温度是25摄氏度" # 提取温度数值 start_index = sentence.find("是") + 1 end_index = sentence.find("摄氏度") temperature = sentence[start_index:end_index] print(temperature) ``` ### 基于正则表达式的方法 正则表达式可以用于匹配复杂的文本模式,对于从一句话提取特定格式的信息非常有用。 ```python import re sentence = "我的邮箱是example@example.com,请联系我" # 提取邮箱地址 pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b' email = re.findall(pattern, sentence) print(email) ``` ### 基于BeautifulSoup库的方法 如果信息来源于HTML或XML格式的文本,可以使用`BeautifulSoup`库进行解析和提取。 ```python from bs4 import BeautifulSoup html_sentence = "<p>这是一个<strong>重要</strong>的信息</p>" soup = BeautifulSoup(html_sentence, 'html.parser') # 提取重要信息 important_info = soup.strong.string print(important_info) ``` ### 基于`find_all()`方法(适用于HTML解析) 在解析HTML时,`find_all()`方法可以根据标签名、属性等条件查找元素。 ```python from bs4 import BeautifulSoup html_sentence = "<a href='https://example.com'>点击这里</a>" soup = BeautifulSoup(html_sentence, 'html.parser') # 提取链接 links = soup.find_all('a') for link in links: print(link.get('href')) ``` ### 基于自然语言处理库(如NLTK、SpaCy) 对于自然语言文本的信息提取,可以使用NLTK或SpaCy等库进行词性标注、命名实体识别等操作。 ```python import spacy nlp = spacy.load("en_core_web_sm") sentence = "Apple is looking at buying U.K. startup for $1 billion" doc = nlp(sentence) # 提取命名实体 for ent in doc.ents: print(ent.text, ent.label_) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值