2016-12-05 3 views
-2

私はテキスト中の数字とそれに対応する大き​​さを特定しようとしています。次のエラーが発生しました:コンマで数値を浮動小数点数として解析していますか?

UNABLE TO PARSE MAGNITUDE: 6,700

ここに私が何をしているのかを理解するのに役立つコードスニペットがあります。

for(Quantity quantity: originalQuantities){ 
    y = Math.round(quantity.getMagnitude()); 

    if ((roleStrings.get(SemanticRole.TIME) != null && (roleStrings.get(SemanticRole.TIME)).contains(String.valueOf(y)))) 
     continue; 
......................... 

ここ数量は次のように定義されたクラスです。

public class Quantity 
{ 
    private Float  magnitude; 
    private String  multiplier; 
    private String  unit; 
    private UnitType type; 
    private Float  absoluteMagnitude; 

enum UnitType 
{ 
    TIME, MONEY, WEIGHT, VOLUME, NUMBER 
} 
public Quantity(String strMagnitude, String multiplier, String unit, 
      String strType) 
    { 
     this.setMagnitude(strMagnitude); 
     this.multiplier = multiplier; 
     this.unit = unit; 
     this.setType(strType); 
    } 

    public Float getMagnitude() 
    { 
     return magnitude; 
    } 

    public String getMultiplier() 
    { 
     return multiplier; 
    } 

    public String getUnit() 
    { 
     return unit; 
    } 

    public UnitType getType() 
    { 
     return type; 
    } 

どのように私はこの問題を解決するのですか?私はロケールとParseFloatと他の変換を使用しようとしましたが、問題を解決できませんでした。ここで

は、大きさを解析するコードです:私は戻っLocale.US

で遊んでたとき、振幅誤差を解析できません

public static List<Quantity> getQuantitiesFromString(String str) throws ParseException 
{ 
    List<Quantity> quantities = new ArrayList<Quantity>(); 
    //final String REGEX = "^(\\+|-)?([1-9]\\d{0,2}|0)?(,\\d{3}){0,}(\\.\\d+)?"; 
    //NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US); 
    //String numberAsString = numberFormat.format(number); 
    // optional +/- sign followed by numbers separated with a decimal 

    Pattern pattern = Pattern.compile("^[-+]?[0-9]*\\.?[0-9]+"); 
    Pattern pattern1 = Pattern.compile("^[0-9][0-9,-]*-[0-9,-]*[0-9]"); 



    List<String> tokens = Arrays.asList(str.split(" ")); 

    for (int i = 0; i < tokens.size(); i++) 
    { 
     String magnitude = ""; 
     String multiplier = ""; 
     String unit = ""; 
     String type = ""; 

     boolean numFound = false; 

     String token = tokens.get(i); 

     // append all numbers matching pattern into a String 
     Matcher matcher = pattern.matcher(token); 
     Matcher matcher1 = pattern1.matcher(token); 

     while (matcher.find()) 
     { 
      numFound = true; 
      magnitude += matcher.group(); 
     } 

     //ignore for number ranges (e.g. 0-10) 
     while (matcher1.find()) 
     { 
      numFound = false; 
      continue; 
     } 

     if (numFound) 
     { 
      // loop through all words starting from current word 
      // keep adding valid unit words until an invalid unit word is 
      // encountered 
      for (int j = i; j < tokens.size(); j++) 
      { 
       // strip non-alphabetic chars from word 
       String word = tokens.get(j).replaceAll("[^a-zA-Z$%]", "") 
         .toLowerCase(); 

       // see if the stripped word is a unit 
       boolean validUnitWord = false; 
       if (getUnitTypesMap().keySet().contains(word)) 
       { 
        validUnitWord = true; 

        if (getUnitTypesMap().get(word).equalsIgnoreCase(
          "number")) 
        { 
         multiplier += multiplier.isEmpty() ? word : " " 
           + word; 
        } 
        else 
        { 
         unit += unit.isEmpty() ? word : " " + word; 
         type = getUnitTypesMap().get(word); 
        } 
       } 

       // break if invalid unit word; else keep searching in next 
       // words 

       // except for current word (index = i), in which case keep 
       // searching regardless 
       if (!validUnitWord && j != i) 
        break; 
      } 

      quantities.add(new Quantity(magnitude, multiplier, unit, type)); 
     } 
    } 

    return quantities; 
} 

EDIT

ました古いコードに、今のような文字列の場合:

debentures amounting to Rs 6,700 crore

私はgetQuantitiesFromStringから得る出力は次のようになります。コンマの後

QUANTITY: [[magnitude=6.0, multiplier=crore, unit=, type=NUMBER, absoluteMagnitude=null]]

すべてが無視されています。私は22,00.15 22000353等:

"^(\+|-)?([1-9]\d{0,2}|0)?(,\d{3}){0,}(\.\d+)?"

のように番号を検出するために、この正規表現を試してみましたが、何らかの理由でそれは私のコードは動作しません。

+3

ここで、何かを解析するコードはありますか? – f1sh

+2

正しいロケールを入力しましたか?日付、金額、重量の表記はロケールごとに異なります。 – Tschallacka

+0

構文解析を解決する他の方法は、 '、'を '.'に置き換えます。 – XtremeBaumer

答えて

0

^のパターン"^[-+]?[0-9]*\\.?[0-9]+"は、文字列6,700の先頭にしかありません。したがって、6を見つけて700を見つけません。^を削除すると、メソッドは6700をコンストラクタに渡します。

関連する問題