【问题标题】:CoreNLP API for N-grams with position带有位置的 N-gram 的 CoreNLP API
【发布时间】:2015-08-05 04:16:33
【问题描述】:

CoreNLP 是否有用于获取具有位置等的 ngram 的 API?

例如,我有一个字符串“我有最好的车”。 如果我使用 mingrams=1 和 maxgrams=2。 我应该得到如下所示的内容。我知道带有 ngram 函数的 stringutil 但如何获得位置。

(I,0)
(I have,0)
(have,1)
(have the,1)
(the,2)
(the best,2) etc etc

基于我传递的字符串。

非常感谢任何帮助。

谢谢

【问题讨论】:

    标签: nlp stanford-nlp n-gram pos-tagger


    【解决方案1】:

    我在实用程序中看不到任何内容。以下是一些帮助示例代码:

    import java.io.*;
    import java.util.*;
    import edu.stanford.nlp.io.*;
    import edu.stanford.nlp.ling.*;
    import edu.stanford.nlp.pipeline.*;
    import edu.stanford.nlp.trees.*;
    import edu.stanford.nlp.semgraph.*;
    import edu.stanford.nlp.trees.TreeCoreAnnotations.*; 
    import edu.stanford.nlp.util.*;
    
    
    public class NGramPositionExample {
    
    
        public static List<List<String>> getNGramsPositions(List<String> items, int minSize, int maxSize) {
            List<List<String>> ngrams = new ArrayList<List<String>>();
        int listSize = items.size();
        for (int i = 0; i < listSize; ++i) {
            for (int ngramSize = minSize; ngramSize <= maxSize; ++ngramSize) {
            if (i + ngramSize <= listSize) {
                List<String> ngram = new ArrayList<String>();
                for (int j = i; j < i + ngramSize; ++j) {
                ngram.add(items.get(j));
                }
                        ngram.add(Integer.toString(i));
                ngrams.add(ngram);
            }
            }
        }
        return ngrams;
        }
    
    
            public static void main (String[] args) throws IOException {
                String testString = "I have the best car";
                List<String> tokens = Arrays.asList(testString.split(" "));
                List<List<String>> ngramsAndPositions = getNGramsPositions(tokens,1,2);
                for (List<String> np : ngramsAndPositions) {
                    System.out.println(Arrays.toString(np.toArray()));
                }
            }
    }
    

    您可以直接剪切和粘贴该实用程序方法。

    这可能是一个有用的添加功能,所以我将把它放在我们的工作清单中。

    【讨论】:

      【解决方案2】:

      只需花费一些代码在 scala 中重写它。它只是上面的代码将其更改为scala。输出会像

      NgramInfo(I,0)NgramInfo(I have,0)NgramInfo(have,1)NgramInfo(have the,1)NgramInfo(the,2)NgramInfo(the best,2)NgramInfo(best,3)NgramInfo(best car,3)NgramInfo(car,4) 
      

      下面是case类的方法

         def getNgramPositions(items: List[String], minSize: Int, maxSize: Int): List[NgramInfo] = {
              var ngramList = new ListBuffer[NgramInfo]
              for (i <- 0 to items.size by 1) {
                for (ngramSize <- minSize until maxSize by 1) {
                  if (i + ngramSize <= items.size) {
                    var stringList = new ListBuffer[String]
                    for (j <- i to i + ngramSize by 1) {
                      if (j < items.size) {
                        stringList += items(j)
                        ngramList += new NgramInfo(stringList.mkString(" "), i)
                      }
                    }
                  }
                }
              }
              ngramList.toList
            }
      
      case class NgramInfo(term: String, termPosition: Int) extends Serializable
      

      谢谢

      【讨论】:

        猜你喜欢
        • 2020-06-28
        • 1970-01-01
        • 2014-09-08
        • 2012-12-09
        • 1970-01-01
        • 2018-04-05
        • 1970-01-01
        • 2016-08-01
        • 1970-01-01
        相关资源
        最近更新 更多