2017-01-10 7 views
0

ツイートに記載されているハッシュタグ(単語ではなく、シンボルのみ)を削除します。例:twitter4jを使用してjavaの文字列(ツイート)からハッシュタグ(「#」)URLを削除します。

  • Tweet:クリスティアーノロナウドは#worldの#bestプレーヤーです。 http:フィルタ

  • :// .....今

    フィルタのつぶやきは次のようになりますクリスティアーノ・ロナウドは世界で最高の選手です。

感想分析がこれらのエンティティではうまくいかないため、私が必要とするのはプレーンテキストです。これはどのように実装できますか?

+0

文字列置換を使用して#を削除し、正規表現を使用してURLを削除する必要があります。正規表現や何かに問題があるときは、新しく質問してください。 – Boendal

+0

[文字列からすべての文字を取り除く]の複製が可能です。(http://stackoverflow.com/questions/4576352/remove-all-occurrences-of-char-from-string) – Berger

答えて

1

ツイートが文字列であると仮定すると、まずすべての「#」を削除してから、すべてのURLをチェックする必要があります。 StringにURLがある場合は、それらを削除する必要があります。

Stringクラスは、String内のStringを他のStringに置き換えるメソッドを提供します。 #を削除するには、次の操作を行います。

//Creating dummy tweet.. you would get it from wherever else 
String tweet = "Ronaldo is the #best player in the #world. http://www.google.de"; 

// Replacing "#" with "" (nothing) 
String tweetWithoutHashtag = tweet.replace("#", ""); 

tweetWithoutHashtagだけで、不要な#さんなしで私たちの最初のつぶやきです。

このツイートにURLを見つけるには、Regexの使用をお勧めします。ここで使用するパターンはthisです。

//Create Regex pattern to find urls 
Pattern urlPattern = Pattern.compile("(http|ftp|https)://([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\[email protected]?^=%&/~+#-])?"); 

//Create a matcher with our 'urlPattern' 
Matcher matcher = urlPattern.matcher(tweetWithoutHashtag); 

//Check if matcher finds url 
if(matcher.find()) { 
    //Matcher found urls 
    //Removing them now.. 
    String tweetWithoutHashtagAndUrl = matcher.replaceAll(""); 
    //Use new tweet here  
} else { 
    //Matcher did not find any urls, which means the 'tweetWithoutHashtag' already is ready for further usage 
    String tweetWithoutHashtagAndUrl = tweetWithoutHashtag; 
} 
+0

良い解決策。ハッシュタグ全体を削除する必要がある場合、言葉を含めてどうすればいいか教えてください。 –

+0

@RakshitBhatnagarもう一度Regexを使う必要があります。 URLと同じですが、別のパターンを使用します。私はこのサイトを推奨することができます:http://regexr.com/ Regexを学ぶ。例が必要なら私に知らせてください。 – hotrod

+0

よろしくお願いします。クール!! –

0

Stringクラスには、文字/正規表現のすべての出現箇所を定義済み(空の空文字列)に置き換えるreplaceAllメソッドがあります。あなたはJavadoc hereを見ることができます。

String tweet = "Cristiano Ronaldo is the #best player in the #world. http://www.google.com"; 
String tweetWithoutHash = tweet.replaceAll("#", ""); 
System.out.println(tweetWithoutHash); // Cristiano Ronaldo is the best player in the world. http://www.google.com 
String urlPattern = "((https?|ftp|gopher|telnet|file|Unsure|http):((//)|(\\\\))+[\\w\\d:#@%/;$()~_?\\+-=\\\\\\.&]*)"; 
String tweetWithoutHashAndUrl = tweetWithoutHash.replaceAll(urlPattern, ""); 
System.out.println(tweetWithoutHashAndUrl); // Cristiano Ronaldo is the best player in the world. 
0

ストップワードは、あなたがあなた自身のリストを追加したり、このステップを削除する必要がありますリストについては、あなたは、ユーザーのツイートremoveStopwords(つぶやき)からストップワード、タグや注釈を削除するには、この機能を使用することができます。 `

public static ArrayList<String> removeStopwords (String tweet){ 
    ArrayList<String> wordsList = new ArrayList<String>(); 
    try{ 
      StringBuilder builder = new StringBuilder(tweet); 
      String[] words = builder.toString().split("\\s"); 
      for (String word : words){ 
       wordsList.add(word.toLowerCase().trim()); 
      } 
      wordsList.removeAll(stopwords); 
      for(int ii = 0; ii < wordsList.size(); ii++){ 
        String [] spl = wordsList.get(ii).split("@"); 
        if (spl.length > 1){ 
         wordsList.remove(ii); 
        }else { 
         String [] spl1 = wordsList.get(ii).split("#"); 
         if (spl1.length > 1){ 
          wordsList.remove(ii); 
         } 
        } 
       if ((wordsList.get(ii).length() == 0)){ 
        wordsList.remove(ii); 
       } 
      } 
     }catch(Exception ex){ 
      System.out.println(ex); 
     } 
    return wordsList; 
} 

関連する問題