twitter / twitter-text

Twitter Text Libraries. This code is used at Twitter to tokenize and parse text to meet the expectations for what can be used on the platform.
https://developer.twitter.com/en/docs/counting-characters
Apache License 2.0
3.07k stars 520 forks source link

[java] Correct way to create TwitterTextWeightedRange? #324

Closed itchyny closed 2 years ago

itchyny commented 4 years ago

Now that 3.1.0 removes configurationFromJson, users cannot create their own TwitterTextConfiguration. I used to initialize my TwitterTextConfiguration using JSON but the feature is removed. I have to use the builder, but I can't create TwitterTextWeightedRange because its methods are private.

class Example {
  private val DEFAULT_VERSION: Int = 3;
  private val DEFAULT_WEIGHTED_LENGTH: Int = 140;
  private val DEFAULT_SCALE: Int = 100;
  private val DEFAULT_WEIGHT: Int = 200;
  private val DEFAULT_EMOJI_PARSING_ENABLED: Boolean = true;
  private val DEFAULT_TRANSFORMED_URL_LENGTH: Int = 23;
  private val DEFAULT_RANGES: java.util.List[TwitterTextWeightedRange] = new java.util.ArrayList(
    // value setStart is not a member of com.twitter.twittertext.TwitterTextConfiguration.TwitterTextWeightedRange
    new TwitterTextWeightedRange().setStart(0).setEnd(4351).setWeight(100),
  )

  override def getMyOwnTwitterTextConfig: TwitterTextConfiguration = {
    new TwitterTextConfiguration.Builder()
        .setVersion(DEFAULT_VERSION)
        .setMaxWeightedTweetLength(DEFAULT_WEIGHTED_LENGTH)
        .setScale(DEFAULT_SCALE)
        .setDefaultWeight(DEFAULT_WEIGHT)
        .setEmojiParsingEnabled(DEFAULT_EMOJI_PARSING_ENABLED)
        .setRanges(DEFAULT_RANGES)
        .setTransformedURLLength(DEFAULT_TRANSFORMED_URL_LENGTH)
        .build();
  }

}

Expected behavior

User can initialize their own TwitterTextWeightedRange.

Actual behavior

The setStart method of TwitterTextWeightedRange is private so user can't build their own TwitterTextConfiguration with custom ranges. I propose

Steps to reproduce the behavior

Please list all relevant steps to reproduce the observed behavior.

Comments

Honestly I don't need to customize all the fields. I just want to change the MaxWeightedTweetLength from the default configuration. So another solution is exposing the default builder.

  public static Builder getDefaultBuilder() {
    return new TwitterTextConfiguration.Builder()
        .setVersion(DEFAULT_VERSION)
        .setMaxWeightedTweetLength(DEFAULT_WEIGHTED_LENGTH)
        .setScale(DEFAULT_SCALE)
        .setDefaultWeight(DEFAULT_WEIGHT)
        .setEmojiParsingEnabled(DEFAULT_EMOJI_PARSING_ENABLED)
        .setRanges(DEFAULT_RANGES)
        .setTransformedURLLength(DEFAULT_TRANSFORMED_URL_LENGTH)
  }
roodybigass commented 4 years ago

😎😎😎😎