2017-01-10 72 views
0

我想删除推文中提到的hashtags(不是单词,只是符号),URL。例如:使用twitter4j从java中的字符串(tweet)中删除hashtag(“#”)URL

  • 鸣叫:克里斯蒂亚诺罗纳尔多是#world##最佳球员。 HTTP:// .....

    现在过滤鸣叫应该像这样:

  • 过滤:克里斯蒂亚诺·罗纳尔多是世界上最好的球员。

我想要的只是纯文本,因为情感分析对这些实体没有很好的表现。这如何实现?

+0

你应该使用字符串替换去除#和正则表达式,你应该可以删除网址。当你遇到正则表达式或其他问题时,你应该询问新的东西。 – Boendal

+0

[从字符串中删除所有出现的字符]可能的重复(http://stackoverflow.com/questions/4576352/remove-all-occurrences-of-char-from-string) – Berger

回答

1

假设您将推文视为字符串,您需要首先删除所有'#',然后检查是否有任何网址。如果字符串中存在网址,则需要将其删除。

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我们只是初步的鸣叫没有不需要#的。

要找到推文中的网址,我会推荐使用正则表达式。我将在这里使用的模式是从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。这与网址一样,但有其他模式。我可以推荐这个网站:http://regexr.com/来学习Regex。如果你需要一个例子让我知道。 – hotrod

+0

Yaa明白了..冷静! –

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

您可以使用此功能来消除停用词,从用户的tweet 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; 
} 

`