/**
   * Searches for a searchKey in the tree. Also retrieves most relevant snippet.
   *
   * @param searchKey, user typed search string.
   * @return String, the most relevant snippet.
   */
  public String search(String searchKey) {
    searchKey = searchKey.toLowerCase();
    searchKey = searchKey.trim();

    searchTerms = searchKey.split("\\W"); // Split around all non-word characters.

    ArrayList<Integer> termIndex = new ArrayList<Integer>();
    ArrayList<Integer> temp;
    Snippet mostRelevantSnippet = null;

    if (searchTerms.length != 0) {
      // Retrieve all indexes of search terms from trieTree
      for (int i = 0; i < searchTerms.length; i++) {
        temp = trieTree.getWordIndexes(searchTerms[i]);
        if (temp != null) termIndex.addAll(temp);
      }

      ArrayList<Snippet> allSnippets = new ArrayList<Snippet>();
      // Now extract snippets around each term search result.
      for (int i = 0; i < termIndex.size(); i++) {
        allSnippets.add(docParser.getSnippet(termIndex.get(i)));
      }

      for (int i = 0; i < allSnippets.size(); i++)
        System.out.println("Snippet " + i + " : " + allSnippets.get(i));

      // Score each snippet and extract most relevant one.
      mostRelevantSnippet = relevanceEngine.getMostRelevant(allSnippets, searchTerms);
    }

    if (mostRelevantSnippet == null) return null;

    return mostRelevantSnippet.toString();
  }
  public void testUsesCorrectIndentationOnASnippetWithBlankLines() throws Exception {
    String expected = "assertEquals(2, 1 + 1);\n" + "\n" + "assertEquals(3, 1 + 2);\n";

    Snippet snippet = snippetReader.readSnippet("blanklines");
    StringWriter written = new StringWriter();
    snippet.writeContent(written, false, false);
    assertEquals(expected, written.toString());
  }
Exemplo n.º 3
0
 /** Test of getSnippet method, of class Snippet. */
 @Test
 public void testGetSnippet() {
   System.out.println("getSnippet");
   Snippet instance = new Snippet();
   instance.setSnippet("snip1");
   String expResult = "snip1";
   String result = instance.getSnippet();
   assertEquals(expResult, result);
 }
Exemplo n.º 4
0
  /** Test of setSnippet method, of class Snippet. */
  @Test
  public void testSetSnippet() {
    System.out.println("setSnippet");
    String snippet = "snip1";
    Snippet instance = new Snippet();
    instance.setSnippet(snippet);

    assertEquals("snip1", instance.getSnippet());
  }
Exemplo n.º 5
0
  public Snippet findById(Long id) {
    final String sql = "SELECT * FROM snippet " + "WHERE id=?";
    Map<String, Object> row = jdbcTemplate.queryForMap(sql, id);
    Snippet snippet = new Snippet();
    snippet.id = id;
    snippet.title = (String) row.get("title");
    snippet.language = (String) row.get("language");
    snippet.code = (String) row.get("code");

    return snippet;
  }
 private Snippet createSnippet(Range inMatch, int inExpandRadius, DocumentHolder inHolder)
     throws IOException {
   List<List<SnippetToken>> sentences =
       inHolder.expandPosition(inMatch.getMinimumInteger(), inExpandRadius);
   Snippet result = new Snippet();
   for (List<SnippetToken> sentence : sentences) {
     result.addSentence(sentence);
   }
   result.markRange(inMatch);
   return result;
 }
 public void testCanReadSnippetsWithSeveralLinesAndNestedSnippet() throws IOException {
   SnippetReader snippetReader = new SnippetReader(getResource("SnippetReaderTest.java"));
   Snippet snippet = snippetReader.readSnippet("multilineSnippet");
   StringWriter written = new StringWriter();
   snippet.writeContent(written, false, false);
   assertEquals(
       "assertEquals(2, 1 + 1);\n"
           + "if(true) {\n"
           + "    assertEquals(6, 2 * 3);\n"
           + "}\n"
           + "assertEquals(2, 4 / 2);\n",
       written.getBuffer().toString());
 }
  private void addMatchToDocument(
      MatchDescriptor inMatch, DocForReport inDocument, DocumentHolder inHolder)
      throws IOException {
    int snippetsNumber = inDocument.snippets.size();
    if (!inDocument.snippets.isEmpty()) {
      Snippet lastSnippet = inDocument.snippets.get(snippetsNumber - 1);
      if (lastSnippet.matchIsEnclosed(inMatch.match)) {
        lastSnippet.markRange(inMatch.match);
        return;
      }
    }

    inDocument.addSnippet(createSnippet(inMatch.match, SNIPPET_EXPAND_RADIUS, inHolder));
  }
Exemplo n.º 9
0
  public static ISnippet createNewSnippet() {
    ISnippetPreference preference = PreferencesBuilder.getSnippetPreference();
    int no = preference.getInt(ISnippetPreference.NEW_SNIPPETS_NUMBER, 0);
    Snippet snippet = new Snippet();
    String body = "";
    String title = "Snippet " + (no + 1);
    snippet.setBody(body);
    snippet.setTitle(title);
    snippet.setCreateDate(new Date());

    preference.setValue(ISnippetPreference.NEW_SNIPPETS_NUMBER, no + 1);

    // スニペット番号の保存。 保存に失敗した場合はログに詳細が保存される。
    preference.saveQuietly();
    return snippet;
  }
Exemplo n.º 10
0
  public SnippetFile(File snipFile) {
    this.snipFile = snipFile;
    Map<String, Snippet> m = new HashMap<String, Snippet>();
    snippets = m;
    try {
      String content = "";
      try {
        @SuppressWarnings("resource")
        String content1 = (new Scanner(snipFile)).useDelimiter("\\Z").next();
        content = content1;
      } catch (NoSuchElementException ex) {
        // empty file ignore error
        return;
      }

      Matcher mx = p.matcher(content);
      while (mx.find()) {
        Snippet s = new Snippet(mx.group(1));
        m.put(s.getName(), s);
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
  }
Exemplo n.º 11
0
  private List<Snippet> queryForSnippets(String sql, Object... sqlArgs) {
    List<Snippet> snippets = new ArrayList<>();
    List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql, sqlArgs);
    for (Map row : rows) {
      Snippet snippet = new Snippet();
      snippet.id = (Long) row.get("id");
      snippet.title = (String) row.get("title");
      snippet.language = (String) row.get("language");
      snippet.code = (String) row.get("code");
      snippet.dateAdded = (Date) row.get("date_added");
      snippets.add(snippet);
    }

    return snippets;
  }
Exemplo n.º 12
0
 default Snippets cat(final Iterable<Snippet> ss) {
   final Snippets $ = make();
   for (final Snippet s1 : this) for (final Snippet s2 : ss) $.add(s1.toString() + s2.toString());
   return $;
 }
 public void addSnippet(Snippet inSnippet) {
   inSnippet.sid = snippets.size();
   snippets.add(inSnippet);
 }
Exemplo n.º 14
0
  /** 初始化界面中出现的布局 */
  private void initLayout() {
    View firstView = views.get(0); // 得到第一个页面view
    Log.i(TAG, "firstView==" + firstView);
    imageView_user_imgOne = (ImageView) firstView.findViewById(R.id.imageView_user_imgOne);
    textView_user_city = (TextView) firstView.findViewById(R.id.textView_user_city);
    textView_user_date = (TextView) firstView.findViewById(R.id.textView_user_date);
    textView_user_temp = (TextView) firstView.findViewById(R.id.textView_user_temp);
    textView_user_index_d = (TextView) firstView.findViewById(R.id.textView_user_index_d);
    textView_user_index_uv = (TextView) firstView.findViewById(R.id.textView_user_index_uv);
    textView_user_index_xc = (TextView) firstView.findViewById(R.id.textView_user_index_xc);
    textView_user_index_tr = (TextView) firstView.findViewById(R.id.textView_user_index_tr);
    textView_user_index_co = (TextView) firstView.findViewById(R.id.textView_user_index_co);
    textView_user_index_cl = (TextView) firstView.findViewById(R.id.textView_user_index_cl);
    textView_user_index_ls = (TextView) firstView.findViewById(R.id.textView_user_index_ls);
    textView_user_index_ag = (TextView) firstView.findViewById(R.id.textView_user_index_ag);

    textView_user_weather = (TextView) firstView.findViewById(R.id.textView_user_weather);
    linear_user_first_other = (LinearLayout) firstView.findViewById(R.id.linear_user_first_other);

    View firstView2 = views.get(1); // 得到第二个页面view
    editText_user_search_text = (EditText) firstView2.findViewById(R.id.editText_user_search_text);
    button_user_search_delete = (Button) firstView2.findViewById(R.id.button_user_search_delete);
    listView_user_search_person =
        (ListView) firstView2.findViewById(R.id.listView_user_search_person);
    lin_user_search_other = (LinearLayout) firstView2.findViewById(R.id.lin_user_search_other);
    button_user_search_classify =
        (Button) firstView2.findViewById(R.id.button_user_search_classify);
    button_user_search_derma = (Button) firstView2.findViewById(R.id.button_user_search_derma);
    //	button_user_search_helper = (Button) firstView2
    //			.findViewById(R.id.button_user_search_helper);
    //	button_user_search_about = (Button) firstView2
    //			.findViewById(R.id.button_user_search_about);

    //	Snippet.setButtonFocusChanged(button_user_search_about);
    Snippet.setButtonFocusChanged(button_user_search_classify);
    // Snippet.setButtonFocusChanged(button_user_search_helper);
    Snippet.setButtonFocusChanged(button_user_search_derma);

    OnClickListener myClickListener =
        new OnClickListener() {

          @Override
          public void onClick(View v) {
            int vId = v.getId();
            if (vId == button_user_search_delete.getId()) {
              editText_user_search_text.setText(""); // 清空编辑显示框
              lin_user_search_other.setVisibility(View.VISIBLE);
            } else if (vId == button_user_search_derma.getId()) {
              goDermaPage();
              //	} else if (vId == button_user_search_helper.getId()) {
              //		goGuidePage();
              //	} else if (vId == button_user_search_about.getId()) {
              //		goAboutPage();
            } else if (vId == button_user_search_classify.getId()) {
              goClassifyPage();
            }
          }
        };

    /* 删除小按钮单击操作 */
    button_user_search_delete.setOnClickListener(myClickListener);
    /* 其它单击监听 */
    button_user_search_derma.setOnClickListener(myClickListener);
    //	button_user_search_helper.setOnClickListener(myClickListener);
    //	button_user_search_about.setOnClickListener(myClickListener);
    button_user_search_classify.setOnClickListener(myClickListener);

    /* 编辑框的内容监听 */
    editText_user_search_text.addTextChangedListener(
        new TextWatcher() {

          @Override
          public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) {

            String inputStr = s.toString();
            /* 判断是否存在删除小图标 */
            if (inputStr.equals("")) {
              button_user_search_delete.setVisibility(View.INVISIBLE);
              lin_user_search_other.setVisibility(View.VISIBLE);
            } else {
              button_user_search_delete.setVisibility(View.VISIBLE);
              lin_user_search_other.setVisibility(View.GONE);
            }

            listViewAdapter(inputStr); // 显示得到的数据
          }

          @Override
          public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

          }

          @Override
          public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // TODO Auto-generated method stub

          }
        });
    /* listView监听 */
    listView_user_search_person.setOnItemClickListener(
        new AdapterView.OnItemClickListener() {

          @Override
          public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            TextView text = (TextView) arg1.findViewById(R.id.textView_item_user_cityName);
            String str = text.getText().toString();
            Log.i(TAG, "str==" + str);
            String cityIdStr = cityAllMap.get(str);
            Log.i(TAG, "cityIdStr==" + cityIdStr);
            if (cityIdStr != null && !cityIdStr.equals("")) {
              setCityFatherId(cityIdStr);
              Intent intent = new Intent(BianhuaActivity.this, BianhuaActivity.class);
              BianhuaActivity.this.startActivity(intent);
            } else {
              Toast.makeText(BianhuaActivity.this, "没找到这个地点的id", Toast.LENGTH_LONG).show();
            }
          }
        });
    // imageView_user_bg.setImageResource(getImageId(backgroundId));
    String backgroundId = getBgFatherId();
    Log.i(TAG, "backgroundId==" + backgroundId);
    imageView_user_bg.setBackgroundResource(getImageId1(backgroundId));
  }