根据单词中字符的位置,确定单词为句中第几个
来源:互联网 发布:上海心动网络怎么样 编辑:程序博客网 时间:2024/05/16 17:11
<pre name="code" class="java">public static void main(String[] args) { // TODO Auto-generated method stub String text = "There was no difference in the effects of AzaC versus AzadC, as both increased the IRF-4 mRNA level" + "in CML-T1 cells as well (data not shown)."; int startIndex = 89; int endIndex = 99; String str = text.substring(0,startIndex); //冒号和逗号 int count = str.split("\\s{1,}"). // "\\s{1,}" 表示一个以上的空白字符 String str2 = text.substring(startIndex,endIndex); int count2 = str2.split("\\s{1,}").length; for(int i=0 ; i<count2 ; i++){ System.out.println(count+i-1); }}
其中要确定位置的是”mRNA level“是句中的第几个单词,我们所知道的信息是m字符在句中的位置是89,level后面空格的位置是99.
以上方式有个缺点:就是遇到标点符号和括号的时候,不能将之认为是一个“单词” ,于是进行改进后的算法如下,以下运用了斯坦福的分词器
package com.a2Process;import java.util.ArrayList;import java.util.List;import java.util.Properties;import java.util.regex.Matcher;import java.util.regex.Pattern;import edu.stanford.nlp.ling.CoreLabel;import edu.stanford.nlp.ling.CoreAnnotations.SentencesAnnotation;import edu.stanford.nlp.ling.CoreAnnotations.TextAnnotation;import edu.stanford.nlp.ling.CoreAnnotations.TokensAnnotation;import edu.stanford.nlp.pipeline.Annotation;import edu.stanford.nlp.pipeline.StanfordCoreNLP;import edu.stanford.nlp.util.CoreMap;public class FindPosition2 {public static void main(String[] args) {// TODO Auto-generated method stub String text = "To define the requirements of Foxp3 with respect to inhibition of NF-kappaB-dependent transcription, we utilized a mutant of Foxp3 acking the FKH domain (Figure 2A) [16], similar to the scurfy mutant Foxp3 of mice, and a mutant Foxp3 protein from a patient with IPEX [4,11,14,17]."; int startIndex = 143; int endIndex = 153; String str = text.substring(0,startIndex-1); int preCountWord = wordNum(str); String selectedStr = text.substring(startIndex-1,endIndex); int selectedCountWord = wordNum(selectedStr); for(int i=0 ; i<selectedCountWord; i++){ System.out.println(preCountWord+i); } }/** * 获取一个字符串里包含多少个单词 */public static int wordNum(String str){Properties props = new Properties();props.put("annotators", "tokenize, ssplit");StanfordCoreNLP pipeline = new StanfordCoreNLP(props); Annotation document = new Annotation(str); pipeline.annotate(document);List<CoreMap> sentences = document.get(SentencesAnnotation.class);int wordnum = 0;for(CoreMap sentence : sentences){ for(CoreLabel token : sentence.get(TokensAnnotation.class)){ wordnum++; String word = token.get(TextAnnotation.class); System.out.println(word); } }return wordnum;}}
0 0
- 根据单词中字符的位置,确定单词为句中第几个
- 判断一个句子的单词中是否有句号,若有,则显示第几个单词中第几个位置上;若无,则返回flase
- POJ 2880:句中最长的单词
- Bailian2880 句中最长的单词【字符串】
- 输出一行字符中单词的个数
- 百练 2880 句中最长的单词
- 一句英文中,单词最长的那个
- 颠倒字符串中单词字符
- 如何统计一行字符中有多少个单词
- 统计一行字符中有多少个单词
- 查询字符在字符串中第几次出现的位置
- 每天学习一算法系列(10)(输入一句英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变,句子中单词以空格符隔开)
- 向左旋转k个字符vs字符串中单词的逆转
- 统计单词中出现的单词数
- 给你一个单词a,如果通过交换单词中字母的顺序可以得到另外的单词b,那么定义b是a的兄弟单词。现在给你一个字典,用户输入一个单词,让你根据字典找出这个单词有多少个兄弟单词。
- Eclipse中鼠标左键点中一个单词,自动出现该单词的所有位置
- 面试题38:翻转句子中单词的顺序,单词内字符的顺序不变
- 翻转句子中单词的顺序,但单词内字符的顺序不变
- Errors running builder 'javascript validator' on project
- Container With Most Water
- ios开发之倒计时实现的两种方法
- Web_PHP_PHP XML操作浅谈;
- Regular Expression Matching
- 根据单词中字符的位置,确定单词为句中第几个
- 给非计算机专业大二学生的建议
- JPA初识
- 关于data-ng-model和ng-model
- Palindrome Number
- gcc源代码分析之varasm.c
- mysql having的用法
- 4Sum
- ExtJs xtype一览表